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'; 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()); return client.fetch({ url: `${API_SERVER}/coMarket/api/getShopPackages`, method: 'post', data: { idCommercialLead: (cl && cl.value) || 0, search } }) .then(response => { if(response.data && response.data.packages){ dispatch(recieveShopPackages(response.data.packages)) } }) .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()); return client.fetch({url: `${API_SERVER}/coMarket/api/getAllCommercialLeads`}) .then(response => { if(response.data && response.data.commercialLeads){ const clOptions = generateClOptions(response.data.commercialLeads); dispatch(recieveShopCommercialLeads(clOptions)); if (clOptions.length) { dispatch(selectCommercialLead(clOptions[0])); dispatch(fetchShopPackages(clOptions[0])); } } }) .catch(error => { client.onError(error, dispatch); }); } } export const selectCommercialLead = (cl) => ({ type: SELECT_SHOP_COMMERCIAL_LEAD, selectedCommercialLead: cl });