Skip to content

Commit 1be22db

Browse files
committed
extract DeletionConfirmationDialog into a generic component
1 parent 8b9b8b3 commit 1be22db

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

app/(pages)/(main)/volunteering/menu/product.tsx

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import { MenuProduct } from "@prisma/client";
22
import { useEffect, useState } from "react";
3-
import {
4-
Button,
5-
Checkbox,
6-
Dialog,
7-
DialogActions,
8-
DialogContent,
9-
DialogContentText,
10-
DialogTitle,
11-
FormControlLabel,
12-
Grid,
13-
TextField,
14-
Tooltip
15-
} from "@mui/material";
3+
import { Button, Checkbox, FormControlLabel, Grid, TextField, Tooltip } from "@mui/material";
164
import { MenuProductCreate } from "@/app/api/v2/escape/menu/products/route";
175
import CircularProgress from "@mui/material/CircularProgress";
6+
import { DeletionConfirmationDialog } from "@/app/components/input/DeletionConfirmationDialog";
187

198
function updateProduct(product: MenuProduct, newAttributes: Partial<MenuProduct>): Promise<Response> {
209
return fetch("/api/v2/escape/menu/products", {
@@ -109,18 +98,23 @@ export function Product(props: { product: MenuProduct, onUpdate: () => void }) {
10998
Delete
11099
</Button>
111100

112-
<DeletionConfirmationDialog open={ deleteDialogOpen } onClose={ () => setDeleteDialogOpen(false) }
113-
onDelete={ () => {
114-
setIsDeleting(true);
115-
deleteProduct(props.product.id).then(() => {
116-
setIsDeleting(false);
117-
setDeleteDialogOpen(false);
118-
119-
props.onUpdate();
120-
});
121-
} }
122-
isDeleting={ isDeleting }
123-
/>
101+
<DeletionConfirmationDialog
102+
open={ deleteDialogOpen }
103+
onClose={ () => setDeleteDialogOpen(false) }
104+
onDelete={ () => {
105+
setIsDeleting(true);
106+
deleteProduct(props.product.id).then(() => {
107+
setIsDeleting(false);
108+
setDeleteDialogOpen(false);
109+
110+
props.onUpdate();
111+
});
112+
} }
113+
showSpinner={ isDeleting }
114+
title={ "Delete product?" }
115+
>
116+
Are you sure you want to delete this product?
117+
</DeletionConfirmationDialog>
124118
</Grid>
125119
</>
126120
)
@@ -354,36 +348,4 @@ export function NewProduct(props: { onUpdate: () => void, categoryId: number | n
354348
</Grid>
355349
</>
356350
);
357-
}
358-
359-
// Dialog to confirm deletion of a product.
360-
function DeletionConfirmationDialog(props: {
361-
open: boolean,
362-
onClose: () => void, // called when "Cancel" is clicked, or the dialog is closed in any other way.
363-
onDelete: () => void, // called when "Delete" is clicked.
364-
isDeleting: boolean // when true, shows a spinner instead of "Delete" in the button.
365-
}) {
366-
return (
367-
<Dialog
368-
open={ props.open }
369-
onClose={ props.onClose }
370-
aria-labelledby="alert-dialog-title"
371-
aria-describedby="alert-dialog-description"
372-
>
373-
<DialogTitle id="alert-dialog-title">
374-
Delete product?
375-
</DialogTitle>
376-
<DialogContent>
377-
<DialogContentText id="alert-dialog-description">
378-
Are you sure you want to delete the product?
379-
</DialogContentText>
380-
</DialogContent>
381-
<DialogActions>
382-
<Button onClick={ props.onClose }>Cancel</Button>
383-
<Button onClick={ props.onDelete } color="error">
384-
{ props.isDeleting ? <CircularProgress/> : <>Delete</> }
385-
</Button>
386-
</DialogActions>
387-
</Dialog>
388-
);
389351
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from "@mui/material";
2+
import CircularProgress from "@mui/material/CircularProgress";
3+
import { ReactNode } from "react";
4+
5+
export type DeletionConfirmationDialogProps = {
6+
open: boolean,
7+
onClose: () => void; // called when "Cancel" is clicked, or the dialog is closed in any other way.
8+
onDelete: () => void; // called when "Delete" is clicked.
9+
showSpinner: boolean; // when true, shows a spinner instead of "Delete" in the button.
10+
11+
title: ReactNode; // Title of the dialog
12+
children: ReactNode; // content
13+
}
14+
15+
export function DeletionConfirmationDialog(
16+
props: DeletionConfirmationDialogProps
17+
) {
18+
console.log(props)
19+
20+
return (
21+
<Dialog
22+
open={ props.open }
23+
onClose={ props.onClose }
24+
aria-labelledby="alert-dialog-title"
25+
aria-describedby="alert-dialog-description"
26+
>
27+
<DialogTitle id="alert-dialog-title">
28+
{ props.title }
29+
</DialogTitle>
30+
<DialogContent>
31+
<DialogContentText id="alert-dialog-description">
32+
{ props.children }
33+
</DialogContentText>
34+
</DialogContent>
35+
<DialogActions>
36+
<Button onClick={ props.onClose }>Cancel</Button>
37+
<Button onClick={ props.onDelete } color="error">
38+
{ props.showSpinner ? <CircularProgress/> : <>Delete</> }
39+
</Button>
40+
</DialogActions>
41+
</Dialog>
42+
)
43+
}

0 commit comments

Comments
 (0)