-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRolesManager.tsx
More file actions
150 lines (137 loc) · 3.79 KB
/
RolesManager.tsx
File metadata and controls
150 lines (137 loc) · 3.79 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
"use client";
import { useState, useEffect } from "react";
import RoleCard from "@/components/admin/roles/RoleCard";
import { Accordion } from "ui/components/accordion";
import { Button } from "ui/components/button";
import { useAction } from "next-safe-action/hooks";
import { editRole } from "@/actions/admin/role-actions";
import { toast } from "sonner";
import CreateRoleDialog from "@/components/admin/roles/CreateRoleDialog";
import Restricted from "@/components/Restricted";
import { PermissionType } from "@/lib/constants/permission";
export default function RolesManager({
roles: initialRoles,
currentUser,
}: {
roles: any[];
currentUser: any;
}) {
const [roles, setRoles] = useState(() =>
[...initialRoles].sort((a, b) => a.position - b.position),
);
const [dirty, setDirty] = useState(false);
const { execute: doEdit } = useAction(editRole, {
onError: ({ error }) => {
toast.error(
"Failed to save role: " +
(error.serverError || "Unknown error"),
);
},
onSuccess: () => {
// Individual success not needed since we save all at once
},
});
useEffect(() => {
setRoles([...initialRoles].sort((a, b) => a.position - b.position));
}, [initialRoles]);
const nextPosition =
roles.length > 0 ? Math.max(...roles.map((r) => r.position)) + 1 : 1;
function onRoleChange(updated: any) {
setRoles((prev) => {
const next = prev.map((r) =>
r.id === updated.id ? { ...r, ...updated } : r,
);
setDirty(true);
return next;
});
}
function onMove(roleId: number, dir: "up" | "down") {
setRoles((prev) => {
const idx = prev.findIndex((r) => r.id === roleId);
if (idx === -1) return prev;
const next = [...prev];
const swapIdx = dir === "up" ? idx - 1 : idx + 1;
if (swapIdx < 0 || swapIdx >= next.length) return prev;
// swap positions
const a = next[idx];
const b = next[swapIdx];
const aPos = a.position;
next[idx] = { ...a, position: b.position };
next[swapIdx] = { ...b, position: aPos };
setDirty(true);
// resort after swap
return next.sort((a, b) => a.position - b.position);
});
}
const total = roles.length;
async function saveAll() {
// Compute which roles actually changed compared to the original `initialRoles`
const changedRoles = roles.filter((r) => {
const orig = initialRoles.find((o: any) => o.id === r.id);
if (!orig) return true;
const origPerm = orig.permissions ?? 0;
const rPerm = r.permissions ?? 0;
const origColor = orig.color ?? null;
const rColor = r.color ?? null;
return (
orig.name !== r.name ||
orig.position !== r.position ||
origPerm !== rPerm ||
origColor !== rColor
);
});
if (changedRoles.length === 0) {
toast.success("No changes to save");
setDirty(false);
return;
}
const promises = changedRoles.map((r) =>
doEdit({
roleId: r.id,
name: r.name,
position: r.position,
permissions: r.permissions,
color: r.color ?? null,
}),
);
try {
await Promise.all(promises);
toast.success("All changes saved successfully");
setDirty(false);
} catch (e: any) {
// Errors are handled by onError callback
console.error(e);
}
}
return (
<div>
<div className="mb-4 flex justify-end">
<Restricted
user={currentUser}
permissions={PermissionType.CREATE_ROLES}
>
<CreateRoleDialog
currentUser={currentUser}
nextPosition={nextPosition}
/>
</Restricted>
</div>
<Accordion type="multiple" className="w-full">
{roles.map((r, i) => (
<RoleCard
key={r.id}
role={r}
currentUser={currentUser}
onRoleChange={onRoleChange}
onMove={onMove}
index={i}
total={total}
/>
))}
</Accordion>
<div className="fixed bottom-0 right-0 m-8">
{dirty ? <Button onClick={saveAll}>Save Changes</Button> : null}
</div>
</div>
);
}