94 lines
3.0 KiB
JavaScript
94 lines
3.0 KiB
JavaScript
import {
|
|
RECIEVE_SHOP_PACKAGES,
|
|
RECEIVE_SHOPS,
|
|
SELECT_SHOP,
|
|
REQUEST_SHOP_PACKAGES,
|
|
SHOP_PAGE_SIZE,
|
|
SEARCH_SHOP_PACKAGES_REQUEST,
|
|
SEARCH_SHOP_PACKAGES_RESULT,
|
|
} from '../../constants/coMarketConstants';
|
|
|
|
const moduleReducers = {};
|
|
|
|
moduleReducers[REQUEST_SHOP_PACKAGES] = (state, action) => {
|
|
return Object.assign({}, state, {
|
|
isLoading: action.isLoading
|
|
});
|
|
};
|
|
|
|
moduleReducers[RECIEVE_SHOP_PACKAGES] = (state = {}, action) => {
|
|
|
|
// implement paging
|
|
// paging is implemented in a way that with every request one more package is requested on top of
|
|
// page size number
|
|
// this means that if retrieved number of packages is greater than page size there may be more packages
|
|
|
|
const shopPage = action.page || 1;
|
|
let shopPackages = [];
|
|
let shopPackagesDiff = [];
|
|
let retrievedShopPackages = action.shopPackages || [];
|
|
|
|
// append newly retrieved packages to existing ones if more packages are loaded
|
|
if (shopPage > state.shopPage) {
|
|
shopPackages = state.loadedShopPackages || [];
|
|
|
|
// get ignored packages from previous request
|
|
const oldShopPackagesDiff = state.shopPackagesDiff || [];
|
|
// append packages ignored previous time to the beginning
|
|
retrievedShopPackages = oldShopPackagesDiff.concat(retrievedShopPackages);
|
|
}
|
|
|
|
// if number of packages is greater than page size there may be more of them to retrieve
|
|
const hasMorePages = retrievedShopPackages.length > SHOP_PAGE_SIZE;
|
|
|
|
// ignore all packages over limit of page size (they will be displayed at the beginning of the next request)
|
|
while (retrievedShopPackages.length > SHOP_PAGE_SIZE) {
|
|
shopPackagesDiff.push(retrievedShopPackages.pop());
|
|
}
|
|
// append packages from this page to existing ones
|
|
shopPackages = shopPackages.concat(retrievedShopPackages);
|
|
|
|
return Object.assign({}, state, {
|
|
shopPackages: shopPackages,
|
|
loadedShopPackages: shopPackages,
|
|
shopPackagesDiff: shopPackagesDiff,
|
|
shopPage: shopPage,
|
|
shopSearch: false,
|
|
shopHasMorePackages: hasMorePages,
|
|
isLoading: action.isLoading
|
|
});
|
|
};
|
|
|
|
moduleReducers[SEARCH_SHOP_PACKAGES_REQUEST] = (state, action) => {
|
|
return Object.assign({}, state, {
|
|
isLoading: action.isLoading
|
|
});
|
|
};
|
|
|
|
moduleReducers[SEARCH_SHOP_PACKAGES_RESULT] = (state, action) => {
|
|
return Object.assign({}, state, {
|
|
shopPackages: action.shopPackages || [],
|
|
shopSearch: true,
|
|
isLoading: action.isLoading
|
|
});
|
|
};
|
|
|
|
moduleReducers[RECEIVE_SHOPS] = (state, action) => {
|
|
return Object.assign({}, state, {
|
|
shops: action.shops
|
|
});
|
|
};
|
|
|
|
moduleReducers[SELECT_SHOP] = (state, action) => {
|
|
return Object.assign({}, state, {
|
|
selectedShop: action.selectedShop
|
|
});
|
|
};
|
|
|
|
const coMarketPackagesReducer = (state = {}, action) => {
|
|
|
|
return moduleReducers[action.type] ? moduleReducers[action.type](state, action) : state;
|
|
};
|
|
|
|
export default coMarketPackagesReducer;
|