-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCreateRoleDialog.tsx
More file actions
177 lines (170 loc) · 4.65 KB
/
CreateRoleDialog.tsx
File metadata and controls
177 lines (170 loc) · 4.65 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"use client";
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "ui/components/dialog";
import { Button } from "ui/components/button";
import { Input } from "ui/components/input";
import { Label } from "ui/components/label";
import { PermissionType } from "@/lib/constants/permission";
import { PermissionMask } from "@/lib/utils/shared/permission";
import { useAction } from "next-safe-action/hooks";
import { createRole } from "@/actions/admin/role-actions";
import { toast } from "sonner";
import Restricted from "@/components/Restricted";
import { UserWithRole } from "db/types";
import { userHasPermission } from "@/lib/utils/server/admin";
export default function CreateRoleDialog({
currentUser,
nextPosition,
}: {
currentUser: UserWithRole;
nextPosition: number;
}) {
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [permissionsMask, setPermissionsMask] = useState(0);
const [color, setColor] = useState("#000000");
const { execute: doCreate } = useAction(createRole, {
onError: ({ error }) => {
toast.error(
"Failed to create role: " +
(error.serverError || "Unknown error"),
);
},
onSuccess: () => {
toast.success("Role created");
setOpen(false);
setName("");
setPermissionsMask(0);
setColor("#000000");
},
});
const permissionEntries = Object.keys(PermissionType)
.filter((k) => isNaN(Number(k)))
.map((k) => ({
name: k.replaceAll("_", " "),
value: (PermissionType as any)[k] as number,
}));
const hasPermLocal = (perm: PermissionType) => {
const mask = new PermissionMask(permissionsMask);
return mask.has(perm);
};
// Check if user can assign this permission (they must have it themselves)
const canAssignPermission = (perm: PermissionType) => {
return userHasPermission(currentUser, perm);
};
const togglePerm = (perm: PermissionType) => {
if (!canAssignPermission(perm)) return;
setPermissionsMask(new PermissionMask(permissionsMask).toggle(perm));
};
const handleCreate = () => {
if (!name.trim()) {
toast.error("Role name is required");
return;
}
doCreate({
name: name.trim(),
position: nextPosition,
permissions: permissionsMask,
color: color || undefined,
});
};
return (
<Restricted
user={currentUser}
permissions={PermissionType.CREATE_ROLES}
>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button>Create New Role</Button>
</DialogTrigger>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Create New Role</DialogTitle>
</DialogHeader>
<div className="grid grid-cols-2 gap-12">
<div>
<div className="mb-4">
<Label htmlFor="name">Role Name</Label>
<Input
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter role name"
/>
</div>
<h4 className="mb-2 font-semibold">Permissions</h4>
<div className="space-y-2">
{permissionEntries.map((p) => (
<div
key={p.name}
className="flex items-center justify-between"
>
<div className="flex items-center gap-x-2">
<input
type="checkbox"
checked={hasPermLocal(p.value)}
disabled={
!canAssignPermission(
p.value,
)
}
onChange={() =>
togglePerm(p.value)
}
/>
<span
className={
!canAssignPermission(
p.value,
)
? "text-muted-foreground"
: ""
}
>
{p.name}
</span>
</div>
<div className="text-sm text-muted-foreground">
{hasPermLocal(p.value)
? "Enabled"
: "Disabled"}
{!canAssignPermission(p.value) &&
" (You can't assign this permission)"}
</div>
</div>
))}
</div>
</div>
<div>
<h4 className="mb-2 font-semibold">Appearance</h4>
<div className="flex items-center gap-x-2">
<Label htmlFor="color">Color</Label>
<input
id="color"
type="color"
value={color}
onChange={(e) => setColor(e.target.value)}
/>
</div>
</div>
</div>
<div className="mt-4 flex justify-end gap-2">
<Button
variant="outline"
onClick={() => setOpen(false)}
>
Cancel
</Button>
<Button onClick={handleCreate}>Create Role</Button>
</div>
</DialogContent>
</Dialog>
</Restricted>
);
}