-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDeleteRoleDialog.tsx
More file actions
72 lines (68 loc) · 1.66 KB
/
DeleteRoleDialog.tsx
File metadata and controls
72 lines (68 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use client";
import { useState } from "react";
import { Button } from "ui/components/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "ui/components/dialog";
import { useAction } from "next-safe-action/hooks";
import { deleteRole } from "@/actions/admin/role-actions";
import { toast } from "sonner";
import { UserWithRole } from "db/types";
import { InferSelectModel } from "drizzle-orm";
import { roles } from "db/schema";
type Role = InferSelectModel<typeof roles>;
export default function DeleteRoleDialog({
role,
class_name,
}: {
role: Role;
currentUser: UserWithRole;
class_name?: string;
}) {
const [open, setOpen] = useState(false);
const { execute: doDelete } = useAction(deleteRole, {
onError: ({ error }) => {
toast.error(
"Failed to delete role: " +
(error.serverError || "Unknown error"),
);
},
onSuccess: () => {
toast.success("Role deleted successfully");
setOpen(false);
},
});
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="destructive" className={class_name}>
Delete
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Delete Role</DialogTitle>
</DialogHeader>
<p>
Are you sure you want to delete the role "{role.name}"? This
action cannot be undone.
</p>
<div className="mt-4 flex justify-end gap-2">
<Button variant="outline" onClick={() => setOpen(false)}>
Cancel
</Button>
<Button
variant="destructive"
onClick={() => doDelete({ roleId: role.id })}
>
Delete
</Button>
</div>
</DialogContent>
</Dialog>
);
}