Fixes
This commit is contained in:
@@ -2,7 +2,10 @@ import {
|
||||
RECIEVE_SHOP_PACKAGES,
|
||||
RECEIVE_SHOPS,
|
||||
SELECT_SHOP,
|
||||
REQUEST_SHOP_PACKAGES
|
||||
REQUEST_SHOP_PACKAGES,
|
||||
SHOP_PAGE_SIZE,
|
||||
SEARCH_SHOP_PACKAGES_REQUEST,
|
||||
SEARCH_SHOP_PACKAGES_RESULT,
|
||||
} from '../../constants/coMarketConstants';
|
||||
|
||||
const moduleReducers = {};
|
||||
@@ -13,9 +16,59 @@ moduleReducers[REQUEST_SHOP_PACKAGES] = (state, action) => {
|
||||
});
|
||||
};
|
||||
|
||||
moduleReducers[RECIEVE_SHOP_PACKAGES] = (state, action) => {
|
||||
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: action.shopPackages,
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user