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

90 lines
2.2 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,
2018-10-16 06:45:28 +02:00
REQUEST_SHOPS,
RECEIVE_SHOPS,
SELECT_SHOP
2018-06-14 16:49:28 +02:00
} 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
});
2018-10-16 06:45:28 +02:00
export const fetchShopPackages = (shop, search) => {
2018-06-14 16:49:28 +02:00
return dispatch => {
dispatch(requestShopPackages());
2018-08-08 14:40:00 +02:00
let searchParam = search ? '?search=' +search : ''
2018-10-11 07:12:53 +02:00
return client.fetch({
2018-10-16 06:45:28 +02:00
url: `${API_SERVER}/wp-json/wc/v2/products?cl_id=${shop.id}` + searchParam,
2018-08-08 14:40:00 +02:00
})
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);
});
}
}
2018-10-16 06:45:28 +02:00
const requestShops = () => ({
type: REQUEST_SHOPS
2018-06-14 16:49:28 +02:00
});
2018-10-16 06:45:28 +02:00
const receiveShops = (json) => ({
type: RECEIVE_SHOPS,
shops: json
2018-06-14 16:49:28 +02:00
});
2018-10-16 06:45:28 +02:00
const generateShopOptions = (shops) => {
shops.forEach((shop) => {
shop.value = shop.id;
shop.label = shop.name;
2018-06-14 16:49:28 +02:00
});
2018-10-16 06:45:28 +02:00
return shops;
2018-06-14 16:49:28 +02:00
}
2018-10-16 06:45:28 +02:00
export const fetchShops = () => {
2018-06-14 16:49:28 +02:00
return dispatch => {
2018-10-16 06:45:28 +02:00
dispatch(requestShops());
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/customer/0/shops` })
2018-10-11 07:12:53 +02:00
.then(response => {
if(response.data){
2018-10-16 06:45:28 +02:00
const shopOptions = generateShopOptions(response.data);
2018-10-11 07:12:53 +02:00
2018-10-16 06:45:28 +02:00
dispatch(receiveShops(shopOptions));
2018-10-11 07:12:53 +02:00
2018-10-16 06:45:28 +02:00
if (shopOptions.length) {
dispatch(selectShop(shopOptions[0]));
dispatch(fetchShopPackages(shopOptions[0]));
2018-10-11 07:12:53 +02:00
}
}
})
.catch(error => {
client.onError(error, dispatch);
});
2018-06-14 16:49:28 +02:00
}
}
2018-10-16 06:45:28 +02:00
export const selectShop = (shopInfo) => ({
type: SELECT_SHOP,
selectedShop: shopInfo
2018-06-14 16:49:28 +02:00
});