Skip to content

Commit

Permalink
fix: prevent duplicate requests to api on forms
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Apr 21, 2022
1 parent d4291b8 commit 2a341f6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/components/forms/SplitUnitForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 =
Expand Down

0 comments on commit 2a341f6

Please sign in to comment.