Files
old-new-wiaas/frontend/src/actions/coMarket/coMarketPackagesActions.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-06-14 16:49:28 +02:00
import {
API_SERVER
} from '../../config';
import HtmlClient from '../../helpers/HtmlClient';
import {
REQUEST_SHOP_PACKAGES,
RECIEVE_SHOP_PACKAGES,
REQUEST_SHOP_COMMERCIAL_LEADS,
RECIEVE_SHOP_COMMERCIAL_LEADS,
SELECT_SHOP_COMMERCIAL_LEAD
} from '../../constants/coMarketConstants';
2018-08-26 15:41:08 +02:00
import { fromWCPackage } from '../../helpers/PackageHelper';
2018-06-14 16:49:28 +02:00
const client = new HtmlClient();
const requestShopPackages = () => ({
type: REQUEST_SHOP_PACKAGES,
isLoading: true
});
const recieveShopPackages = (json) => ({
type: RECIEVE_SHOP_PACKAGES,
isLoading: false,
shopPackages: json
});
export const fetchShopPackages = (cl, search) => {
return dispatch => {
dispatch(requestShopPackages());
2018-08-08 14:40:00 +02:00
let searchParam = search ? '?search=' +search : ''
2018-06-14 16:49:28 +02:00
return client.fetch({
2018-08-08 14:40:00 +02:00
url: `${API_SERVER}/wp-json/wc/v2/products` + searchParam,
// TODO: Add comercialLead parameter after the support for it is added on backend
// method: 'post',
// data: {
// idCommercialLead: (cl && cl.value) || 0,
// search
// }
})
2018-06-14 16:49:28 +02:00
.then(response => {
2018-08-08 14:40:00 +02:00
if (response.data) {
2018-08-26 15:41:08 +02:00
dispatch(recieveShopPackages(response.data.map(wcPackage => fromWCPackage(wcPackage))))
2018-06-14 16:49:28 +02:00
}
})
.catch(error => {
client.onError(error, dispatch);
});
}
}
const requestShopCommercialLeads = () => ({
type: REQUEST_SHOP_COMMERCIAL_LEADS
});
const recieveShopCommercialLeads = (json) => ({
type: RECIEVE_SHOP_COMMERCIAL_LEADS,
commercialLeads: json
});
const generateClOptions = (commercialLeads) => {
commercialLeads.forEach((cl) => {
cl.value = cl.idCommercialLead;
cl.label = cl.commercialLeadName;
});
return commercialLeads;
}
export const fetchShopCommercialLeads = () => {
return dispatch => {
dispatch(requestShopCommercialLeads());
2018-08-08 14:40:00 +02:00
// TODO: FetchcomercialLead after the support for it is added on backend
// return client.fetch({url: `${API_SERVER}/coMarket/api/getAllCommercialLeads`})
// .then(response => {
// if(response.data && response.data.commercialLeads){
const clOptions = generateClOptions([{ "idCommercialLead": 14, "commercialLeadName": "Coor Service Management", "mail": "rikard@co-ideation.com" }]);
dispatch(recieveShopCommercialLeads(clOptions));
if (clOptions.length) {
dispatch(selectCommercialLead(clOptions[0]));
dispatch(fetchShopPackages(clOptions[0]));
}
2018-06-14 16:49:28 +02:00
}
2018-08-08 14:40:00 +02:00
// })
// .catch(error => {
// client.onError(error, dispatch);
// });
2018-06-14 16:49:28 +02:00
}
2018-08-08 14:40:00 +02:00
// }
2018-06-14 16:49:28 +02:00
export const selectCommercialLead = (cl) => ({
type: SELECT_SHOP_COMMERCIAL_LEAD,
selectedCommercialLead: cl
});