Skip to content

Commit

Permalink
fix: prevent spam click of confirm button which creates many same items
Browse files Browse the repository at this point in the history
  • Loading branch information
codeofmochi committed Jun 29, 2021
1 parent 7066c10 commit 4d3f56f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/components/main/NewItemModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ const useStyles = makeStyles(() => ({
},
}));

// time to be considered between 2 clicks for a double-click (https://en.wikipedia.org/wiki/Double-click#Speed_and_timing)
const DOUBLE_CLICK_DELAY_MS = 500;

const NewItemModal = ({ open, handleClose }) => {
const { t } = useTranslation();
const classes = useStyles();
const [isConfirmButtonDisabled, setConfirmButtonDisabled] = useState(false);
const [selectedItemType, setSelectedItemType] = useState(ITEM_TYPES.FOLDER);
const [initialItem] = useState({});
const [currentType, setCurrentType] = useState(ITEM_TYPES.FOLDER);
Expand Down Expand Up @@ -67,12 +71,18 @@ const NewItemModal = ({ open, handleClose }) => {
}, [selectedItemType]);

const submit = () => {
if (isConfirmButtonDisabled) {
return false;
}
if (!isItemValid(updatedPropertiesPerType[currentType])) {
// todo: notify user
return false;
}

setConfirmButtonDisabled(true);
postItem({ parentId, ...updatedPropertiesPerType[currentType] });
// schedule button disable state reset AFTER end of click event handling
setTimeout(() => setConfirmButtonDisabled(false), DOUBLE_CLICK_DELAY_MS);
return handleClose();
};

Expand Down Expand Up @@ -140,7 +150,10 @@ const NewItemModal = ({ open, handleClose }) => {
onClick={submit}
color="primary"
id={ITEM_FORM_CONFIRM_BUTTON_ID}
disabled={!isItemValid(updatedPropertiesPerType[currentType])}
disabled={
isConfirmButtonDisabled ||
!isItemValid(updatedPropertiesPerType[currentType])
}
>
{t('Add')}
</Button>
Expand Down

0 comments on commit 4d3f56f

Please sign in to comment.