From 24442d524b3df42671f857b5f736c2c9872e01a5 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Mon, 28 Feb 2022 22:56:06 -0500 Subject: [PATCH] fix(striker-ui): add purge file confirm dialog --- striker-ui/components/ConfirmDialog.tsx | 68 ++++++++++++++++++++ striker-ui/components/Files/FileEditForm.tsx | 50 ++++++++++++-- 2 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 striker-ui/components/ConfirmDialog.tsx diff --git a/striker-ui/components/ConfirmDialog.tsx b/striker-ui/components/ConfirmDialog.tsx new file mode 100644 index 00000000..2c365eaf --- /dev/null +++ b/striker-ui/components/ConfirmDialog.tsx @@ -0,0 +1,68 @@ +import { MouseEventHandler } from 'react'; +import { Box, ButtonProps, Dialog, DialogProps } from '@mui/material'; + +import ContainedButton from './ContainedButton'; +import { Panel, PanelHeader } from './Panels'; +import { BodyText, HeaderText } from './Text'; + +type ConfirmDialogProps = { + actionCancelText?: string; + actionProceedText: string; + contentText: string; + dialogProps: DialogProps; + onCancel: MouseEventHandler; + onProceed: MouseEventHandler; + proceedButtonProps?: ButtonProps; + titleText: string; +}; + +const CONFIRM_DIALOG_DEFAULT_PROPS = { + actionCancelText: 'Cancel', + proceedButtonProps: { sx: undefined }, +}; + +const ConfirmDialog = ( + { + actionCancelText, + actionProceedText, + contentText, + dialogProps: { open }, + onCancel, + onProceed, + proceedButtonProps, + titleText, + }: ConfirmDialogProps = CONFIRM_DIALOG_DEFAULT_PROPS as ConfirmDialogProps, +): JSX.Element => { + const { sx: proceedButtonSx } = + proceedButtonProps ?? CONFIRM_DIALOG_DEFAULT_PROPS.proceedButtonProps; + + return ( + + + + + + :not(:first-child)': { + marginLeft: '.5em', + }, + }} + > + {actionCancelText} + + {actionProceedText} + + + + ); +}; + +ConfirmDialog.defaultProps = CONFIRM_DIALOG_DEFAULT_PROPS; + +export default ConfirmDialog; diff --git a/striker-ui/components/Files/FileEditForm.tsx b/striker-ui/components/Files/FileEditForm.tsx index 5dbd4bd1..c80e5761 100644 --- a/striker-ui/components/Files/FileEditForm.tsx +++ b/striker-ui/components/Files/FileEditForm.tsx @@ -9,6 +9,7 @@ import { Box, Checkbox, checkboxClasses } from '@mui/material'; import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import { GREY, RED, TEXT } from '../../lib/consts/DEFAULT_THEME'; +import ConfirmDialog from '../ConfirmDialog'; import ContainedButton from '../ContainedButton'; import FileInfo from './FileInfo'; import Spinner from '../Spinner'; @@ -29,6 +30,16 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { const [filesToEdit, setFilesToEdit] = useState([]); const [isLoadingFilesToEdit, setIsLoadingFilesToEdit] = useState(false); + const [isOpenPurgeConfirmDialog, setIsOpenConfirmPurgeDialog] = + useState(false); + const [selectedFilesCount, setSelectedFilesCount] = useState(0); + + const purgeButtonStyleOverride = { + backgroundColor: RED, + color: TEXT, + + '&:hover': { backgroundColor: RED }, + }; const generateFileInfoChangeHandler = (fileIndex: number): FileInfoChangeHandler => @@ -70,6 +81,8 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { }; const purgeFiles: MouseEventHandler = () => { + setIsOpenConfirmPurgeDialog(false); + filesToEdit .filter(({ isSelected }) => isSelected) .forEach(({ fileUUID }) => { @@ -77,6 +90,26 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { }); }; + const cancelPurge: MouseEventHandler = () => { + setIsOpenConfirmPurgeDialog(false); + }; + + const confirmPurge: MouseEventHandler = () => { + // We need this local variable because setState functions are async; the + // changes won't reflect until the next render cycle. + // In this case, the user would have to click on the purge button twice to + // trigger the confirmation dialog without using this local variable. + const localSelectedFilesCount = filesToEdit.filter( + ({ isSelected }) => isSelected, + ).length; + + setSelectedFilesCount(localSelectedFilesCount); + + if (localSelectedFilesCount > 0) { + setIsOpenConfirmPurgeDialog(true); + } + }; + useEffect(() => { setIsLoadingFilesToEdit(true); @@ -182,12 +215,8 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { }} > Purge @@ -195,6 +224,15 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { )} + )}