Skip to content

Commit e6dad7b

Browse files
committed
add ability to delete categories
1 parent 1be22db commit e6dad7b

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MenuCategory } from "@prisma/client";
88
import { NewProduct, Product } from "@/app/(pages)/(main)/volunteering/menu/product";
99
import { styled } from "@mui/system";
1010
import CircularProgress from "@mui/material/CircularProgress";
11+
import { DeletionConfirmationDialog } from "@/app/components/input/DeletionConfirmationDialog";
1112

1213

1314
// Updates a given category.
@@ -66,6 +67,10 @@ export function Category(
6667
let [isUpdating, setIsUpdating] = useState<boolean>(false);
6768

6869

70+
let [deleteDialogOpen, setDeleteDialogOpen] = useState<boolean>(false);
71+
let [isDeleting, setIsDeleting] = useState<boolean>(false);
72+
73+
6974
useEffect(() => {
7075
// useEffect is always run once at the start.
7176
if (isFirst) {
@@ -81,7 +86,7 @@ export function Category(
8186
<Stack>
8287
<Grid container spacing={ 2 } columns={ 10 }>
8388

84-
<Grid item xs={ 9 }>
89+
<Grid item xs={ 8 }>
8590
<LargeTextField
8691
type="text"
8792
value={ categoryName }
@@ -120,6 +125,17 @@ export function Category(
120125
}
121126
</Button>
122127
</Grid>
128+
129+
<Grid item xs={ 1 } display="flex" alignItems="center">
130+
<Button
131+
color="error"
132+
onClick={ () => {
133+
setDeleteDialogOpen(true);
134+
} }
135+
>
136+
Delete
137+
</Button>
138+
</Grid>
123139
</Grid>
124140

125141

@@ -147,6 +163,21 @@ export function Category(
147163

148164
<NewProduct onUpdate={ props.onUpdate } categoryId={ props.category.id }></NewProduct>
149165
</Grid>
166+
167+
<DeletionConfirmationDialog
168+
open={ deleteDialogOpen }
169+
onClose={ () => setDeleteDialogOpen(false) }
170+
onDelete={ () => {
171+
setIsDeleting(true);
172+
deleteCategory(props.category.id).then(() => {
173+
props.onUpdate();
174+
});
175+
} }
176+
showSpinner={ isDeleting }
177+
title="Delete category?"
178+
>
179+
Are you sure you want to delete this category?
180+
</DeletionConfirmationDialog>
150181
</Stack>
151182
)
152183
}
@@ -187,9 +218,7 @@ export function NewCategory(props: { onUpdate: () => void }) {
187218
setIsCreating(false);
188219
props.onUpdate();
189220
});
190-
}
191-
192-
}
221+
} }
193222
>{ isCreating ? <CircularProgress/> : <>Create</> }</Button>
194223

195224

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { getServerSession } from "next-auth";
3+
import { Auth } from "@/app/api/utils/auth";
4+
import prisma from "@/prisma/prismaClient";
5+
import { authOptions } from "@/app/api/utils/authOptions";
6+
7+
export async function DELETE(
8+
_req: NextRequest,
9+
context: { params: Promise<{ categoryId: string }> }
10+
) {
11+
const params = await context.params;
12+
13+
let session = await getServerSession(authOptions);
14+
let auth = new Auth(session, params)
15+
.requireRoles([])
16+
.requireParams(["categoryId"]);
17+
18+
if (auth.failed) return auth.response;
19+
20+
let categoryId = Number(params.categoryId);
21+
// verify productId is an integer
22+
if (isNaN(categoryId) || !categoryId) {
23+
return NextResponse.json({error: "categoryId must be an integer"}, {status: 400});
24+
}
25+
26+
27+
await prisma.$transaction(async () => {
28+
await prisma.menuProduct.deleteMany({
29+
where: {
30+
category_id: categoryId
31+
}
32+
});
33+
34+
await prisma.menuCategory.delete(
35+
{
36+
where: {
37+
id: categoryId,
38+
},
39+
}
40+
);
41+
});
42+
43+
return NextResponse.json({})
44+
}

0 commit comments

Comments
 (0)