diff --git a/src/actions/sponsor-pages-actions.js b/src/actions/sponsor-pages-actions.js index 4a3222b4e..7a6a5f2c6 100644 --- a/src/actions/sponsor-pages-actions.js +++ b/src/actions/sponsor-pages-actions.js @@ -16,6 +16,7 @@ import { getRequest, postRequest, putRequest, + deleteRequest, startLoading, stopLoading, escapeFilterValue @@ -47,6 +48,10 @@ export const RECEIVE_SPONSOR_CUSTOMIZED_PAGE = export const SPONSOR_CUSTOMIZED_PAGE_ADDED = "SPONSOR_CUSTOMIZED_PAGE_ADDED"; export const SPONSOR_CUSTOMIZED_PAGE_UPDATED = "SPONSOR_CUSTOMIZED_PAGE_UPDATED"; +export const SPONSOR_CUSTOMIZED_PAGE_ARCHIVED = + "SPONSOR_CUSTOMIZED_PAGE_ARCHIVED"; +export const SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED = + "SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED"; export const cloneGlobalPage = (pagesIds, sponsorIds, allSponsors) => async (dispatch, getState) => { @@ -353,6 +358,71 @@ export const saveSponsorCustomizedPage = }); }; +export const archiveCustomizedPage = (pageId) => async (dispatch, getState) => { + const { currentSummitState, currentSponsorState } = getState(); + const { currentSummit } = currentSummitState; + const { + entity: { id: sponsorId } + } = currentSponsorState; + const accessToken = await getAccessTokenSafely(); + const params = { access_token: accessToken }; + + dispatch(startLoading()); + + return putRequest( + null, + createAction(SPONSOR_CUSTOMIZED_PAGE_ARCHIVED)({ pageId }), + `${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/sponsor-pages/${pageId}/archive`, + null, + snackbarErrorHandler + )(params)(dispatch) + .then(() => { + dispatch( + snackbarSuccessHandler({ + title: T.translate("general.success"), + html: T.translate("edit_sponsor.pages_tab.customized_page_archived") + }) + ); + }) + .finally(() => { + dispatch(stopLoading()); + }); +}; + +export const unarchiveCustomizedPage = + (pageId) => async (dispatch, getState) => { + const { currentSummitState, currentSponsorState } = getState(); + const { currentSummit } = currentSummitState; + const { + entity: { id: sponsorId } + } = currentSponsorState; + const accessToken = await getAccessTokenSafely(); + const params = { access_token: accessToken }; + + dispatch(startLoading()); + + return deleteRequest( + null, + createAction(SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED)({ pageId }), + `${window.SPONSOR_PAGES_API_URL}/api/v1/summits/${currentSummit.id}/sponsors/${sponsorId}/sponsor-pages/${pageId}/archive`, + null, + snackbarErrorHandler + )(params)(dispatch) + .then(() => { + dispatch( + snackbarSuccessHandler({ + title: T.translate("general.success"), + html: T.translate( + "edit_sponsor.pages_tab.customized_page_unarchived" + ) + }) + ); + }) + .finally(() => { + dispatch(stopLoading()); + }); + }; + const normalizeSponsorCustomPage = (entity, summitTZ) => { const normalizedEntity = { ...entity, diff --git a/src/i18n/en.json b/src/i18n/en.json index c9f25a6c8..5e55dab63 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -2489,7 +2489,9 @@ "add_selected_page_template": "Add Selected Page Template", "managed_page_saved": "Managed Page updated successfully.", "custom_page_saved": "Custom Sponsor Page saved", - "custom_page_created": "Custom Sponsor Page created" + "custom_page_created": "Custom Sponsor Page created", + "customized_page_archived": "Customized Sponsor Page successfully archived.", + "customized_page_unarchived": "Customized Sponsor Page successfully unarchived." }, "cart_tab": { "new_form": "New Form", diff --git a/src/pages/sponsors/sponsor-pages-tab/__tests__/sponsor-pages-tab.test.js b/src/pages/sponsors/sponsor-pages-tab/__tests__/sponsor-pages-tab.test.js index 10d610185..27f9b9d39 100644 --- a/src/pages/sponsors/sponsor-pages-tab/__tests__/sponsor-pages-tab.test.js +++ b/src/pages/sponsors/sponsor-pages-tab/__tests__/sponsor-pages-tab.test.js @@ -4,7 +4,11 @@ import { act, screen, waitFor } from "@testing-library/react"; import SponsorPagesTab from "../index"; import { renderWithRedux } from "../../../../utils/test-utils"; import { DEFAULT_STATE as sponsorPagesDefaultState } from "../../../../reducers/sponsors/sponsor-page-pages-list-reducer"; -import { getSponsorCustomizedPage } from "../../../../actions/sponsor-pages-actions"; +import { + getSponsorCustomizedPage, + archiveCustomizedPage, + unarchiveCustomizedPage +} from "../../../../actions/sponsor-pages-actions"; // Mocks @@ -46,7 +50,14 @@ jest.mock("../../../../actions/sponsor-pages-actions", () => ({ ), saveSponsorCustomizedPage: jest.fn(() => () => Promise.resolve()), saveSponsorManagedPage: jest.fn(() => () => Promise.resolve()), - resetSponsorPage: jest.fn(() => ({ type: "MOCK_ACTION" })) + resetSponsorPage: jest.fn(() => ({ type: "MOCK_ACTION" })), + archiveCustomizedPage: jest.fn(() => () => Promise.resolve()), + unarchiveCustomizedPage: jest.fn(() => () => Promise.resolve()) +})); + +jest.mock("../../../../actions/sponsor-forms-actions", () => ({ + ...jest.requireActual("../../../../actions/sponsor-forms-actions"), + getSponsorships: jest.fn(() => () => Promise.resolve()) })); // Helpers @@ -86,6 +97,12 @@ const defaultState = { }, hideArchived: false, term: "" + }, + currentSummitState: { + currentSummit: { + id: 1, + time_zone: { name: "UTC" } + } } }; @@ -219,4 +236,54 @@ describe("SponsorPagesTab", () => { ).not.toBeInTheDocument(); }); }); + + it("should call archiveCustomizedPage for non-archived item", async () => { + renderWithRedux( + , + { + initialState: { + sponsorPagePagesListState: { + ...defaultState.sponsorPagePagesListState, + customizedPages: { + ...defaultState.sponsorPagePagesListState.customizedPages, + pages: [createCustomizedPage(1, { is_archived: false })], + totalItems: 1 + } + } + } + } + ); + + const archiveButton = screen.getByText("general.archive"); + await act(async () => { + await userEvent.click(archiveButton); + }); + + expect(archiveCustomizedPage).toHaveBeenCalledWith(1); + }); + + it("should call unarchiveCustomizedPage for archived item", async () => { + renderWithRedux( + , + { + initialState: { + sponsorPagePagesListState: { + ...defaultState.sponsorPagePagesListState, + customizedPages: { + ...defaultState.sponsorPagePagesListState.customizedPages, + pages: [createCustomizedPage(1, { is_archived: true })], + totalItems: 1 + } + } + } + } + ); + + const unarchiveButton = screen.getByText("general.unarchive"); + await act(async () => { + await userEvent.click(unarchiveButton); + }); + + expect(unarchiveCustomizedPage).toHaveBeenCalledWith(1); + }); }); diff --git a/src/pages/sponsors/sponsor-pages-tab/index.js b/src/pages/sponsors/sponsor-pages-tab/index.js index 099c6ee6b..070721e82 100644 --- a/src/pages/sponsors/sponsor-pages-tab/index.js +++ b/src/pages/sponsors/sponsor-pages-tab/index.js @@ -29,13 +29,15 @@ import { saveSponsorManagedPage, saveSponsorCustomizedPage, getSponsorCustomizedPage, - resetSponsorPage + resetSponsorPage, + unarchiveCustomizedPage, + archiveCustomizedPage } from "../../../actions/sponsor-pages-actions"; import { getSponsorships } from "../../../actions/sponsor-forms-actions"; import CustomAlert from "../../../components/mui/custom-alert"; import SearchInput from "../../../components/mui/search-input"; import MuiTable from "../../../components/mui/table/mui-table"; -import { DEFAULT_CURRENT_PAGE } from "../../../utils/constants"; +import { DEFAULT_CURRENT_PAGE, MAX_PER_PAGE } from "../../../utils/constants"; import AddSponsorPageTemplatePopup from "./components/add-sponsor-page-template-popup"; import PageTemplatePopup from "../../sponsors-global/page-templates/page-template-popup"; @@ -53,6 +55,9 @@ const SponsorPagesTab = ({ getSponsorCustomizedPages, saveSponsorCustomizedPage, getSponsorCustomizedPage, + getSponsorships, + unarchiveCustomizedPage, + archiveCustomizedPage, resetSponsorPage }) => { const [openPopup, setOpenPopup] = useState(null); @@ -162,11 +167,13 @@ const SponsorPagesTab = ({ }; const handleAddPage = () => { - setOpenPopup("pagePopup"); + getSponsorships(1, MAX_PER_PAGE).then(() => setOpenPopup("pagePopup")); }; const handleArchiveCustomizedPage = (item) => - console.log("ARCHIVE CUSTOMIZED ", item); + item.is_archived + ? unarchiveCustomizedPage(item.id) + : archiveCustomizedPage(item.id); const handleArchiveManagedPage = (item) => console.log("ARCHIVE MANAGED ", item); @@ -217,7 +224,8 @@ const SponsorPagesTab = ({ order, orderDir ); - }).finally(() => setOpenPopup(null)); + }) + .finally(() => setOpenPopup(null)); }; const handleSaveCustomizedPage = (entity) => { @@ -231,7 +239,8 @@ const SponsorPagesTab = ({ order, orderDir ); - }).finally(() => setOpenPopup(null));; + }) + .finally(() => setOpenPopup(null)); }; const handleClosePagePopup = () => { @@ -433,6 +442,8 @@ export default connect(mapStateToProps, { getSponsorCustomizedPage, getSponsorCustomizedPages, saveSponsorCustomizedPage, + unarchiveCustomizedPage, + archiveCustomizedPage, getSponsorships, resetSponsorPage })(SponsorPagesTab); diff --git a/src/reducers/sponsors/sponsor-page-pages-list-reducer.js b/src/reducers/sponsors/sponsor-page-pages-list-reducer.js index 9c7f4eb03..826961717 100644 --- a/src/reducers/sponsors/sponsor-page-pages-list-reducer.js +++ b/src/reducers/sponsors/sponsor-page-pages-list-reducer.js @@ -18,7 +18,9 @@ import { RECEIVE_SPONSOR_CUSTOMIZED_PAGES, REQUEST_SPONSOR_CUSTOMIZED_PAGES, RECEIVE_SPONSOR_CUSTOMIZED_PAGE, - RESET_EDIT_PAGE + RESET_EDIT_PAGE, + SPONSOR_CUSTOMIZED_PAGE_ARCHIVED, + SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED } from "../../actions/sponsor-pages-actions"; import { SET_CURRENT_SUMMIT } from "../../actions/summit-actions"; import { RECEIVE_GLOBAL_SPONSORSHIPS } from "../../actions/sponsor-forms-actions"; @@ -173,6 +175,32 @@ const sponsorPagePagesListReducer = (state = DEFAULT_STATE, action) => { const customizedPage = payload.response; return { ...state, currentEditPage: customizedPage }; } + case SPONSOR_CUSTOMIZED_PAGE_ARCHIVED: { + const { pageId } = payload; + const pages = state.customizedPages.pages.map((page) => + page.id === pageId ? { ...page, is_archived: true } : page + ); + return { + ...state, + customizedPages: { + ...state.customizedPages, + pages: [...pages] + } + }; + } + case SPONSOR_CUSTOMIZED_PAGE_UNARCHIVED: { + const { pageId } = payload; + const pages = state.customizedPages.pages.map((page) => + page.id === pageId ? { ...page, is_archived: false } : page + ); + return { + ...state, + customizedPages: { + ...state.customizedPages, + pages: [...pages] + } + }; + } case RESET_EDIT_PAGE: { return { ...state, currentEditPage: DEFAULT_PAGE }; }