diff --git a/src/pages/sponsors-global/form-templates/form-template-item-list-page.js b/src/pages/sponsors-global/form-templates/form-template-item-list-page.js index 95e6bcd47..e43552e5e 100644 --- a/src/pages/sponsors-global/form-templates/form-template-item-list-page.js +++ b/src/pages/sponsors-global/form-templates/form-template-item-list-page.js @@ -161,9 +161,20 @@ const FormTemplateItemListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived + (item.is_archived ? unarchiveFormTemplateItem(formTemplateId, item) - : archiveFormTemplateItem(formTemplateId, item); + : archiveFormTemplateItem(formTemplateId, item) + ).then(() => { + getFormTemplateItems( + formTemplateId, + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleShowArchivedForms = (ev) => { getFormTemplateItems( diff --git a/src/pages/sponsors-global/form-templates/form-template-list-page.js b/src/pages/sponsors-global/form-templates/form-template-list-page.js index 0d7fef187..4bd90e37d 100644 --- a/src/pages/sponsors-global/form-templates/form-template-list-page.js +++ b/src/pages/sponsors-global/form-templates/form-template-list-page.js @@ -153,7 +153,19 @@ const FormTemplateListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived ? unarchiveFormTemplate(item) : archiveFormTemplate(item); + (item.is_archived + ? unarchiveFormTemplate(item) + : archiveFormTemplate(item) + ).then(() => { + getFormTemplates( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleShowArchivedForms = (value) => { getFormTemplates( diff --git a/src/pages/sponsors-global/inventory/inventory-list-page.js b/src/pages/sponsors-global/inventory/inventory-list-page.js index b6e0d072f..8478e5989 100644 --- a/src/pages/sponsors-global/inventory/inventory-list-page.js +++ b/src/pages/sponsors-global/inventory/inventory-list-page.js @@ -262,9 +262,19 @@ const InventoryListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived + (item.is_archived ? unarchiveInventoryItem(item) - : archiveInventoryItem(item); + : archiveInventoryItem(item) + ).then(() => { + getInventoryItems( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const columns = [ { diff --git a/src/pages/sponsors-global/page-templates/page-template-list-page.js b/src/pages/sponsors-global/page-templates/page-template-list-page.js index 0b4b05d21..1da0b1718 100644 --- a/src/pages/sponsors-global/page-templates/page-template-list-page.js +++ b/src/pages/sponsors-global/page-templates/page-template-list-page.js @@ -119,9 +119,19 @@ const PageTemplateListPage = ({ }; const handleArchive = (item) => - item.is_archived + (item.is_archived ? unarchivePageTemplate(item.id) - : archivePageTemplate(item.id); + : archivePageTemplate(item.id) + ).then(() => { + getPageTemplates( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleEdit = (row) => { getPageTemplate(row.id).then(() => setOpenPageDialog(true)); diff --git a/src/pages/sponsors/show-pages-list-page/index.js b/src/pages/sponsors/show-pages-list-page/index.js index 450652e90..b694e285a 100644 --- a/src/pages/sponsors/show-pages-list-page/index.js +++ b/src/pages/sponsors/show-pages-list-page/index.js @@ -94,7 +94,12 @@ const ShowPagesListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived ? unarchiveShowPage(item.id) : archiveShowPage(item.id); + (item.is_archived + ? unarchiveShowPage(item.id) + : archiveShowPage(item.id) + ).then(() => { + getShowPages(term, currentPage, perPage, order, orderDir, showArchived); + }); const handleShowArchivedForms = (ev) => { getShowPages( diff --git a/src/pages/sponsors/sponsor-form-item-list-page/components/__test__/inventory-popup.test.js b/src/pages/sponsors/sponsor-form-item-list-page/components/__test__/inventory-popup.test.js index 427d6d0a3..f602a371c 100644 --- a/src/pages/sponsors/sponsor-form-item-list-page/components/__test__/inventory-popup.test.js +++ b/src/pages/sponsors/sponsor-form-item-list-page/components/__test__/inventory-popup.test.js @@ -104,7 +104,7 @@ describe("InventoryPopup", () => { ); const user = userEvent.setup(); - const node = screen.getByTestId("CloseIcon"); + const node = screen.getByTestId("close-dialog"); await user.click(node); expect(onClose).toHaveBeenCalledTimes(1); @@ -161,7 +161,7 @@ describe("InventoryPopup", () => { const textNode = await screen.findByText("1 items selected"); expect(textNode.textContent).toBe("1 items selected"); - const node = screen.getByTestId("CloseIcon"); + const node = screen.getByTestId("close-dialog"); await user.click(node); const textNode2 = screen.getByText("0 items selected"); diff --git a/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-add-item-from-inventory-popup.js b/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-add-item-from-inventory-popup.js index 0b3d89101..f12ca9633 100644 --- a/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-add-item-from-inventory-popup.js +++ b/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-add-item-from-inventory-popup.js @@ -175,7 +175,12 @@ const SponsorFormAddItemFromInventoryPopup = ({ {T.translate("sponsor_form_item_list.add_from_inventory.title")} - + diff --git a/src/pages/sponsors/sponsor-form-item-list-page/index.js b/src/pages/sponsors/sponsor-form-item-list-page/index.js index b8b2822a0..1a0aeb1e2 100644 --- a/src/pages/sponsors/sponsor-form-item-list-page/index.js +++ b/src/pages/sponsors/sponsor-form-item-list-page/index.js @@ -110,9 +110,19 @@ const SponsorFormItemListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived + (item.is_archived ? unarchiveSponsorFormItem(formId, item.id) - : archiveSponsorFormItem(formId, item.id); + : archiveSponsorFormItem(formId, item.id) + ).then(() => { + getSponsorFormItems( + formId, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleRowDelete = (itemId) => { deleteSponsorFormItem(formId, itemId).then(() => { diff --git a/src/pages/sponsors/sponsor-forms-list-page/index.js b/src/pages/sponsors/sponsor-forms-list-page/index.js index ef6fa7471..a7740253f 100644 --- a/src/pages/sponsors/sponsor-forms-list-page/index.js +++ b/src/pages/sponsors/sponsor-forms-list-page/index.js @@ -118,9 +118,19 @@ const SponsorFormsListPage = ({ }; const handleArchiveItem = (item) => - item.is_archived + (item.is_archived ? unarchiveSponsorForm(item.id) - : archiveSponsorForm(item.id); + : archiveSponsorForm(item.id) + ).then(() => { + getSponsorForms( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleShowArchivedForms = (ev) => { getSponsorForms( diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/manage-items/sponsor-forms-manage-items.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/manage-items/sponsor-forms-manage-items.js index 5ccaca2fb..382ad9c7d 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/manage-items/sponsor-forms-manage-items.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/manage-items/sponsor-forms-manage-items.js @@ -151,9 +151,20 @@ const SponsorFormsManageItems = ({ }; const handleArchiveItem = (item) => - item.is_archived + (item.is_archived ? unarchiveSponsorCustomizedFormItem(formId, item.id) - : archiveSponsorCustomizedFormItem(formId, item.id); + : archiveSponsorCustomizedFormItem(formId, item.id) + ).then(() => { + getSponsorCustomizedFormItems( + formId, + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleShowArchivedItems = (ev) => { getSponsorCustomizedFormItems( diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/index.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/index.js index 044424ff9..c95e0e4c8 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/index.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/index.js @@ -146,9 +146,20 @@ const SponsorFormsTab = ({ }; const handleArchiveForm = (item) => - item.is_archived + (item.is_archived ? unarchiveSponsorCustomizedForm(item.id) - : archiveSponsorCustomizedForm(item.id); + : archiveSponsorCustomizedForm(item.id) + ).then(() => { + const { perPage, order, orderDir, currentPage } = customizedForms; + getSponsorCustomizedForms( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ); + }); const handleCustomizedFormManageItems = (item) => { history.push( diff --git a/src/reducers/sponsors/show-pages-list-reducer.js b/src/reducers/sponsors/show-pages-list-reducer.js index 9b69913f3..00b4186a0 100644 --- a/src/reducers/sponsors/show-pages-list-reducer.js +++ b/src/reducers/sponsors/show-pages-list-reducer.js @@ -27,6 +27,7 @@ import { RECEIVE_SUMMIT_SPONSORSHIP_TYPES } from "../../actions/summit-actions"; import { denormalizePageModules } from "../../utils/page-template"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_SHOW_PAGE = { code: "", @@ -109,22 +110,26 @@ const showPagesListReducer = (state = DEFAULT_STATE, action) => { } case SHOW_PAGE_ARCHIVED: { const { pageId } = payload; + const { totalCount, perPage, currentPage } = state; const pages = state.showPages.map((page) => page.id === pageId ? { ...page, is_archived: true } : page ); return { ...state, - showPages: [...pages] + showPages: [...pages], + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) }; } case SHOW_PAGE_UNARCHIVED: { const { pageId } = payload; + const { totalCount, perPage, currentPage } = state; const pages = state.showPages.map((page) => page.id === pageId ? { ...page, is_archived: false } : page ); return { ...state, - showPages: [...pages] + showPages: [...pages], + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) }; } case RECEIVE_SHOW_PAGE: { diff --git a/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js b/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js index a2de1b315..84c58770f 100644 --- a/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js +++ b/src/reducers/sponsors/sponsor-customized-form-items-list-reducer.js @@ -25,6 +25,7 @@ import { RESET_SPONSOR_FORM_MANAGED_ITEM } from "../../actions/sponsor-forms-actions"; import { SET_CURRENT_SUMMIT } from "../../actions/summit-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_ITEM_ENTITY = { code: "", @@ -126,21 +127,31 @@ const sponsorCustomizedFormItemsListReducer = ( } case SPONSOR_CUSTOMIZED_FORM_ITEM_ARCHIVED: { const { id: itemId } = payload.response; + const { totalCount, currentPage, perPage } = state; const items = state.items.map((item) => item.id === itemId ? { ...item, is_archived: true } : item ); - return { ...state, items }; + return { + ...state, + items, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } case SPONSOR_CUSTOMIZED_FORM_ITEM_UNARCHIVED: { const { itemId } = payload; + const { totalCount, currentPage, perPage } = state; const items = state.items.map((item) => item.id === itemId ? { ...item, is_archived: false } : item ); - return { ...state, items }; + return { + ...state, + items, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } case SPONSOR_FORM_MANAGED_ITEM_UPDATED: { const updatedItem = payload.response; diff --git a/src/reducers/sponsors/sponsor-form-items-list-reducer.js b/src/reducers/sponsors/sponsor-form-items-list-reducer.js index 318ea185f..21e0c7597 100644 --- a/src/reducers/sponsors/sponsor-form-items-list-reducer.js +++ b/src/reducers/sponsors/sponsor-form-items-list-reducer.js @@ -23,6 +23,7 @@ import { SPONSOR_FORM_ITEM_UNARCHIVED } from "../../actions/sponsor-forms-actions"; import { SET_CURRENT_SUMMIT } from "../../actions/summit-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_STATE = { items: [], @@ -117,21 +118,31 @@ const sponsorFormItemsListReducer = (state = DEFAULT_STATE, action) => { } case SPONSOR_FORM_ITEM_ARCHIVED: { const { id: itemId } = payload.response; + const { totalCount, perPage, currentPage } = state; const items = state.items.map((item) => item.id === itemId ? { ...item, is_archived: true } : item ); - return { ...state, items }; + return { + ...state, + items, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } case SPONSOR_FORM_ITEM_UNARCHIVED: { const { itemId } = payload; + const { totalCount, perPage, currentPage } = state; const items = state.items.map((item) => item.id === itemId ? { ...item, is_archived: false } : item ); - return { ...state, items }; + return { + ...state, + items, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } default: return state; diff --git a/src/reducers/sponsors/sponsor-forms-list-reducer.js b/src/reducers/sponsors/sponsor-forms-list-reducer.js index de8688891..ee3213724 100644 --- a/src/reducers/sponsors/sponsor-forms-list-reducer.js +++ b/src/reducers/sponsors/sponsor-forms-list-reducer.js @@ -28,6 +28,7 @@ import { SET_CURRENT_SUMMIT, RECEIVE_SUMMIT_SPONSORSHIP_TYPES } from "../../actions/summit-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; export const DEFAULT_STATE = { sponsorForms: [], @@ -152,21 +153,31 @@ const sponsorFormsListReducer = (state = DEFAULT_STATE, action) => { } case SPONSOR_FORM_ARCHIVED: { const { id: formId } = payload.response; + const { totalCount, perPage, currentPage } = state; const sponsorForms = state.sponsorForms.map((item) => item.id === formId ? { ...item, is_archived: true } : item ); - return { ...state, sponsorForms }; + return { + ...state, + sponsorForms, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } case SPONSOR_FORM_UNARCHIVED: { const { formId } = payload; + const { totalCount, perPage, currentPage } = state; const sponsorForms = state.sponsorForms.map((item) => item.id === formId ? { ...item, is_archived: false } : item ); - return { ...state, sponsorForms }; + return { + ...state, + sponsorForms, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) + }; } case SPONSOR_FORM_DELETED: { const { formId } = payload; diff --git a/src/reducers/sponsors/sponsor-page-forms-list-reducer.js b/src/reducers/sponsors/sponsor-page-forms-list-reducer.js index 2f5c4b6da..6853b69cf 100644 --- a/src/reducers/sponsors/sponsor-page-forms-list-reducer.js +++ b/src/reducers/sponsors/sponsor-page-forms-list-reducer.js @@ -24,6 +24,7 @@ import { SPONSOR_CUSTOMIZED_FORM_UPDATED } from "../../actions/sponsor-forms-actions"; import { SET_CURRENT_SUMMIT } from "../../actions/summit-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; export const DEFAULT_STATE = { managedForms: { @@ -233,6 +234,10 @@ const sponsorPageFormsListReducer = (state = DEFAULT_STATE, action) => { } case SPONSOR_CUSTOMIZED_FORM_ARCHIVED_CHANGED: { const { formId, isArchived } = payload; + const { + customizedForms: { totalCount, currentPage, perPage } + } = state; + const forms = state.customizedForms.forms.map((form) => { if (form.id === formId) { return { @@ -247,7 +252,8 @@ const sponsorPageFormsListReducer = (state = DEFAULT_STATE, action) => { ...state, customizedForms: { ...state.customizedForms, - forms + forms, + currentPage: getSafePageAfterRemove(totalCount, perPage, currentPage) } }; } diff --git a/src/reducers/sponsors/sponsor-page-pages-list-reducer.js b/src/reducers/sponsors/sponsor-page-pages-list-reducer.js index e30e9d436..2a13020e3 100644 --- a/src/reducers/sponsors/sponsor-page-pages-list-reducer.js +++ b/src/reducers/sponsors/sponsor-page-pages-list-reducer.js @@ -29,6 +29,7 @@ import { } from "../../actions/summit-actions"; import { PAGES_MODULE_KINDS } from "../../utils/constants"; import { denormalizePageModules } from "../../utils/page-template"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_PAGE = { code: "", @@ -198,6 +199,9 @@ const sponsorPagePagesListReducer = (state = DEFAULT_STATE, action) => { } case SPONSOR_CUSTOMIZED_PAGE_ARCHIVED: { const { pageId } = payload; + const { + customizedPages: { totalItems, currentPage, perPage } + } = state; const pages = state.customizedPages.pages.map((page) => page.id === pageId ? { ...page, is_archived: true } : page ); @@ -205,12 +209,16 @@ const sponsorPagePagesListReducer = (state = DEFAULT_STATE, action) => { ...state, customizedPages: { ...state.customizedPages, - pages: [...pages] + pages: [...pages], + currentPage: getSafePageAfterRemove(totalItems, perPage, currentPage) } }; } case SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED: { const { pageId } = payload; + const { + customizedPages: { totalItems, currentPage, perPage } + } = state; const pages = state.customizedPages.pages.map((page) => page.id === pageId ? { ...page, is_archived: false } : page ); @@ -218,7 +226,8 @@ const sponsorPagePagesListReducer = (state = DEFAULT_STATE, action) => { ...state, customizedPages: { ...state.customizedPages, - pages: [...pages] + pages: [...pages], + currentPage: getSafePageAfterRemove(totalItems, perPage, currentPage) } }; } diff --git a/src/reducers/sponsors_inventory/form-template-item-list-reducer.js b/src/reducers/sponsors_inventory/form-template-item-list-reducer.js index aa9cb2570..d040b7a6b 100644 --- a/src/reducers/sponsors_inventory/form-template-item-list-reducer.js +++ b/src/reducers/sponsors_inventory/form-template-item-list-reducer.js @@ -20,6 +20,7 @@ import { FORM_TEMPLATE_ITEM_ARCHIVED, FORM_TEMPLATE_ITEM_UNARCHIVED } from "../../actions/form-template-item-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_STATE = { formTemplateItems: [], @@ -89,10 +90,16 @@ const formTemplateItemListReducer = (state = DEFAULT_STATE, action = {}) => { } case FORM_TEMPLATE_ITEM_DELETED: { const { formTemplateItemId } = payload; + const { totalFormTemplateItems, perPage, currentPage } = state; return { ...state, formTemplateItems: state.formTemplateItems.filter( (a) => a.id !== formTemplateItemId + ), + currentPage: getSafePageAfterRemove( + totalFormTemplateItems, + perPage, + currentPage ) }; } @@ -102,23 +109,39 @@ const formTemplateItemListReducer = (state = DEFAULT_STATE, action = {}) => { } case FORM_TEMPLATE_ITEM_ARCHIVED: { const updatedFormTemplateItem = payload.response; - + const { totalFormTemplateItems, perPage, currentPage } = state; const updatedFormTemplatesItems = state.formTemplateItems.map((item) => item.id === updatedFormTemplateItem.id ? { ...item, is_archived: true } : item ); - return { ...state, formTemplateItems: updatedFormTemplatesItems }; + return { + ...state, + formTemplateItems: updatedFormTemplatesItems, + currentPage: getSafePageAfterRemove( + totalFormTemplateItems, + perPage, + currentPage + ) + }; } case FORM_TEMPLATE_ITEM_UNARCHIVED: { const updatedFormTemplateItemId = payload; - + const { totalFormTemplateItems, perPage, currentPage } = state; const updatedFormTemplatesItems = state.formTemplateItems.map((item) => item.id === updatedFormTemplateItemId ? { ...item, is_archived: false } : item ); - return { ...state, formTemplateItems: updatedFormTemplatesItems }; + return { + ...state, + formTemplateItems: updatedFormTemplatesItems, + currentPage: getSafePageAfterRemove( + totalFormTemplateItems, + perPage, + currentPage + ) + }; } default: return state; diff --git a/src/reducers/sponsors_inventory/form-template-list-reducer.js b/src/reducers/sponsors_inventory/form-template-list-reducer.js index 8406d3b3c..ea2cf493e 100644 --- a/src/reducers/sponsors_inventory/form-template-list-reducer.js +++ b/src/reducers/sponsors_inventory/form-template-list-reducer.js @@ -20,6 +20,7 @@ import { FORM_TEMPLATE_ARCHIVED, FORM_TEMPLATE_UNARCHIVED } from "../../actions/form-template-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_STATE = { formTemplates: [], @@ -99,23 +100,41 @@ const formTemplateListReducer = (state = DEFAULT_STATE, action = {}) => { } case FORM_TEMPLATE_ARCHIVED: { const updatedFormTemplate = payload.response; + const { totalFormTemplates, currentPage, perPage } = state; const updatedFormTemplates = state.formTemplates.map((item) => item.id === updatedFormTemplate.id ? { ...item, is_archived: true } : item ); - return { ...state, formTemplates: updatedFormTemplates }; + return { + ...state, + formTemplates: updatedFormTemplates, + currentPage: getSafePageAfterRemove( + totalFormTemplates, + perPage, + currentPage + ) + }; } case FORM_TEMPLATE_UNARCHIVED: { const updatedFormTemplateId = payload; + const { totalFormTemplates, currentPage, perPage } = state; const updatedFormTemplates = state.formTemplates.map((item) => item.id === updatedFormTemplateId ? { ...item, is_archived: false } : item ); - return { ...state, formTemplates: updatedFormTemplates }; + return { + ...state, + formTemplates: updatedFormTemplates, + currentPage: getSafePageAfterRemove( + totalFormTemplates, + perPage, + currentPage + ) + }; } case CHANGE_FORM_TEMPLATE_SEARCH_TERM: { const { term } = payload; diff --git a/src/reducers/sponsors_inventory/inventory-item-list-reducer.js b/src/reducers/sponsors_inventory/inventory-item-list-reducer.js index 6da37a276..955ea04bb 100644 --- a/src/reducers/sponsors_inventory/inventory-item-list-reducer.js +++ b/src/reducers/sponsors_inventory/inventory-item-list-reducer.js @@ -25,6 +25,7 @@ import { INVENTORY_ITEM_UNARCHIVED, INVENTORY_ITEM_IMAGE_SAVED } from "../../actions/inventory-item-actions"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_STATE = { inventoryItems: [], @@ -102,10 +103,16 @@ const inventoryItemListReducer = (state = DEFAULT_STATE, action = {}) => { } case INVENTORY_ITEM_DELETED: { const { inventoryItemId } = payload; + const { totalInventoryItems, perPage, currentPage } = state; return { ...state, inventoryItems: state.inventoryItems.filter( (a) => a.id !== inventoryItemId + ), + currentPage: getSafePageAfterRemove( + totalInventoryItems, + perPage, + currentPage ) }; } @@ -199,19 +206,35 @@ const inventoryItemListReducer = (state = DEFAULT_STATE, action = {}) => { } case INVENTORY_ITEM_ARCHIVED: { const updatedItem = payload.response; - + const { totalInventoryItems, perPage, currentPage } = state; const updatedInventoryItems = state.inventoryItems.map((item) => item.id === updatedItem.id ? { ...item, is_archived: true } : item ); - return { ...state, inventoryItems: updatedInventoryItems }; + return { + ...state, + inventoryItems: updatedInventoryItems, + currentPage: getSafePageAfterRemove( + totalInventoryItems, + perPage, + currentPage + ) + }; } case INVENTORY_ITEM_UNARCHIVED: { const updatedItemId = payload; - + const { totalInventoryItems, perPage, currentPage } = state; const updatedInventoryItems = state.inventoryItems.map((item) => item.id === updatedItemId ? { ...item, is_archived: false } : item ); - return { ...state, inventoryItems: updatedInventoryItems }; + return { + ...state, + inventoryItems: updatedInventoryItems, + currentPage: getSafePageAfterRemove( + totalInventoryItems, + perPage, + currentPage + ) + }; } case INVENTORY_ITEM_IMAGE_SAVED: { const newImage = payload.response; diff --git a/src/reducers/sponsors_inventory/page-template-list-reducer.js b/src/reducers/sponsors_inventory/page-template-list-reducer.js index c7d955d45..ebefd76a7 100644 --- a/src/reducers/sponsors_inventory/page-template-list-reducer.js +++ b/src/reducers/sponsors_inventory/page-template-list-reducer.js @@ -20,6 +20,7 @@ import { PAGE_TEMPLATE_UNARCHIVED } from "../../actions/page-template-actions"; import { PAGES_MODULE_KINDS } from "../../utils/constants"; +import { getSafePageAfterRemove } from "../../utils/methods"; const DEFAULT_STATE = { pageTemplates: [], @@ -97,30 +98,52 @@ const pageTemplateListReducer = (state = DEFAULT_STATE, action = {}) => { } case PAGE_TEMPLATE_DELETED: { const { pageTemplateId } = payload; + const { totalPageTemplates, perPage, currentPage } = state; return { ...state, pageTemplates: state.pageTemplates.filter( (a) => a.id !== pageTemplateId + ), + currentPage: getSafePageAfterRemove( + totalPageTemplates, + perPage, + currentPage ) }; } case PAGE_TEMPLATE_ARCHIVED: { const updatedFormTemplate = payload.response; - + const { totalPageTemplates, perPage, currentPage } = state; const updatedPageTemplates = state.pageTemplates.map((item) => item.id === updatedFormTemplate.id ? { ...item, is_archived: true } : item ); - return { ...state, pageTemplates: updatedPageTemplates }; + return { + ...state, + pageTemplates: updatedPageTemplates, + currentPage: getSafePageAfterRemove( + totalPageTemplates, + perPage, + currentPage + ) + }; } case PAGE_TEMPLATE_UNARCHIVED: { const { pageTemplateId } = payload; - + const { totalPageTemplates, perPage, currentPage } = state; const updatedPageTemplates = state.pageTemplates.map((item) => item.id === pageTemplateId ? { ...item, is_archived: false } : item ); - return { ...state, pageTemplates: updatedPageTemplates }; + return { + ...state, + pageTemplates: updatedPageTemplates, + currentPage: getSafePageAfterRemove( + totalPageTemplates, + perPage, + currentPage + ) + }; } default: return state; diff --git a/src/utils/__tests__/methods.test.js b/src/utils/__tests__/methods.test.js index ac5275c43..edc7c7385 100644 --- a/src/utils/__tests__/methods.test.js +++ b/src/utils/__tests__/methods.test.js @@ -1,4 +1,8 @@ -import { getMediaInputValue, normalizeSelectAllField } from "../methods"; +import { + getMediaInputValue, + normalizeSelectAllField, + getSafePageAfterRemove +} from "../methods"; const FIXED_NOW = 1_772_551_911_231; beforeAll(() => jest.spyOn(Date, "now").mockReturnValue(FIXED_NOW)); @@ -55,69 +59,95 @@ describe("getMediaInputValue", () => { expect(result.filename).toBe("README"); }); }); +}); - describe("normalizeSelectAllField", () => { - it("should return default object when items is empty array", () => { - expect(normalizeSelectAllField([], "apply_to_all", "items")).toEqual({ - apply_to_all: false, - items: [] - }); +describe("normalizeSelectAllField", () => { + it("should return default object when items is empty array", () => { + expect(normalizeSelectAllField([], "apply_to_all", "items")).toEqual({ + apply_to_all: false, + items: [] }); + }); - it("should return all selected when array contains 'all'", () => { - expect( - normalizeSelectAllField( - ["all", { id: 1 }, { id: 2 }], - "apply_to_all", - "items" - ) - ).toEqual({ - apply_to_all: true, - items: [] - }); + it("should return all selected when array contains 'all'", () => { + expect( + normalizeSelectAllField( + ["all", { id: 1 }, { id: 2 }], + "apply_to_all", + "items" + ) + ).toEqual({ + apply_to_all: true, + items: [] }); + }); - it("should return all selected when allSelected flag is true", () => { - expect( - normalizeSelectAllField([{ id: 1 }], "apply_to_all", "items", true) - ).toEqual({ - apply_to_all: true, - items: [] - }); + it("should return all selected when allSelected flag is true", () => { + expect( + normalizeSelectAllField([{ id: 1 }], "apply_to_all", "items", true) + ).toEqual({ + apply_to_all: true, + items: [] }); + }); - it.each([[], null, undefined])( - "should return apply_to_all true when allSelected is true and items is %s", - (items) => { - expect( - normalizeSelectAllField(items, "apply_to_all", "items", true) - ).toEqual({ apply_to_all: true, items: [] }); - } - ); - - it.each([[], null, undefined])( - "should return apply_to_all false when allSelected is false and items is %s", - (items) => { - expect( - normalizeSelectAllField(items, "apply_to_all", "items", false) - ).toEqual({ apply_to_all: false, items: [] }); - } - ); - - it("should return array of ids when items are objects with id", () => { + it.each([[], null, undefined])( + "should return apply_to_all true when allSelected is true and items is %s", + (items) => { expect( - normalizeSelectAllField([{ id: 1 }, { id: 2 }], "apply_to_all", "items") - ).toEqual({ - apply_to_all: false, - items: [1, 2] - }); + normalizeSelectAllField(items, "apply_to_all", "items", true) + ).toEqual({ apply_to_all: true, items: [] }); + } + ); + + it.each([[], null, undefined])( + "should return apply_to_all false when allSelected is false and items is %s", + (items) => { + expect( + normalizeSelectAllField(items, "apply_to_all", "items", false) + ).toEqual({ apply_to_all: false, items: [] }); + } + ); + + it("should return array of ids when items are objects with id", () => { + expect( + normalizeSelectAllField([{ id: 1 }, { id: 2 }], "apply_to_all", "items") + ).toEqual({ + apply_to_all: false, + items: [1, 2] }); + }); - it("should return an array of values directly when items are primitives", () => { - expect(normalizeSelectAllField([1, 2], "apply_to_all", "items")).toEqual({ - apply_to_all: false, - items: [1, 2] - }); + it("should return an array of values directly when items are primitives", () => { + expect(normalizeSelectAllField([1, 2], "apply_to_all", "items")).toEqual({ + apply_to_all: false, + items: [1, 2] }); }); }); + +describe("getSafePageAfterRemove", () => { + it("should stay on page 1 when there is only one page", () => { + expect(getSafePageAfterRemove(10, 10, 1)).toBe(1); + }); + + it("should go back to page 1 when removing the last item on page 2", () => { + expect(getSafePageAfterRemove(11, 10, 2)).toBe(1); + }); + + it("should stay on page 2 when it still has items after removal", () => { + expect(getSafePageAfterRemove(12, 10, 2)).toBe(2); + }); + + it("should go back one page when the removal empties the last page", () => { + expect(getSafePageAfterRemove(21, 10, 3)).toBe(2); + }); + + it("should never return a page lower than 1", () => { + expect(getSafePageAfterRemove(1, 10, 1)).toBe(1); + }); + + it("should stay on current page when removal does not reduce page count", () => { + expect(getSafePageAfterRemove(20, 10, 2)).toBe(2); + }); +}); diff --git a/src/utils/methods.js b/src/utils/methods.js index dd426a3dd..418a1e9d3 100644 --- a/src/utils/methods.js +++ b/src/utils/methods.js @@ -636,3 +636,8 @@ export const formatDate = ( export const getFileUploadAllowedExtensions = () => window.FILE_UPLOAD_ALLOWED_EXTENSIONS?.split(",").filter(Boolean) ?? []; + +export const getSafePageAfterRemove = (totalCount, perPage, currentPage) => { + const pageCountDecreases = Number.isInteger((totalCount - 1) / perPage); + return pageCountDecreases && currentPage > 1 ? currentPage - 1 : currentPage; +};