Skip to content

Commit

Permalink
Only trigger deny callback if option was explicitly chosen by user (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz authored Mar 15, 2022
1 parent 86be69f commit e64a2cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/components/CustomDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ interface DialogState extends DialogShowData {
isOpen: boolean;
}

export type DialogCloseReason = 'escapeKeyDown' | 'backdropClick' | 'user';

export const CustomDialog = (): JSX.Element => {
const [dialog, setDialog] = useState<DialogState>({
isOpen: false,
title: '',
message: '',
});

const closeDialog = (didConfirm: boolean) => {
const callback = didConfirm ? dialog.onConfirm : dialog.onDeny;
const closeDialog = (reason: DialogCloseReason, didConfirm: boolean) => {
let callback;
if (didConfirm) {
callback = dialog.onConfirm;
} else if (reason === 'user') {
callback = dialog.onDeny;
}
setDialog((prevDialog) => ({
...prevDialog,
isOpen: false,
Expand Down Expand Up @@ -57,17 +64,17 @@ export const CustomDialog = (): JSX.Element => {
}, []);

return (
<CustomDialogRoot onClose={() => closeDialog(false)} open={dialog.isOpen}>
<CustomDialogRoot onClose={(e, reason) => closeDialog(reason, false)} open={dialog.isOpen}>
<DialogTitle>{dialog.title}</DialogTitle>
<DialogContent>
<DialogContentText>{dialog.message}</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => closeDialog(false)}>
<Button onClick={() => closeDialog('user', false)}>
{I18N.translate(dialog.onConfirm || dialog.onDeny ? 'no' : 'close')}
</Button>
{dialog.onConfirm && (
<Button onClick={() => closeDialog(true)} variant="contained">
<Button onClick={() => closeDialog('user', true)} variant="contained">
{I18N.translate('yes')}
</Button>
)}
Expand Down
1 change: 0 additions & 1 deletion src/components/SyncDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const SyncDialog = (): JSX.Element => {
<CustomDialogRoot
open={isOpen}
aria-labelledby="sync-dialog-title"
disableEscapeKeyDown={true}
onClose={() => {
// Do nothing
}}
Expand Down

0 comments on commit e64a2cd

Please sign in to comment.