Skip to content

Instantly share code, notes, and snippets.

@maiordom
Created February 24, 2018 16:12
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/fe2596c6e121200b332c3871eabb54b1 to your computer and use it in GitHub Desktop.
Save maiordom/fe2596c6e121200b332c3871eabb54b1 to your computer and use it in GitHub Desktop.
import * as isEmpty from 'lodash/isEmpty';
import { createAction } from '4game-core/utils/createAction';
import * as ShopService from 'src/services/Shop';
import { IAction } from '4game-core/types/IAction';
import { IOption } from '4game-core/types/IOption';
import { IProductCard } from '4game-core/store/ShopService';
import { IGetWebshopUserProducts } from 'src/services/Shop';
import { IShopCategory } from 'src/store/ShopCategories';
import { IWebShopServices } from 'src/types/IWebShopServices';
import { setOrderResult, openBilling } from 'src/actions/ShopWindow';
interface ISetProductsParams {
products: IGetWebshopUserProducts;
serviceName: string;
categoryId: number;
}
export interface IResetServicePaginationParams { serviceName: string; }
export interface ISetPerPageCountParams { option: IOption; }
export interface ISetServicePageParams { page: number; serviceName: string; }
export interface ISetShopCategoryParams { category: IShopCategory; serviceName: string; }
export interface ISetShopServicesParams { services: IWebShopServices; }
export interface ISetSortByTypeParams { option: IOption; }
export interface ISetSinglesParams { items: Array<IProductCard>; }
export interface ISetCertificatesParams { certificates: Array<IProductCard>; }
import { IBuyWebshopProductRequestParams } from 'src/types/requests/IBuyWebshopProductRequestParams';
import { IOrderResult } from '../store/ShopWindow';
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 ISetSinglesAction extends IAction<ISetSinglesParams> {}
export interface ISetSortByTypeAction extends IAction<ISetSortByTypeParams> {}
export interface ISetCertificatesAction extends IAction<ISetCertificatesParams> {}
export const {
resetServicePagination,
setPerPageCount,
setProducts,
setServicePage,
setShopCategory,
setShopServices,
setSingles,
setSortByType,
setCertificates
} = {
resetServicePagination: (params: IResetServicePaginationParams) => createAction('resetServicePagination', params),
setPerPageCount: (params: ISetPerPageCountParams) => createAction('setPerPageCount', params),
setProducts: (params: ISetProductsParams) => createAction('setProducts', params),
setServicePage: (params: ISetServicePageParams) => createAction('setServicePage', params),
setShopCategory: (params: ISetShopCategoryParams) => createAction('setShopCategory', params),
setShopServices: (params: ISetShopServicesParams) => createAction('setShopServices', params),
setSingles: (params: ISetSinglesParams) => createAction('setSingles', params),
setSortByType: (params: ISetSortByTypeParams) => createAction('setSortByType', params),
setCertificates: (params: ISetCertificatesParams) => createAction('setCertificates', 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 getUserCertificates = () => (dispatch, getState) => {
const userId = getState().profile.personalData.userId;
const { rawShopServices } = getState();
if (isEmpty(rawShopServices)) {
return Promise.reject(null);
}
const forgameRuServiceId = rawShopServices.forgameRu && rawShopServices.forgameRu.id;
return ShopService.getWebshopUserProducts({
productType: 'certificate',
serviceId: forgameRuServiceId,
userId
}).then((res) => {
dispatch(setCertificates({ certificates: res.items }));
});
};
export const getSingles = () => (dispatch, getState) => {
const userId = getState().profile.personalData.userId;
ShopService.getWebshopUserProducts({
productType: 'singleGame',
userId
}).then(({ items }) => {
dispatch(setSingles({items}));
});
};
export const setPage = (page: number) => (dispatch, getState) => {
const serviceName = getState().shopMenu.selected.serviceName;
dispatch(setServicePage({ page, serviceName }));
};
export const buyWebshopProduct = (params: IBuyWebshopProductRequestParams) => (dispatch, getState) => {
params.userId = getState().profile.personalData.userId;
return ShopService.buyWebshopProduct(params)
.then((data: IOrderResult) => {
dispatch(setOrderResult({ id: params.id, data }));
})
.catch((exx) => {
if (exx.code === 'webshop.needbuythrough') {
dispatch(openBilling({
id: params.id,
orderId: exx.data.orderId,
productCost: params.amount,
signature: exx.data.signature,
token: exx.data.token,
type: params.type,
underfundedSum: exx.data.underfundedSum
}));
}
});
};
import * as compact from 'lodash/compact';
import { ITransport } from '4game-core/types/ITransport';
import routes from '4game-core/routes/api';
import prettifyNumber from '4game-core/utils/prettifyNumber';
import { IGetPageWidgetsRequestParams } from 'src/types/requests/IContentRequestsParams';
import {
IGetPageWidgetsResponseType,
IWidgetBannerResponseType,
IWidgetCollectionResponseType,
IWidgetProductResponseType,
IWidgetProductsCollectionResponseType,
IWidgetSingleResponseType,
IWidgetSocialResponseType,
IWidgetVideoResponseType
} from 'src/types/responses/IContentReponses';
import {
IContentPage,
IWidgetBanner,
IWidgetCollection,
IWidgetProduct,
IWidgetProductsCollection,
IWidgetSingle,
IWidgetSocial,
IWidgetVideo
} from '4game-core/store/Content';
let transport: ITransport = {
call: (routeObject, params) => Promise.resolve()
};
export const setTransport = (transportInstance: ITransport) => {
transport = transportInstance;
};
const getCollection = (item: IWidgetCollectionResponseType) => ({
id: item.id,
display: item.display,
size: {
width: item.size.width
},
type: 'collection',
widgets: compact(item.widgets.map((widget) => {
switch (widget.data.type) {
case 'productsCollection':
return getProductCollection(widget as IWidgetProductsCollectionResponseType);
case 'banner':
return getBanner(widget as IWidgetBannerResponseType);
case 'video':
return getVideo(widget as IWidgetVideoResponseType);
}
}))
} as IWidgetCollection);
const getProductCollection = (widget: IWidgetProductsCollectionResponseType) => ({
id: widget.id,
type: 'productsCollection',
source: {
serviceId: widget.data.source.serviceId,
categoryId: widget.data.source.categoryId,
sorting: widget.data.source.sorting,
sortingDirection: widget.data.source.sortingDirection
}
} as IWidgetProductsCollection);
const getBanner = (widget: IWidgetBannerResponseType) => ({
id: widget.id,
type: 'banner',
size: {
width: widget.size.width
},
source: {
data: widget.data.data,
label: widget.data.label,
target: widget.data.target,
title: widget.data.title
}
} as IWidgetBanner);
const getVideo = (widget: IWidgetVideoResponseType) => ({
id: widget.id,
type: 'video',
size: {
width: widget.size.width
},
source: {
link: widget.data.link,
source: 'youtube',
type: 'video'
}
} as IWidgetVideo);
const getSocial = (widget: IWidgetSocialResponseType) => ({
id : widget.id,
type: 'social',
size: {
width: widget.size.width
},
source: widget.data.list.map((social) => ({
id: social.id,
followers: prettifyNumber(social.followers),
type: social.type
}))
} as IWidgetSocial);
const getProduct = (widget: IWidgetProductResponseType) => ({
id: widget.id,
productId: widget.data.productId,
type: 'product',
size: {
width: widget.size.width
}
} as IWidgetProduct);
const getSingle = (widget: IWidgetSingleResponseType) => {
switch (widget.data.type) {
case 'product':
return getProduct(widget as IWidgetProductResponseType);
case 'video':
return getVideo(widget as IWidgetVideoResponseType);
case 'banner':
return getBanner(widget as IWidgetBannerResponseType);
case 'social':
return getSocial(widget as IWidgetSocialResponseType);
}
};
export const getPageWidgets = (
params: IGetPageWidgetsRequestParams
): Promise<IContentPage> =>
transport.call(routes.content.getPageWidgets, params)
.then((res: IGetPageWidgetsResponseType) => ({
items: res.items.map((item) => {
if (item.type === 'single') {
return getSingle(item);
}
return getCollection(item);
})
} as IContentPage));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment