Skip to content

Commit 3c1550b

Browse files
committed
feat: complete confirmation dialog of cart page
1 parent 3db0ac8 commit 3c1550b

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

src/app/[locale]/(main)/(container)/cart/components/Header.tsx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Dialog, { DialogProps } from '@/components/Dialog/Dialog';
2+
import useNewDialog from '@/components/Dialog/hooks/useNewDialog';
13
import useEmptyCart from '@/hooks/useEmptyCart';
24
import { cartAtom } from '@/store/atoms';
35
import { DeleteOutline, MoreVert } from '@mui/icons-material';
@@ -10,7 +12,6 @@ import {
1012
MenuItem,
1113
} from '@mui/material';
1214
import { useAtomValue } from 'jotai';
13-
import { useConfirm } from 'material-ui-confirm';
1415
import { useTranslations } from 'next-intl';
1516
import React, { useState } from 'react';
1617

@@ -29,19 +30,47 @@ const Header = () => {
2930

3031
const { emptyCartLoading, emptyCartMutate } = useEmptyCart();
3132

32-
const confirm = useConfirm();
33-
const handleClickOnRemoveAll = () => {
34-
confirm({
35-
title: t('pages.cart.confirm.removeAllTitle'),
36-
description: t('pages.cart.confirm.removeAllDescription'),
37-
confirmationText: t('buttons.removeAll'),
38-
}).then(async () => {
39-
await emptyCartMutate();
40-
});
33+
const { open, handleCloseDialog, handleOpenDialog, returnButton } =
34+
useNewDialog();
35+
36+
const handleClickOnConfirm = async () => {
37+
await emptyCartMutate();
38+
};
39+
40+
const onCloseDialog: DialogProps['onClose'] = (event, reason) => {
41+
if (!emptyCartLoading) {
42+
handleCloseDialog();
43+
}
4144
};
4245

4346
return (
4447
<>
48+
<Dialog
49+
title={t('pages.cart.confirm.removeAllTitle')}
50+
open={open}
51+
closeButtonDisabled={emptyCartLoading}
52+
onClose={onCloseDialog}
53+
PaperProps={{
54+
sx: {
55+
width: 500,
56+
},
57+
}}
58+
dialogContentProps={{
59+
dividers: true,
60+
}}
61+
buttons={[
62+
returnButton,
63+
{
64+
children: t('buttons.removeAll'),
65+
onClick: handleClickOnConfirm,
66+
isLoading: emptyCartLoading,
67+
variant: 'contained',
68+
color: 'primary',
69+
},
70+
]}
71+
>
72+
{t('pages.cart.confirm.removeAllDescription')}
73+
</Dialog>
4574
<Menu
4675
id="basic-menu"
4776
anchorEl={anchorEl}
@@ -51,7 +80,7 @@ const Header = () => {
5180
'aria-labelledby': 'basic-button',
5281
}}
5382
>
54-
<MenuItem onClick={handleClickOnRemoveAll}>
83+
<MenuItem onClick={handleOpenDialog}>
5584
<ListItemIcon>
5685
<DeleteOutline fontSize="small" />
5786
</ListItemIcon>

src/components/Dialog/Dialog.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ import DialogTransition from '../common/DialogTransition';
1414
export interface DialogProps extends MuiDialogProps {
1515
dialogContentProps?: DialogContentProps;
1616
buttons?: ButtonWithLoadingProps[];
17+
closeButtonDisabled?: boolean;
1718
}
1819

19-
const Dialog: FC<DialogProps> = ({ title, buttons = [], ...props }) => {
20+
const Dialog: FC<DialogProps> = ({
21+
title,
22+
buttons = [],
23+
closeButtonDisabled = false,
24+
...props
25+
}) => {
2026
return (
2127
<MuiDialog TransitionComponent={DialogTransition} {...props}>
2228
<DialogTitle
@@ -27,14 +33,16 @@ const Dialog: FC<DialogProps> = ({ title, buttons = [], ...props }) => {
2733
}}
2834
>
2935
{title}
30-
<IconButton
31-
aria-label="close-dialog"
32-
onClick={() => {
33-
props.onClose?.({}, 'escapeKeyDown');
34-
}}
35-
>
36-
<Close />
37-
</IconButton>
36+
{!closeButtonDisabled && (
37+
<IconButton
38+
aria-label="close-dialog"
39+
onClick={() => {
40+
props.onClose?.({}, 'escapeKeyDown');
41+
}}
42+
>
43+
<Close />
44+
</IconButton>
45+
)}
3846
</DialogTitle>
3947
<DialogContent {...props.dialogContentProps}>
4048
<Box mt={1}>{props.children}</Box>

src/components/Dialog/hooks/useNewDialog.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { ButtonWithLoadingProps } from '@/components/common/ButtonWithLoading';
2+
import { useTranslations } from 'next-intl';
13
import { useState } from 'react';
24

35
export interface IUseNewDialog {
46
(): {
57
open: boolean;
68
handleOpenDialog: () => void;
79
handleCloseDialog: () => void;
10+
returnButton: ButtonWithLoadingProps;
811
};
912
}
1013
const useNewDialog: IUseNewDialog = () => {
@@ -17,7 +20,17 @@ const useNewDialog: IUseNewDialog = () => {
1720
const handleCloseDialog = () => {
1821
setOpen(false);
1922
};
20-
return { open, handleOpenDialog, handleCloseDialog };
23+
24+
const t = useTranslations();
25+
26+
const returnButton: ButtonWithLoadingProps = {
27+
children: t('buttons.return'),
28+
variant: 'outlined',
29+
color: 'inherit',
30+
onClick: handleCloseDialog,
31+
};
32+
33+
return { open, handleOpenDialog, handleCloseDialog, returnButton };
2134
};
2235

2336
export default useNewDialog;

src/providers/ConfirmAlertProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ConfirmAlertProvider: FC<PropsWithChildren> = ({ children }) => {
1111
const defaultOptions: ConfirmProviderProps['defaultOptions'] = {
1212
confirmationButtonProps: {
1313
variant: 'contained',
14-
color: 'error',
14+
color: 'primary',
1515
},
1616
cancellationButtonProps: {
1717
variant: 'outlined',

0 commit comments

Comments
 (0)