diff --git a/src/actions/inventory-item-actions.js b/src/actions/inventory-item-actions.js index 0c85483d5..9a7fa66ca 100644 --- a/src/actions/inventory-item-actions.js +++ b/src/actions/inventory-item-actions.js @@ -233,23 +233,20 @@ export const saveInventoryItem = (entity) => async (dispatch) => { promises.push(saveItemMetaFieldTypes(normalizedEntity)(dispatch)); } - Promise.all(promises) - .then(() => { - dispatch( - showSuccessMessage( - T.translate("edit_inventory_item.inventory_item_saved") - ) - ); - }) - .catch((err) => { - console.error(err); - }) - .finally(() => { - dispatch(stopLoading()); - }); + return Promise.all(promises).then(() => { + dispatch( + showSuccessMessage( + T.translate("edit_inventory_item.inventory_item_saved") + ) + ); + }); }) .catch((err) => { console.error(err); + throw err; + }) + .finally(() => { + dispatch(stopLoading()); }); } @@ -279,23 +276,20 @@ export const saveInventoryItem = (entity) => async (dispatch) => { promises.push(saveItemMetaFieldTypes(inventoryItem)(dispatch)); } - Promise.all(promises) - .then(() => { - dispatch( - showMessage(success_message, () => { - history.push("/app/inventory"); - }) - ); - }) - .catch((err) => { - console.error(err); - }) - .finally(() => { - dispatch(stopLoading()); - }); + return Promise.all(promises).then(() => { + dispatch( + showMessage(success_message, () => { + history.push("/app/inventory"); + }) + ); + }); }) .catch((err) => { console.error(err); + throw err; + }) + .finally(() => { + dispatch(stopLoading()); }); }; diff --git a/src/actions/page-template-actions.js b/src/actions/page-template-actions.js index 3f1d455d0..e84c17051 100644 --- a/src/actions/page-template-actions.js +++ b/src/actions/page-template-actions.js @@ -175,7 +175,6 @@ export const savePageTemplate = (entity) => async (dispatch) => { html: T.translate("page_template_list.page_crud.page_saved") }) ); - getPageTemplates()(dispatch); }) .catch((err) => { console.error(err); @@ -200,7 +199,6 @@ export const savePageTemplate = (entity) => async (dispatch) => { html: T.translate("page_template_list.page_crud.page_created") }) ); - getPageTemplates()(dispatch); }) .catch((err) => { console.error(err); diff --git a/src/actions/sponsor-actions.js b/src/actions/sponsor-actions.js index e20b7fb84..b32b68b40 100644 --- a/src/actions/sponsor-actions.js +++ b/src/actions/sponsor-actions.js @@ -1128,7 +1128,7 @@ export const saveSummitSponsorship = (entity) => async (dispatch, getState) => { const normalizedEntity = normalizeSponsorship(entity); if (entity.id) { - putRequest( + return putRequest( createAction(UPDATE_SUMMIT_SPONSORSHIP), createAction(SUMMIT_SPONSORSHIP_UPDATED), `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsorships-types/${entity.id}`, @@ -1140,24 +1140,24 @@ export const saveSummitSponsorship = (entity) => async (dispatch, getState) => { showSuccessMessage(T.translate("edit_sponsorship.sponsorship_saved")) ); }); - } else { - const success_message = { - title: T.translate("general.done"), - html: T.translate("edit_sponsorship.sponsorship_created"), - type: "success" - }; - - postRequest( - createAction(UPDATE_SUMMIT_SPONSORSHIP), - createAction(SUMMIT_SPONSORSHIP_ADDED), - `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsorships-types`, - normalizedEntity, - authErrorHandler, - entity - )(params)(dispatch).then(() => { - dispatch(showMessage(success_message)); - }); } + + const success_message = { + title: T.translate("general.done"), + html: T.translate("edit_sponsorship.sponsorship_created"), + type: "success" + }; + + return postRequest( + createAction(UPDATE_SUMMIT_SPONSORSHIP), + createAction(SUMMIT_SPONSORSHIP_ADDED), + `${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/sponsorships-types`, + normalizedEntity, + authErrorHandler, + entity + )(params)(dispatch).then(() => { + dispatch(showMessage(success_message)); + }); }; export const uploadSponsorshipBadgeImage = diff --git a/src/actions/sponsor-forms-actions.js b/src/actions/sponsor-forms-actions.js index 860c841e8..cec640289 100644 --- a/src/actions/sponsor-forms-actions.js +++ b/src/actions/sponsor-forms-actions.js @@ -1322,7 +1322,9 @@ export const updateSponsorFormItem = ); }); }) - .catch(console.log) // need to catch promise reject + .catch((err) => { + throw err; + }) .finally(() => { dispatch(stopLoading()); }); @@ -1457,7 +1459,9 @@ export const addInventoryItems = }) ); }) - .catch(console.log) // need to catch promise reject + .catch((err) => { + throw err; + }) .finally(() => { dispatch(stopLoading()); }); diff --git a/src/pages/sponsors-global/form-templates/form-template-from-duplicate-popup.js b/src/pages/sponsors-global/form-templates/form-template-from-duplicate-popup.js index f76fc6528..17b79a843 100644 --- a/src/pages/sponsors-global/form-templates/form-template-from-duplicate-popup.js +++ b/src/pages/sponsors-global/form-templates/form-template-from-duplicate-popup.js @@ -22,7 +22,6 @@ import SearchInput from "openstack-uicore-foundation/lib/components/mui/search-i import MenuButton from "../../../components/mui/menu-button"; const FormTemplateFromDuplicateDialog = ({ - open, options, onClose, onDuplicate, @@ -94,7 +93,7 @@ const FormTemplateFromDuplicateDialog = ({ ]; return ( - + {T.translate("form_template_from_duplicate_dialog.duplicate_form")} @@ -176,7 +175,6 @@ const FormTemplateFromDuplicateDialog = ({ }; FormTemplateFromDuplicateDialog.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onDuplicate: PropTypes.func.isRequired, onSearch: PropTypes.func.isRequired, 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 90263fa87..b14752eed 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 @@ -174,7 +174,7 @@ const FormTemplateItemListPage = ({ ); }; - const handleFormTemplateSave = (item) => { + const handleFormTemplateSave = (item) => saveFormTemplateItem(formTemplateId, item).then(() => getFormTemplateItems( formTemplateId, @@ -184,10 +184,8 @@ const FormTemplateItemListPage = ({ order, orderDir, showArchived - ) + ).catch(() => {}) ); - setShowInventoryItemModal(false); - }; const columns = [ { 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..b6d0f1cbe 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 @@ -139,6 +139,11 @@ const FormTemplateListPage = ({ setFormTemplateFromDuplicatePopupOpen(false); }; + const handleCloseFormTemplateDialog = () => { + resetFormTemplateForm(); + setFormTemplatePopupOpen(false); + }; + const handleDuplicatePopupClose = () => { getFormTemplates( "", @@ -212,17 +217,17 @@ const FormTemplateListPage = ({ sortDir: orderDir }; - const handleOnSave = async (values) => { - await saveFormTemplate(values); - getFormTemplates( - "", - DEFAULT_CURRENT_PAGE, - perPage, - order, - orderDir, - showArchived + const handleOnSave = (values) => + saveFormTemplate(values).then(() => + getFormTemplates( + "", + DEFAULT_CURRENT_PAGE, + perPage, + order, + orderDir, + showArchived + ).catch(() => {}) ); - }; return (
@@ -330,31 +335,33 @@ const FormTemplateListPage = ({ />
)} - setFormTemplatePopupOpen(false)} - onMetaFieldTypeDeleted={deleteFormTemplateMetaFieldType} - onMetaFieldTypeValueDeleted={deleteFormTemplateMetaFieldTypeValue} - onMaterialDeleted={deleteFormTemplateMaterial} - /> - + {formTemplatePopupOpen && ( + + )} + {formTemplateFromDuplicatePopupOpen && ( + + )} ); }; diff --git a/src/pages/sponsors-global/form-templates/form-template-popup.js b/src/pages/sponsors-global/form-templates/form-template-popup.js index baef4dfdc..cd9eddac9 100644 --- a/src/pages/sponsors-global/form-templates/form-template-popup.js +++ b/src/pages/sponsors-global/form-templates/form-template-popup.js @@ -27,7 +27,6 @@ import { } from "../../../utils/yup"; const FormTemplateDialog = ({ - open, onClose, onSave, toDuplicate = false, @@ -74,9 +73,9 @@ const FormTemplateDialog = ({ if (isSaving) return; setIsSaving(true); - Promise.resolve(onSave(finalValues)) + onSave(finalValues) .then(() => { - closePopup(); + onClose(); }) .catch(() => { // keep dialog open on save error to preserve user input @@ -96,7 +95,7 @@ const FormTemplateDialog = ({ return ( { + const [isSaving, setIsSaving] = useState(false); + const formik = useFormik({ initialValues: { ...initialEntity, @@ -65,7 +67,14 @@ const SponsorItemDialog = ({ quantity_limit_per_show: positiveNumberValidation(), meta_fields: formMetafieldsValidation() }), - onSubmit: (values) => onSave(values) + onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); + onSave(values) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); + } }); const mediaType = { @@ -79,6 +88,7 @@ const SponsorItemDialog = ({ useScrollToError(formik); const handleClose = () => { + if (isSaving) return; formik.resetForm(); onClose(); }; @@ -92,12 +102,18 @@ const SponsorItemDialog = ({ disableEnforceFocus disableAutoFocus disableRestoreFocus + disableEscapeKeyDown={isSaving} > {initialEntity.id ? T.translate("edit_inventory_item.edit_item") : T.translate("edit_inventory_item.new_item")} - + @@ -227,7 +243,12 @@ const SponsorItemDialog = ({ - diff --git a/src/pages/sponsors-global/inventory/inventory-list-page.js b/src/pages/sponsors-global/inventory/inventory-list-page.js index ec18de994..c497ea7bc 100644 --- a/src/pages/sponsors-global/inventory/inventory-list-page.js +++ b/src/pages/sponsors-global/inventory/inventory-list-page.js @@ -132,20 +132,17 @@ const InventoryListPage = ({ setOpen(true); }; - const handleInventorySave = (item) => { - saveInventoryItem(item) - .then(() => - getInventoryItems( - term, - currentPage, - perPage, - order, - orderDir, - showArchived - ) - ) - .finally(() => setOpen(false)); - }; + const handleInventorySave = (item) => + saveInventoryItem(item).then(() => + getInventoryItems( + term, + currentPage, + perPage, + order, + orderDir, + showArchived + ).catch(() => {}) + ); const handleArchiveItem = (item) => item.is_archived 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..d3bf33de6 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 @@ -114,9 +114,17 @@ const PageTemplateListPage = ({ setOpenCloneDialog(true); }; - const handleSavePageTemplate = (entity) => { - savePageTemplate(entity).then(() => setOpenPageDialog(false)); - }; + const handleSavePageTemplate = (entity) => + savePageTemplate(entity).then(() => + getPageTemplates( + term, + DEFAULT_CURRENT_PAGE, + perPage, + order, + orderDir, + showArchived + ).catch(() => {}) + ); const handleArchive = (item) => item.is_archived diff --git a/src/pages/sponsors-global/page-templates/page-template-popup/index.js b/src/pages/sponsors-global/page-templates/page-template-popup/index.js index 91a7b2068..d4a374cc9 100644 --- a/src/pages/sponsors-global/page-templates/page-template-popup/index.js +++ b/src/pages/sponsors-global/page-templates/page-template-popup/index.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; import T from "i18n-react/dist/i18n-react"; import PropTypes from "prop-types"; import { @@ -43,6 +43,7 @@ const PageTemplatePopup = ({ sponsorshipIds, title }) => { + const [isSaving, setIsSaving] = useState(false); const popupTitle = title ?? (pageTemplate?.id @@ -131,12 +132,17 @@ const PageTemplatePopup = ({ }), enableReinitialize: true, onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); const modulesWithOrder = values.modules.map((m, idx) => ({ ...m, custom_order: idx })); - onSave({ ...values, modules: modulesWithOrder }); + onSave({ ...values, modules: modulesWithOrder }) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); } }); @@ -179,11 +185,27 @@ const PageTemplatePopup = ({ }); }; + const handleClose = () => { + if (isSaving) return; + onClose(); + }; + return ( - + {popupTitle} - + @@ -306,7 +328,12 @@ const PageTemplatePopup = ({ - diff --git a/src/pages/sponsors/popup/add-sponsor-popup.js b/src/pages/sponsors/popup/add-sponsor-popup.js index e44b14512..0e9054e38 100644 --- a/src/pages/sponsors/popup/add-sponsor-popup.js +++ b/src/pages/sponsors/popup/add-sponsor-popup.js @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import T from "i18n-react/dist/i18n-react"; import { FormikProvider, useFormik } from "formik"; import * as yup from "yup"; @@ -23,7 +23,9 @@ import useScrollToError from "../../../hooks/useScrollToError"; import CompanyInputMUI from "../../../components/mui/formik-inputs/company-input-mui"; import SponsorshipsBySummitSelectMUI from "../../../components/mui/formik-inputs/sponsorship-summit-select-mui"; -const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { +const AddSponsorDialog = ({ onClose, onSubmit, summitId }) => { + const [isSaving, setIsSaving] = useState(false); + const formik = useFormik({ initialValues: { company: null, @@ -51,7 +53,14 @@ const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { ) .min(1, "At least one sponsorship is required") }), - onSubmit, + onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); + onSubmit(values) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); + }, enableReinitialize: true }); @@ -59,6 +68,7 @@ const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { useScrollToError(formik); const handleClose = () => { + if (isSaving) return; formik.resetForm(); onClose(); }; @@ -70,12 +80,23 @@ const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { }, [formik.errors]); return ( - + {T.translate("sponsor_list.add_sponsor")} - handleClose()} sx={{ mr: 1 }}> + handleClose()} + sx={{ mr: 1 }} + disabled={isSaving} + > @@ -135,7 +156,8 @@ const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { variant="contained" disabled={ !formik.values.company || - formik.values.sponsorships.length === 0 + formik.values.sponsorships.length === 0 || + isSaving } > {T.translate("sponsor_list.add_sponsor")} @@ -148,7 +170,6 @@ const AddSponsorDialog = ({ open, onClose, onSubmit, summitId }) => { }; AddSponsorDialog.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, summitId: PropTypes.number.isRequired diff --git a/src/pages/sponsors/popup/edit-tier-popup.js b/src/pages/sponsors/popup/edit-tier-popup.js index 48eb28c22..cb4452fa4 100644 --- a/src/pages/sponsors/popup/edit-tier-popup.js +++ b/src/pages/sponsors/popup/edit-tier-popup.js @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import T from "i18n-react/dist/i18n-react"; import { useFormik, FormikProvider } from "formik"; import * as yup from "yup"; @@ -27,13 +27,14 @@ import MuiFormikSelect from "../../../components/mui/formik-inputs/mui-formik-se import MuiFormikCheckbox from "../../../components/mui/formik-inputs/mui-formik-checkbox"; const EditTierDialog = ({ - open, onClose, onSubmit, onBadgeImageAttach, onBadgeImageRemove, entity: initialEntity }) => { + const [isSaving, setIsSaving] = useState(false); + const formik = useFormik({ initialValues: initialEntity, validationSchema: yup.object({ @@ -69,7 +70,14 @@ const EditTierDialog = ({ ), should_display_on_lobby_page: yup.bool(T.translate("validation.boolean")) }), - onSubmit, + onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); + onSubmit(values) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); + }, enableReinitialize: true }); @@ -88,6 +96,7 @@ const EditTierDialog = ({ }; const handleClose = () => { + if (isSaving) return; formik.resetForm(); onClose(); }; @@ -123,14 +132,25 @@ const EditTierDialog = ({ ]; return ( - + {`${initialEntity.id ? "Edit" : "Add"} ${T.translate( "edit_summit_sponsorship.tier" )}`} - handleClose()} sx={{ mr: 1 }}> + handleClose()} + sx={{ mr: 1 }} + disabled={isSaving} + > @@ -512,7 +532,12 @@ const EditTierDialog = ({ - @@ -523,7 +548,6 @@ const EditTierDialog = ({ }; EditTierDialog.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired }; diff --git a/src/pages/sponsors/show-pages-list-page/__tests__/show-pages-list-page.test.js b/src/pages/sponsors/show-pages-list-page/__tests__/show-pages-list-page.test.js index 89bbc7ef0..4ada962ec 100644 --- a/src/pages/sponsors/show-pages-list-page/__tests__/show-pages-list-page.test.js +++ b/src/pages/sponsors/show-pages-list-page/__tests__/show-pages-list-page.test.js @@ -41,7 +41,9 @@ jest.mock(
{pageTemplate?.id} - +
); } @@ -49,7 +51,7 @@ jest.mock( jest.mock("../../../../actions/show-pages-actions", () => ({ ...jest.requireActual("../../../../actions/show-pages-actions"), - getShowPages: jest.fn(() => ({ type: "MOCK_ACTION" })), + getShowPages: jest.fn(() => () => Promise.resolve()), getShowPage: jest.fn( () => () => Promise.resolve({ id: 1, name: "Test Page" }) ), diff --git a/src/pages/sponsors/show-pages-list-page/index.js b/src/pages/sponsors/show-pages-list-page/index.js index 2b1757d51..5eb64f82d 100644 --- a/src/pages/sponsors/show-pages-list-page/index.js +++ b/src/pages/sponsors/show-pages-list-page/index.js @@ -106,7 +106,7 @@ const ShowPagesListPage = ({ const handleShowArchivedForms = (ev) => { getShowPages( term, - currentPage, + DEFAULT_CURRENT_PAGE, perPage, order, orderDir, @@ -125,12 +125,17 @@ const ShowPagesListPage = ({ ); }; - const handleSaveShowPage = (entity) => { - saveShowPage(entity).then(() => { - setOpenPopup(null); - getShowPages(); - }); - }; + const handleSaveShowPage = (entity) => + saveShowPage(entity).then(() => + getShowPages( + term, + DEFAULT_CURRENT_PAGE, + perPage, + order, + orderDir, + showArchived + ).catch(() => {}) + ); const handleOpenPageTemplatePopup = async (row) => { await Promise.all([ 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 f602a371c..c8ff0d0bd 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 @@ -1,72 +1,35 @@ import React from "react"; -import { screen } from "@testing-library/react"; +import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { renderWithRedux } from "../../../../../utils/test-utils"; import InventoryPopup from "../sponsor-form-add-item-from-inventory-popup"; -jest.mock("../../../../../actions/inventory-item-actions", () => ({ - getInventoryItems: jest.fn(() => ({ - type: "RECEIVE_INVENTORY_ITEMS", - payload: { - response: { - data: [ - { - id: "123", - code: "AAA", - name: "My Item", - early_bird_rate: 1000, - standard_rate: 1000, - onsite_rate: 1000, - is_archived: false - }, - - { - id: "456", - code: "AAAA", - name: "My Item 2", - early_bird_rate: 1000, - standard_rate: 1000, - onsite_rate: 1000, - is_archived: false - } - ] - }, - current_page: 1, - total: 2, - last_page: 1 - } - })) -})); - jest.mock("openstack-uicore-foundation/lib/utils/money", () => ({ currencyAmountFromCents: jest.fn() })); describe("InventoryPopup", () => { + const buildInventoryItems = (items = []) => ({ + inventoryItems: items, + term: "", + order: "", + orderDir: "1", + currentPage: 1, + perPage: 10, + totalInventoryItems: items.length + }); + it("check if title is being rendered", async () => { - const formId = "AAA"; - const open = true; const onClose = jest.fn(); - - const inventoryItems = { - inventoryItems: [], - term: 1, - order: 1, - orderDir: "A", - currentPage: 1, - perPag: 3, - totalInventoryItems: 2 - }; - - renderWithRedux( - , - { - initialState: { - currentInventoryItemListState: { - inventoryItems - } - } - } + const onSave = jest.fn(); + const getInventoryItems = jest.fn(); + + render( + ); const node = screen.getByText( @@ -78,29 +41,17 @@ describe("InventoryPopup", () => { }); it("check if close button calls close callback", async () => { - const formId = "AAA"; - const open = true; const onClose = jest.fn(); - - const inventoryItems = { - inventoryItems: [], - term: 1, - order: 1, - orderDir: "A", - currentPage: 1, - perPag: 3, - totalInventoryItems: 2 - }; - - renderWithRedux( - , - { - initialState: { - currentInventoryItemListState: { - inventoryItems - } - } - } + const onSave = jest.fn(); + const getInventoryItems = jest.fn(); + + render( + ); const user = userEvent.setup(); @@ -111,46 +62,38 @@ describe("InventoryPopup", () => { }); it("check if close button clears selected rows", async () => { - const formId = "AAA"; - const open = true; const onClose = jest.fn(); - - renderWithRedux( - , - { - initialState: { - currentInventoryItemListState: { - inventoryItems: [ - { - id: "123", - code: "AAA", - name: "My Item", - early_bird_rate: "100", - standard_rate: "100", - onsite_rate: "100", - hasImage: false, - images: [] - }, - { - id: "456", - code: "AAAA", - name: "My Item", - early_bird_rate: "100", - standard_rate: "100", - onsite_rate: "100", - hasImage: false, - images: [] - } - ], - term: "", - order: "", - orderDir: "1", - currentPage: 1, - perPage: 10, - totalInventoryItems: 2 + const onSave = jest.fn(); + const getInventoryItems = jest.fn(); + + render( + ); const user = userEvent.setup(); 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 b2e980d8a..8df83f36c 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 @@ -1,7 +1,6 @@ import React, { useEffect, useState } from "react"; import PropTypes from "prop-types"; import T from "i18n-react/dist/i18n-react"; -import { connect } from "react-redux"; import { Button, Checkbox, @@ -21,15 +20,11 @@ import MuiTable from "openstack-uicore-foundation/lib/components/mui/table"; import SearchInput from "openstack-uicore-foundation/lib/components/mui/search-input"; import { ImagePreviewCell } from "../../../../components/image-preview-cell"; import { formatRateFromCents } from "../../../../utils/rate-helpers"; -import { addInventoryItems } from "../../../../actions/sponsor-forms-actions"; -import { getInventoryItems } from "../../../../actions/inventory-item-actions"; import { DEFAULT_CURRENT_PAGE } from "../../../../utils/constants"; const SponsorFormAddItemFromInventoryPopup = ({ - formId, - open, onClose, - addInventoryItems, + onSave, getInventoryItems, inventoryItems }) => { @@ -43,16 +38,21 @@ const SponsorFormAddItemFromInventoryPopup = ({ totalInventoryItems: total } = inventoryItems; const [selectedRows, setSelectedRows] = useState([]); + const [isSaving, setIsSaving] = useState(false); const handleClose = () => { + if (isSaving) return; setSelectedRows([]); onClose(); }; const handleOnAdd = () => { - addInventoryItems(formId, selectedRows).finally(() => { - handleClose(); - }); + if (isSaving) return; + setIsSaving(true); + onSave(selectedRows) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); }; const handleOnCheck = (rowId, checked) => { @@ -161,7 +161,13 @@ const SponsorFormAddItemFromInventoryPopup = ({ ]; return ( - + @@ -217,7 +224,7 @@ const SponsorFormAddItemFromInventoryPopup = ({ diff --git a/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-item-popup.js b/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-item-popup.js index e62317851..b908a5c4d 100644 --- a/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-item-popup.js +++ b/src/pages/sponsors/sponsor-form-item-list-page/components/sponsor-form-item-popup.js @@ -1,7 +1,6 @@ -import React from "react"; +import React, { useState } from "react"; import PropTypes from "prop-types"; import T from "i18n-react/dist/i18n-react"; -import { connect } from "react-redux"; import { Dialog, DialogTitle, @@ -10,38 +9,33 @@ import { Typography } from "@mui/material"; import CloseIcon from "@mui/icons-material/Close"; -import { - resetSponsorFormItem, - saveSponsorFormItem, - updateSponsorFormItem -} from "../../../../actions/sponsor-forms-actions"; import SponsorFormItemForm from "./sponsor-form-item-form"; -const SponsorFormItemPopup = ({ - formId, - item, - open, - onClose, - resetSponsorFormItem, - saveSponsorFormItem, - updateSponsorFormItem -}) => { +const SponsorFormItemPopup = ({ item, onClose, onSave }) => { + const [isSaving, setIsSaving] = useState(false); + const handleClose = () => { - // clear form from reducer - resetSponsorFormItem(); + if (isSaving) return; onClose(); }; const handleOnSave = (values) => { - const save = values.id ? updateSponsorFormItem : saveSponsorFormItem; - - save(formId, values).finally(() => { - handleClose(); - }); + if (isSaving) return; + setIsSaving(true); + onSave(values) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); }; return ( - + - + - + ); }; SponsorFormItemPopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, - formId: PropTypes.string.isRequired, - resetSponsorFormItem: PropTypes.func.isRequired, - saveSponsorFormItem: PropTypes.func.isRequired, - updateSponsorFormItem: PropTypes.func.isRequired + onSave: PropTypes.func.isRequired, + item: PropTypes.object }; -const mapStateToProps = ({ - sponsorFormItemsListState, - currentSummitState -}) => ({ - item: sponsorFormItemsListState.currentItem, - summitTZ: currentSummitState.currentSummit.time_zone_id -}); - -export default connect(mapStateToProps, { - resetSponsorFormItem, - saveSponsorFormItem, - updateSponsorFormItem -})(SponsorFormItemPopup); +export default SponsorFormItemPopup; 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 03768bc3a..34bf47211 100644 --- a/src/pages/sponsors/sponsor-form-item-list-page/index.js +++ b/src/pages/sponsors/sponsor-form-item-list-page/index.js @@ -31,10 +31,14 @@ import { deleteSponsorFormItem, getSponsorFormItem, getSponsorFormItems, + saveSponsorFormItem, updateSponsorFormItem, + addInventoryItems, + resetSponsorFormItem, archiveSponsorFormItem, unarchiveSponsorFormItem } from "../../../actions/sponsor-forms-actions"; +import { getInventoryItems } from "../../../actions/inventory-item-actions"; import SponsorFormItemPopup from "./components/sponsor-form-item-popup"; import SponsorFormAddItemFromInventoryPopup from "./components/sponsor-form-add-item-from-inventory-popup"; import { DEFAULT_CURRENT_PAGE } from "../../../utils/constants"; @@ -44,16 +48,22 @@ import { rateToCents } from "../../../utils/rate-helpers"; const SponsorFormItemListPage = ({ match, items, + currentItem, currentPage, perPage, showArchived, order, orderDir, totalCount, + inventoryItems, + getInventoryItems, getSponsorFormItems, getSponsorFormItem, deleteSponsorFormItem, + saveSponsorFormItem, updateSponsorFormItem, + addInventoryItems, + resetSponsorFormItem, archiveSponsorFormItem, unarchiveSponsorFormItem }) => { @@ -100,6 +110,19 @@ const SponsorFormItemListPage = ({ }); }; + const handleClosePopup = () => { + resetSponsorFormItem(); + setOpenPopup(null); + }; + + const handleSaveItem = (values) => { + const save = values.id ? updateSponsorFormItem : saveSponsorFormItem; + return save(formId, values); + }; + + const handleAddFromInventory = (itemIds) => + addInventoryItems(formId, itemIds); + const handleCellEdit = (rowId, column, value) => { // since editable cell is TextField and not PriceField, we need to convert to cents const valueInCents = rateToCents(value); @@ -126,6 +149,7 @@ const SponsorFormItemListPage = ({ }; const handleNewItem = () => { + resetSponsorFormItem(); setOpenPopup("crud"); }; @@ -301,29 +325,42 @@ const SponsorFormItemListPage = ({ /> )} - setOpenPopup(null)} - /> - setOpenPopup(null)} - /> + {openPopup === "crud" && ( + + )} + {openPopup === "inventory" && ( + setOpenPopup(null)} + /> + )} ); }; -const mapStateToProps = ({ sponsorFormItemsListState }) => ({ - ...sponsorFormItemsListState +const mapStateToProps = ({ + sponsorFormItemsListState, + currentInventoryItemListState +}) => ({ + ...sponsorFormItemsListState, + inventoryItems: currentInventoryItemListState }); export default connect(mapStateToProps, { getSponsorFormItems, deleteSponsorFormItem, getSponsorFormItem, + saveSponsorFormItem, updateSponsorFormItem, + addInventoryItems, + resetSponsorFormItem, + getInventoryItems, archiveSponsorFormItem, unarchiveSponsorFormItem })(SponsorFormItemListPage); diff --git a/src/pages/sponsors/sponsor-forms-list-page/components/form-template/form-template-popup.js b/src/pages/sponsors/sponsor-forms-list-page/components/form-template/form-template-popup.js index 964104c19..916e52f8e 100644 --- a/src/pages/sponsors/sponsor-forms-list-page/components/form-template/form-template-popup.js +++ b/src/pages/sponsors/sponsor-forms-list-page/components/form-template/form-template-popup.js @@ -23,7 +23,6 @@ const FormTemplatePopup = ({ summitTZ, sponsorships, formTemplate, - open, onClose, getSummitSponsorshipTypes, resetFormTemplate, @@ -67,7 +66,7 @@ const FormTemplatePopup = ({ return ( { handleClose(); }} @@ -111,7 +110,6 @@ const FormTemplatePopup = ({ }; FormTemplatePopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, edit: PropTypes.bool.isRequired }; diff --git a/src/pages/sponsors/sponsor-forms-list-page/components/global-template/global-template-popup.js b/src/pages/sponsors/sponsor-forms-list-page/components/global-template/global-template-popup.js index 9058f366c..72ae3e6c3 100644 --- a/src/pages/sponsors/sponsor-forms-list-page/components/global-template/global-template-popup.js +++ b/src/pages/sponsors/sponsor-forms-list-page/components/global-template/global-template-popup.js @@ -6,7 +6,7 @@ import { cloneGlobalTemplate } from "../../../../../actions/sponsor-forms-action import SelectTemplatesDialog from "./select-templates-dialog"; import SelectSponsorshipsDialog from "./select-sponsorships-dialog"; -const GlobalTemplatePopup = ({ open, onClose, cloneGlobalTemplate }) => { +const GlobalTemplatePopup = ({ onClose, cloneGlobalTemplate }) => { const [stage, setStage] = useState("templates"); const [selectedTemplates, setSelectedTemplates] = useState([]); const [isSaving, setIsSaving] = useState(false); @@ -45,7 +45,7 @@ const GlobalTemplatePopup = ({ open, onClose, cloneGlobalTemplate }) => { return ( { }; GlobalTemplatePopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired }; diff --git a/src/pages/sponsors/sponsor-forms-list-page/index.js b/src/pages/sponsors/sponsor-forms-list-page/index.js index 0c7d3e6ac..dc1cabe4d 100644 --- a/src/pages/sponsors/sponsor-forms-list-page/index.js +++ b/src/pages/sponsors/sponsor-forms-list-page/index.js @@ -432,15 +432,15 @@ const SponsorFormsListPage = ({ /> )} - setOpenPopup(null)} - /> - setOpenPopup(null)} - edit={openPopup === "edit"} - /> + {openPopup === "clone" && ( + setOpenPopup(null)} /> + )} + {(openPopup === "new" || openPopup === "edit") && ( + setOpenPopup(null)} + edit={openPopup === "edit"} + /> + )} ); }; diff --git a/src/pages/sponsors/sponsor-list-page.js b/src/pages/sponsors/sponsor-list-page.js index 7b9ee01dd..9bc7d95f8 100644 --- a/src/pages/sponsors/sponsor-list-page.js +++ b/src/pages/sponsors/sponsor-list-page.js @@ -69,12 +69,12 @@ const SponsorListPage = ({ deleteSponsor(sponsorId); }; - const handleNewSponsor = (sponsor) => { - addSponsorToSummit(sponsor).then(() => { - setShowAddSponsorModal(false); - getSponsors(term, DEFAULT_CURRENT_PAGE, perPage, order, orderDir); - }); - }; + const handleNewSponsor = (sponsor) => + addSponsorToSummit(sponsor).then(() => + getSponsors(term, DEFAULT_CURRENT_PAGE, perPage, order, orderDir).catch( + () => {} + ) + ); const handlePageChange = (page) => { getSponsors(term, page, perPage, order, orderDir); @@ -272,7 +272,6 @@ const SponsorListPage = ({ {showAddSponsorModal && ( { return values; }; -const EditBadgeScanPopup = ({ badgeScan, open, onClose, onSubmit }) => { +const EditBadgeScanPopup = ({ badgeScan, onClose, onSubmit }) => { const formik = useFormik({ initialValues: { id: badgeScan.id, @@ -111,7 +111,7 @@ const EditBadgeScanPopup = ({ badgeScan, open, onClose, onSubmit }) => { }, [formik.values]); return ( - + {T.translate("edit_badge_scan.edit_badge_scan")} @@ -218,7 +218,6 @@ const EditBadgeScanPopup = ({ badgeScan, open, onClose, onSubmit }) => { }; EditBadgeScanPopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired }; diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-badge-scans/index.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-badge-scans/index.js index 7b9699c1a..b85c38f95 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-badge-scans/index.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-badge-scans/index.js @@ -244,7 +244,6 @@ const SponsorBadgeScans = ({ {showEditBadgeScanPopup && ( diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/__tests__/select-form-dialog.test.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/__tests__/select-form-dialog.test.js index 298658f7f..bbc6efcda 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/__tests__/select-form-dialog.test.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/__tests__/select-form-dialog.test.js @@ -157,7 +157,6 @@ const renderWithStore = (props, storeState = {}) => { const store = mockStore(defaultState); const defaultProps = { - open: true, summitId: 1, sponsor: { id: 10, name: "Test Sponsor" }, onClose: jest.fn(), @@ -197,16 +196,6 @@ describe("SelectFormDialog", () => { expect(getSponsorFormsForCart).toHaveBeenCalled(); }); - test("does not call getSponsorFormsForCart when dialog is closed", () => { - const { - getSponsorFormsForCart - } = require("../../../../../../../../actions/sponsor-cart-actions"); - - renderWithStore({ open: false }); - - expect(getSponsorFormsForCart).not.toHaveBeenCalled(); - }); - test("displays initial selection count as 0", () => { renderWithStore(); diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/index.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/index.js index 8b4631380..2d9c2fdd2 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/index.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-cart-tab/components/select-form-dialog/index.js @@ -26,7 +26,6 @@ const SelectFormDialog = ({ availableForms, summitId, sponsor, - open, onSave, onClose, getSponsorFormsForCart @@ -36,10 +35,8 @@ const SelectFormDialog = ({ const { forms, currentPage, term, order, orderDir, total } = availableForms; useEffect(() => { - if (open) { - getSponsorFormsForCart(); - } - }, [open]); + getSponsorFormsForCart(); + }, []); const handleSort = (key, dir) => { getSponsorFormsForCart(term, 1, key, dir); @@ -110,7 +107,7 @@ const SelectFormDialog = ({ ]; return ( - + { )} - setOpenAddFormDialog(false)} - /> + {!!openAddFormDialog && ( + setOpenAddFormDialog(false)} + /> + )} )} /> diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/customized-form/__tests__/customized-form-popup.test.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/customized-form/__tests__/customized-form-popup.test.js index 036ca9781..42cacff58 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/customized-form/__tests__/customized-form-popup.test.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-forms-tab/components/customized-form/__tests__/customized-form-popup.test.js @@ -66,7 +66,6 @@ describe("CustomizedFormPopup", () => { renderWithRedux( { renderWithRedux( { renderWithRedux( { + const handleItemSave = (item) => saveSponsorFormManagedItem(formId, item).then(() => { getSponsorCustomizedFormItems( formId, @@ -139,11 +139,9 @@ const SponsorFormsManageItems = ({ order, orderDir, showArchived - ); + ).catch(() => {}); resetSponsorFormManagedItem(); - setOpenPopup(null); }); - }; const handleOpenItemPopup = () => { resetSponsorFormManagedItem(); 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 e1c0f8a10..9cce303e1 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 @@ -496,15 +496,16 @@ const SponsorFormsTab = ({ /> )} - + {!!customFormEdit && ( + + )} ); }; diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-extra-question-popup.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-extra-question-popup.js index 5b3c04c00..a1083be38 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-extra-question-popup.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-extra-question-popup.js @@ -39,7 +39,6 @@ import { ExtraQuestionsTypeAllowSubQuestion } from "../../../../../utils/constan const AddSponsorExtraQuestionPopup = ({ entity: extraQuestion, summit, - open, onClose, onSubmit, allClasses, @@ -296,7 +295,7 @@ const AddSponsorExtraQuestionPopup = ({ : []; return ( - + {extraQuestion.id @@ -655,7 +654,6 @@ const mapStateToProps = ({ currentSponsorExtraQuestionState }) => ({ }); AddSponsorExtraQuestionPopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired }; diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-tier-popup.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-tier-popup.js index fbf6d6d5d..6cbf069c4 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-tier-popup.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/add-tier-popup.js @@ -23,7 +23,7 @@ import useScrollToError from "../../../../../hooks/useScrollToError"; import SponsorshipsBySummitSelectMUI from "../../../../../components/mui/formik-inputs/sponsorship-summit-select-mui"; import MuiFormikTextField from "../../../../../components/mui/formik-inputs/mui-formik-textfield"; -const AddTierPopup = ({ sponsor, open, onClose, onSubmit, summitId }) => { +const AddTierPopup = ({ sponsor, onClose, onSubmit, summitId }) => { const formik = useFormik({ initialValues: { company: sponsor.company?.name, @@ -68,7 +68,7 @@ const AddTierPopup = ({ sponsor, open, onClose, onSubmit, summitId }) => { }, [formik.values]); return ( - + {T.translate("edit_sponsor.add_tier")} @@ -147,7 +147,6 @@ const AddTierPopup = ({ sponsor, open, onClose, onSubmit, summitId }) => { }; AddTierPopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, summitId: PropTypes.number.isRequired diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/extra-questions.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/extra-questions.js index 6fa3703f6..62da352bf 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/extra-questions.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/extra-questions.js @@ -209,7 +209,6 @@ const SponsorExtraQuestions = ({ {showAddExtraQuestionPopup && ( + {T.translate("edit_sponsor.manage_addons")} @@ -396,7 +395,6 @@ const ManageTierAddonsPopup = ({ }; ManageTierAddonsPopup.propTypes = { - open: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, onSponsorshipAddonRemove: PropTypes.func.isRequired, diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/sponsorship.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/sponsorship.js index 2b3ccbd75..5a966b761 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/sponsorship.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-general-form/sponsorship.js @@ -174,7 +174,6 @@ const Sponsorship = ({ @@ -184,7 +183,6 @@ const Sponsorship = ({ {pageTemplate?.id} - + ); } @@ -47,8 +49,8 @@ jest.mock( jest.mock("../../../../../../actions/sponsor-pages-actions", () => ({ ...jest.requireActual("../../../../../../actions/sponsor-pages-actions"), - getSponsorManagedPages: jest.fn(() => ({ type: "MOCK_ACTION" })), - getSponsorCustomizedPages: jest.fn(() => ({ type: "MOCK_ACTION" })), + getSponsorManagedPages: jest.fn(() => () => Promise.resolve()), + getSponsorCustomizedPages: jest.fn(() => () => Promise.resolve()), getSponsorCustomizedPage: jest.fn( () => () => Promise.resolve({ id: 1, name: "Test Page" }) ), diff --git a/src/pages/sponsors/sponsor-page/tabs/sponsor-pages-tab/components/add-sponsor-page-template-popup/index.js b/src/pages/sponsors/sponsor-page/tabs/sponsor-pages-tab/components/add-sponsor-page-template-popup/index.js index 9a72b86ab..90cf133cd 100644 --- a/src/pages/sponsors/sponsor-page/tabs/sponsor-pages-tab/components/add-sponsor-page-template-popup/index.js +++ b/src/pages/sponsors/sponsor-page/tabs/sponsor-pages-tab/components/add-sponsor-page-template-popup/index.js @@ -59,6 +59,7 @@ const AddSponsorPageTemplatePopup = ({ summitId }) => { const [selectedPages, setSelectedPages] = useState([]); + const [isSaving, setIsSaving] = useState(false); const sponsorshipIds = sponsor.sponsorships.map((e) => e.id); @@ -72,12 +73,17 @@ const AddSponsorPageTemplatePopup = ({ add_ons: yup.array() }), onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); const { add_ons } = values; const entity = { pages: selectedPages, add_ons }; - onSubmit(entity); + onSubmit(entity) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); }, enableReinitialize: true }); @@ -140,6 +146,7 @@ const AddSponsorPageTemplatePopup = ({ }; const handleClose = () => { + if (isSaving) return; onClose(); }; @@ -190,12 +197,23 @@ const AddSponsorPageTemplatePopup = ({ ]; return ( - + {T.translate("edit_sponsor.pages_tab.add_page_using_template")} - handleClose()} sx={{ mr: 1 }}> + handleClose()} + sx={{ mr: 1 }} + disabled={isSaving} + > @@ -293,7 +311,7 @@ const AddSponsorPageTemplatePopup = ({ diff --git a/src/pages/sponsorship-types/components/sponsorship-dialog.js b/src/pages/sponsorship-types/components/sponsorship-dialog.js index dcb093564..737a88aef 100644 --- a/src/pages/sponsorship-types/components/sponsorship-dialog.js +++ b/src/pages/sponsorship-types/components/sponsorship-dialog.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; import T from "i18n-react/dist/i18n-react"; import PropTypes from "prop-types"; import { FormikProvider, useFormik } from "formik"; @@ -28,12 +28,9 @@ const SIZE_OPTIONS_DDL = [ { label: "Big", value: "Big" } ]; -const SponsorshipDialog = ({ - entity: initialEntity, - onClose, - onSave, - isSaving = false -}) => { +const SponsorshipDialog = ({ entity: initialEntity, onClose, onSave }) => { + const [isSaving, setIsSaving] = useState(false); + const formik = useFormik({ initialValues: { id: initialEntity?.id ?? 0, @@ -45,12 +42,20 @@ const SponsorshipDialog = ({ validationSchema: yup.object().shape({ name: requiredStringValidation() }), - onSubmit: (values) => onSave(values) + onSubmit: (values) => { + if (isSaving) return; + setIsSaving(true); + onSave(values) + .then(() => onClose()) + .catch(() => {}) + .finally(() => setIsSaving(false)); + } }); useScrollToError(formik); const handleClose = () => { + if (isSaving) return; formik.resetForm(); onClose(); }; diff --git a/src/pages/sponsorship-types/sponsorship-list-page.js b/src/pages/sponsorship-types/sponsorship-list-page.js index 66854a359..3004bc7ec 100644 --- a/src/pages/sponsorship-types/sponsorship-list-page.js +++ b/src/pages/sponsorship-types/sponsorship-list-page.js @@ -44,7 +44,6 @@ const SponsorshipListPage = ({ resetSponsorshipForm }) => { const [open, setOpen] = useState(false); - const [isSaving, setIsSaving] = useState(false); useEffect(() => { getSponsorships(); @@ -76,21 +75,20 @@ const SponsorshipListPage = ({ }; const handleClose = () => { - if (isSaving) return; resetSponsorshipForm(); setOpen(false); }; - const handleSave = (entity) => { - if (isSaving) return; - setIsSaving(true); - saveSponsorship(entity) - .then(() => - getSponsorships(term, DEFAULT_CURRENT_PAGE, perPage, order, orderDir) - ) - .then(() => setOpen(false)) - .finally(() => setIsSaving(false)); - }; + const handleSave = (entity) => + saveSponsorship(entity).then(() => + getSponsorships( + term, + DEFAULT_CURRENT_PAGE, + perPage, + order, + orderDir + ).catch(() => {}) + ); const handleDelete = (sponsorshipId) => { deleteSponsorship(sponsorshipId); @@ -196,7 +194,6 @@ const SponsorshipListPage = ({ entity={currentSponsorship} onSave={handleSave} onClose={handleClose} - isSaving={isSaving} /> )} diff --git a/src/pages/tags/tag-list-page.js b/src/pages/tags/tag-list-page.js index 498c1bff8..0e4155652 100644 --- a/src/pages/tags/tag-list-page.js +++ b/src/pages/tags/tag-list-page.js @@ -75,9 +75,9 @@ const TagListPage = ({ }; const handleSaveTag = (entity) => - saveTag(entity).then(() => { - getTags(search, currentPage, perPage, order, orderDir); - }); + saveTag(entity).then(() => + getTags(search, currentPage, perPage, order, orderDir).catch(() => {}) + ); const handleDeleteTag = (id) => { deleteTag(id)