124 lines
3.3 KiB
JavaScript
124 lines
3.3 KiB
JavaScript
import {
|
|
API_SERVER
|
|
} from '../../config';
|
|
import HtmlClient from '../../helpers/HtmlClient';
|
|
import {
|
|
REQUEST_SHOP_PACKAGES,
|
|
RECIEVE_SHOP_PACKAGES,
|
|
SEARCH_SHOP_PACKAGES_REQUEST,
|
|
SEARCH_SHOP_PACKAGES_RESULT,
|
|
REQUEST_SHOPS,
|
|
RECEIVE_SHOPS,
|
|
SELECT_SHOP,
|
|
SHOP_PAGE_SIZE
|
|
} from '../../constants/coMarketConstants';
|
|
import { fromWCPackage } from '../../helpers/PackageHelper';
|
|
|
|
const client = new HtmlClient();
|
|
|
|
const requestShopPackages = () => ({
|
|
type: REQUEST_SHOP_PACKAGES,
|
|
isLoading: true
|
|
});
|
|
const recieveShopPackages = (packages, page = 1) => ({
|
|
type: RECIEVE_SHOP_PACKAGES,
|
|
isLoading: false,
|
|
shopPackages: packages,
|
|
page: page,
|
|
});
|
|
|
|
export const fetchShopPackages = (shop, page = 1) => {
|
|
return dispatch => {
|
|
dispatch(requestShopPackages());
|
|
|
|
return client.fetch({
|
|
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}&page=${page}&per_page=${SHOP_PAGE_SIZE + 1}`,
|
|
})
|
|
.then(response => {
|
|
if (response.data) {
|
|
const packages = response.data.map(wcPackage => fromWCPackage(wcPackage));
|
|
dispatch(recieveShopPackages(packages, page))
|
|
}
|
|
})
|
|
.catch(error => {
|
|
client.onError(error, dispatch);
|
|
});
|
|
}
|
|
};
|
|
|
|
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);
|
|
});
|
|
}
|
|
};
|
|
|
|
const requestShops = () => ({
|
|
type: REQUEST_SHOPS
|
|
});
|
|
const receiveShops = (json) => ({
|
|
type: RECEIVE_SHOPS,
|
|
shops: json
|
|
});
|
|
|
|
const generateShopOptions = (shops) => {
|
|
shops.forEach((shop) => {
|
|
shop.value = shop.id;
|
|
shop.label = shop.name;
|
|
});
|
|
|
|
return shops;
|
|
}
|
|
|
|
export const fetchShops = (userId) => {
|
|
return dispatch => {
|
|
dispatch(requestShops());
|
|
|
|
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/customer/${userId}/shops` })
|
|
.then(response => {
|
|
|
|
if(response.data){
|
|
|
|
const shopOptions = generateShopOptions(response.data);
|
|
|
|
dispatch(receiveShops(shopOptions));
|
|
|
|
if (shopOptions.length) {
|
|
dispatch(selectShop(shopOptions[0]));
|
|
dispatch(fetchShopPackages(shopOptions[0]));
|
|
}
|
|
}
|
|
})
|
|
.catch(error => {
|
|
client.onError(error, dispatch);
|
|
});
|
|
}
|
|
}
|
|
|
|
export const selectShop = (shopInfo) => ({
|
|
type: SELECT_SHOP,
|
|
selectedShop: shopInfo
|
|
});
|