From 2a341f6e88f049331cd2ca8d9f8a35933b0b42a6 Mon Sep 17 00:00:00 2001 From: Daniel Stefan Date: Thu, 21 Apr 2022 14:54:14 +0300 Subject: [PATCH] fix: prevent duplicate requests to api on forms --- src/components/forms/SplitUnitForm.js | 60 ++++++++++++++------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/components/forms/SplitUnitForm.js b/src/components/forms/SplitUnitForm.js index 4312d969..9c0a5873 100644 --- a/src/components/forms/SplitUnitForm.js +++ b/src/components/forms/SplitUnitForm.js @@ -49,7 +49,7 @@ const SplitUnitForm = ({ onClose, record }) => { ]); const intl = useIntl(); const [validationErrors, setValidationErrors] = useState([]); - const { notification } = useSelector(state => state.app); + const { notification, showProgressOverlay } = useSelector(state => state.app); const { units, pickLists } = useSelector(store => store.climateWarehouse); const fullRecord = units.filter( @@ -73,39 +73,41 @@ const SplitUnitForm = ({ onClose, record }) => { }); const onSubmit = () => { - validationSchema - .validate(data, { abortEarly: false, recursive: true }) - .then(() => { - setValidationErrors([]); - dispatch( - splitUnits({ - warehouseUnitId: fullRecord.warehouseUnitId, - records: data.map(splittedUnit => { - const newUnit = {}; - newUnit.unitCount = splittedUnit.unitCount; + if (!showProgressOverlay) { + validationSchema + .validate(data, { abortEarly: false, recursive: true }) + .then(() => { + setValidationErrors([]); + dispatch( + splitUnits({ + warehouseUnitId: fullRecord.warehouseUnitId, + records: data.map(splittedUnit => { + const newUnit = {}; + newUnit.unitCount = splittedUnit.unitCount; - if (splittedUnit.unitOwner !== '') { - newUnit.unitOwner = splittedUnit.unitOwner; - } + if (splittedUnit.unitOwner !== '') { + newUnit.unitOwner = splittedUnit.unitOwner; + } - if (splittedUnit.countryJurisdictionOfOwner !== '') { - newUnit.countryJurisdictionOfOwner = - splittedUnit.countryJurisdictionOfOwner; - } + if (splittedUnit.countryJurisdictionOfOwner !== '') { + newUnit.countryJurisdictionOfOwner = + splittedUnit.countryJurisdictionOfOwner; + } - if (splittedUnit.inCountryJurisdictionOfOwner !== '') { - newUnit.inCountryJurisdictionOfOwner = - splittedUnit.inCountryJurisdictionOfOwner; - } + if (splittedUnit.inCountryJurisdictionOfOwner !== '') { + newUnit.inCountryJurisdictionOfOwner = + splittedUnit.inCountryJurisdictionOfOwner; + } - return newUnit; + return newUnit; + }), }), - }), - ); - }) - .catch(err => { - setValidationErrors([...err.errors]); - }); + ); + }) + .catch(err => { + setValidationErrors([...err.errors]); + }); + } }; const unitWasSuccessfullySplit =