-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess-manager.tsx
More file actions
130 lines (119 loc) · 3.87 KB
/
Copy pathaccess-manager.tsx
File metadata and controls
130 lines (119 loc) · 3.87 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
"use client";
import axios from "axios";
import type { FC } from "react";
import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import { ArrowLeftRight } from "lucide-react";
import { Separator } from "./ui/separator";
import { CardContent } from "./ui/card";
import { ScrollArea } from "./ui/scroll-area";
import { Button } from "./ui/button";
import { toast } from "@/hooks/use-toast";
import { onMutationError } from "@/utils/mutation-error";
import { PermissionAddRequest } from "@/lib/validators/permission-add";
import { PermissionRemoveRequest } from "@/lib/validators/permission-remove";
import type { UserAccess } from "@prisma/client";
interface AccessManagerProps {
companies: number[];
userAccess: Pick<UserAccess, "companyId" | "id">[];
userId: string;
}
const AccessManager: FC<AccessManagerProps> = ({
companies,
userAccess,
userId,
}) => {
const router = useRouter();
const userCompanies = userAccess.map(({ companyId }) => companyId);
const filteredCompanies = companies.filter(
(company) => !userCompanies.includes(company)
);
const { mutate: addPermissions } = useMutation({
mutationFn: async ({ companyId, userId }: PermissionAddRequest) => {
const payload: PermissionAddRequest = {
companyId,
userId,
};
const { data } = await axios.post("/api/user/permission", payload);
return data;
},
onError: onMutationError,
onSuccess: () => {
toast({
description: "Permission has been added!",
});
router.refresh();
},
});
const { mutate: removePermissions } = useMutation({
mutationFn: async ({ id }: PermissionRemoveRequest) => {
const payload: PermissionRemoveRequest = { id };
const { data } = await axios.patch("/api/user/permission", payload);
return data;
},
onError: onMutationError,
onSuccess: () => {
toast({
description: "Permission has been removed!",
});
router.refresh();
},
});
return (
<>
<Separator />
<CardContent className="flex justify-between items-center">
<ScrollArea className="h-72 w-fit mt-6 rounded-md border">
<div className="p-4">
<h4 className="mb-4 text-sm font-medium leading-none">
Has access
</h4>
{userAccess.length === 0 ? (
<div className="text-muted-foreground text-sm">Nothing.</div>
) : (
userAccess.map(({ companyId, id }) => (
<>
<Button
variant="ghost"
size="xs"
key={companyId}
className="text-sm"
onClick={() => removePermissions({ id })}
>
{companyId}
</Button>
<Separator className="my-0.5" />
</>
))
)}
</div>
</ScrollArea>
<ArrowLeftRight />
<ScrollArea className="h-72 w-fit mt-6 rounded-md border">
<div className="p-4">
<h4 className="mb-4 text-sm font-medium leading-none">No access</h4>
{filteredCompanies.length === 0 ? (
<div className="text-muted-foreground text-sm">Nothing.</div>
) : (
filteredCompanies.map((companyId) => (
<>
<Button
variant="ghost"
size="xs"
key={companyId}
className="text-sm"
onClick={() => addPermissions({ userId, companyId })}
>
{companyId}
</Button>
<Separator className="my-0.5" />
</>
))
)}
</div>
</ScrollArea>
</CardContent>
</>
);
};
export default AccessManager;