Skip to content

Instantly share code, notes, and snippets.

@maiordom
Created November 9, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiordom/1881334707f21eac4a743e2b22e71bdd to your computer and use it in GitHub Desktop.
Save maiordom/1881334707f21eac4a743e2b22e71bdd to your computer and use it in GitHub Desktop.
import { createAction } from 'src/utils/createAction';
import * as ShopService from 'src/services/Shop';
import { IAction } from 'src/types/IAction';
import { IGetWebshopUserProducts } from 'src/services/Shop';
import { IOption } from 'src/types/IOption';
import { IShopCategory } from 'src/store/ShopCategories';
import { IWebShopServices } from 'src/types/IWebShopServices';
interface ISetProductsParams {
products: IGetWebshopUserProducts;
serviceName: string;
categoryId: number;
}
interface IResetServicePaginationParams { serviceName: string; }
interface ISetPerPageCountParams { option: IOption; }
interface ISetServicePageParams { page: number; serviceName: string; }
interface ISetShopCategoryParams { category: IShopCategory; serviceName: string; }
interface ISetShopServicesParams { services: IWebShopServices; }
interface ISetSortByTypeParams { option: IOption; }
export interface IResetServicePaginationAction extends IAction<IResetServicePaginationParams> {}
export interface ISetPerPageCountAction extends IAction<ISetPerPageCountParams> {}
export interface ISetProductsAction extends IAction<ISetProductsParams> {}
export interface ISetServicePageAction extends IAction<ISetServicePageParams> {}
export interface ISetShopCategoryAction extends IAction<ISetShopCategoryParams> {}
export interface ISetShopServicesAction extends IAction<ISetShopServicesParams> {}
export interface ISetSortByTypeAction extends IAction<ISetSortByTypeParams> {}
export const {
setProducts,
resetServicePagination,
setServicePage,
setPerPageCount,
setSortByType,
setShopCategory,
setShopServices
} = {
setProducts: (params: ISetProductsParams) => createAction('setProducts', params),
resetServicePagination: (params: IResetServicePaginationParams) => createAction('resetServicePagination', params),
setServicePage: (params: ISetServicePageParams) => createAction('setServicePage', params),
setPerPageCount: (params: ISetPerPageCountParams) => createAction('setPerPageCount', params),
setSortByType: (params: ISetSortByTypeParams) => createAction('setSortByType', params),
setShopCategory: (params: ISetShopCategoryParams) => createAction('setShopCategory', params),
setShopServices: (params: ISetShopServicesParams) => createAction('setShopServices', params),
};
export const getWebshopServices = () => (dispatch) =>
ShopService.getWebshopServices()
.then((res) => {
dispatch(setShopServices({ services: res }));
});
export const getWebshopTreeCategories = (requestParams, serviceName) => (dispatch) =>
ShopService.getWebshopTreeCategories(requestParams)
.then((res) => {
dispatch(setShopCategory({ category: res, serviceName }));
return res;
});
export const getWebshopUserProducts = (categoryId?: number, serviceName?: string) => (dispatch, getState) => {
serviceName = serviceName || getState().shopMenu.selected && getState().shopMenu.selected.serviceName;
categoryId = categoryId || getState().shopService[serviceName].categoryId;
const count = getState().shop.controls.perPageCount.selected.value;
const from = getState().shopService[serviceName].pagination.from;
const userId = getState().profile.personalData.userId;
ShopService.getWebshopUserProducts({
categoryId,
count,
from,
userId,
}).then((res) => {
dispatch(setProducts({ products: res, serviceName, categoryId }));
});
};
export const setPage = (page: number) => (dispatch, getState) => {
const serviceName = getState().shopMenu.selected.serviceName;
dispatch(setServicePage({ page, serviceName }));
};
export const buyWebshopProduct = (params) => (dispatch, getState) => {
const userId = getState().profile.personalData.userId;
params.userId = userId;
return ShopService.buyWebshopProduct(params);
};
import { handleActions } from 'redux-actions';
import * as each from 'lodash/each';
import * as find from 'lodash/find';
import * as a from 'src/actions/Shop';
import {
IResetServicePaginationAction,
ISetPerPageCountAction,
ISetProductsAction,
ISetServicePageAction,
ISetShopCategoryAction,
ISetShopServicesAction,
ISetSortByTypeAction
} from 'src/actions/Shop';
import { IProductCard, IShopService, IShopServices } from 'src/store/ShopService';
import { IShopCategory } from 'src/store/ShopCategories';
import { IWebShopServices, IWebShopService } from 'src/types/IWebShopServices';
export default handleActions({
[a.setServicePage.name]: (state, { payload: { serviceName, page } }: ISetServicePageAction) =>
state.setIn(`shopService.${serviceName}.pagination`, {
from: state.getIn('shop.controls.perPageCount.selected.value') * page
}),
[a.setPerPageCount.name]: (state, { payload: { option } }: ISetPerPageCountAction) =>
state.setIn('shop.controls.perPageCount.selected', option),
[a.setSortByType.name]: (state, { payload: { option } }: ISetSortByTypeAction) =>
state.setIn('shop.controls.sortByType.selected', option),
[a.setProducts.name]: (state, { payload: { products, serviceName, categoryId } }: ISetProductsAction) => {
const { total, items } = products;
state.setIn(`shopService.${serviceName}`, { items, categoryId });
const itemsPerPage: number = state.getIn('shop.controls.perPageCount.selected.value');
const pageCount: number = Math.round(total / itemsPerPage);
state.setIn(`shopService.${serviceName}.pagination`, { pageCount });
return state;
},
[a.setShopServices.name]: (state, { payload: { services } }: ISetShopServicesAction) => {
const shopService: IShopServices = state.getIn('shopService');
services.forEach((service: IWebShopService) => {
each(shopService, (game: IShopService) => {
if (game.name === service.name) {
game.serviceId = service.id;
}
});
});
return state;
},
[a.setShopCategory.name]: (state, { payload: { category, serviceName } }: ISetShopCategoryAction) =>
state.setIn(`shopCategories.${serviceName}`, category),
[a.resetServicePagination.name]: (state, { payload: { serviceName } }: IResetServicePaginationAction) =>
state.setIn(`shopService.${serviceName}.pagination`, { from: 0 })
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment