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

124 lines
3.3 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,
2019-01-07 21:15:06 +01:00
SEARCH_SHOP_PACKAGES_REQUEST,
SEARCH_SHOP_PACKAGES_RESULT,
2018-10-16 06:45:28 +02:00
REQUEST_SHOPS,
RECEIVE_SHOPS,
2019-01-07 21:15:06 +01:00
SELECT_SHOP,
SHOP_PAGE_SIZE
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
});
2019-01-07 21:15:06 +01:00
const recieveShopPackages = (packages, page = 1) => ({
2018-06-14 16:49:28 +02:00
type: RECIEVE_SHOP_PACKAGES,
isLoading: false,
2019-01-07 21:15:06 +01:00
shopPackages: packages,
page: page,
2018-06-14 16:49:28 +02:00
});
2019-01-07 21:15:06 +01:00
export const fetchShopPackages = (shop, page = 1) => {
2018-06-14 16:49:28 +02:00
return dispatch => {
dispatch(requestShopPackages());
2018-08-08 14:40:00 +02:00
2018-10-11 07:12:53 +02:00
return client.fetch({
2019-01-07 21:15:06 +01:00
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}&page=${page}&per_page=${SHOP_PAGE_SIZE + 1}`,
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) {
2019-01-07 21:15:06 +01:00
const packages = response.data.map(wcPackage => fromWCPackage(wcPackage));
dispatch(recieveShopPackages(packages, page))
2018-06-14 16:49:28 +02:00
}
})
.catch(error => {
client.onError(error, dispatch);
});
}
2019-01-07 21:15:06 +01:00
};
const searchShopPackagesRequest = () => ({
type: SEARCH_SHOP_PACKAGES_REQUEST,
isLoading: true
});
const searchShopPackagesResult = (packages) => ({
type: SEARCH_SHOP_PACKAGES_RESULT,
isLoading: false,
shopPackages: packages,
});
export const searchShopPackages = (shop, search) => {
return dispatch => {
dispatch(searchShopPackagesRequest());
return client.fetch({
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}&search=${search}`,
})
.then(response => {
if (response.data) {
const packages = response.data.map(wcPackage => fromWCPackage(wcPackage));
dispatch(searchShopPackagesResult(packages))
}
})
.catch(error => {
client.onError(error, dispatch);
});
}
};
2018-06-14 16:49:28 +02:00
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-17 00:36:19 +02:00
export const fetchShops = (userId) => {
2018-06-14 16:49:28 +02:00
return dispatch => {
2018-10-16 06:45:28 +02:00
dispatch(requestShops());
2018-10-17 00:36:19 +02:00
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/customer/${userId}/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
});