Skip to content

Commit 9fbe77e

Browse files
committed
Assign manager to STS and update sts done
1 parent 73a698b commit 9fbe77e

File tree

5 files changed

+278
-9
lines changed

5 files changed

+278
-9
lines changed

client/components/dataTables/STSList.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { EditUserModal } from "../modals/EditUserInfoModal";
4747
import gettAllRoles from "@/hooks/user_data/useGetAllRole";
4848
import { roleList } from "@/data/roles";
4949
import useGetAllSTS from "@/hooks/dataQuery/useGetAllSTS";
50+
import { EditSTSInfoModal } from "../modals/EditSTSInfoModal";
5051

5152
export type STS = {
5253
id: string;
@@ -57,7 +58,7 @@ export type STS = {
5758
longitude: string;
5859
};
5960

60-
export const columns: ColumnDef<STS>[] = [
61+
export const columns: ColumnDef<STS>[] = [
6162
{
6263
accessorKey: "name",
6364
header: ({ column }) => {
@@ -95,7 +96,9 @@ export const columns: ColumnDef<STS>[] = [
9596
);
9697
},
9798
cell: ({ row }) => (
98-
<div className="text-center font-medium">{row.getValue("wardNumber")}</div>
99+
<div className="text-center font-medium">
100+
{row.getValue("wardNumber")}
101+
</div>
99102
),
100103
},
101104
{
@@ -122,12 +125,12 @@ export const columns: ColumnDef<STS>[] = [
122125
id: "actions",
123126
enableHiding: false,
124127
cell: ({ row }) => {
125-
const user: STS = row.original;
128+
const sts: STS = row.original;
126129

127130
return (
128131
<div>
129-
{/* <DeleteUserModal userInfo={user} />
130-
<EditUserModal userInfo={user} /> */}
132+
{/* <DeleteUserModal userInfo={user} /> */}
133+
<EditSTSInfoModal stsInfo={sts} />
131134
</div>
132135
);
133136
},
@@ -136,10 +139,7 @@ export const columns: ColumnDef<STS>[] = [
136139

137140
export default function STSListTable() {
138141
const [data, setData] = React.useState<STS[]>([]);
139-
const {
140-
fetchAllSTS,
141-
stsData
142-
}= useGetAllSTS();
142+
const { fetchAllSTS, stsData } = useGetAllSTS();
143143
const [sorting, setSorting] = React.useState<SortingState>([]);
144144
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
145145
[]
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { Button } from "@/components/ui/button";
2+
import {
3+
Dialog,
4+
DialogContent,
5+
DialogDescription,
6+
DialogFooter,
7+
DialogHeader,
8+
DialogTitle,
9+
DialogTrigger,
10+
} from "@/components/ui/dialog";
11+
12+
import { Input } from "@/components/ui/input";
13+
import { Label } from "@/components/ui/label";
14+
import React, { use, useEffect, useState } from "react";
15+
import { EditIcon, Trash } from "lucide-react";
16+
import deleteUser from "@/hooks/user_data/deleteUser";
17+
import {
18+
Select,
19+
SelectTrigger,
20+
SelectValue,
21+
SelectContent,
22+
SelectGroup,
23+
SelectLabel,
24+
SelectItem,
25+
} from "../ui/select";
26+
import editUser from "@/hooks/user_data/editUser";
27+
import gettAllRoles from "@/hooks/user_data/useGetAllRole";
28+
import { number } from "prop-types";
29+
import { admin, landfillManager, stsManager, unassigned } from "@/data/roles";
30+
import editSTS from "@/hooks/entityCreation/editSTS";
31+
import getUserByRole from "@/hooks/user_data/getUserByRole";
32+
33+
export type STS = {
34+
id: string;
35+
name: string;
36+
wardNumber: string;
37+
capacity: string;
38+
latitude: string;
39+
longitude: string;
40+
};
41+
42+
type STSManager = {
43+
id: string;
44+
username: string;
45+
};
46+
47+
export const EditSTSInfoModal = ({ stsInfo }: { stsInfo: STS }) => {
48+
const [stsData, setSTSData] = useState(stsInfo);
49+
const [stsManagerData, setSTSManagerData] = useState<STSManager>();
50+
const [stsManagerList, setSTSManagerList] = useState<STSManager[]>([]);
51+
52+
const getManagerList = async () => {
53+
const result = await getUserByRole(stsManager);
54+
if (result) await setSTSManagerList(result);
55+
};
56+
57+
useEffect(() => {
58+
getManagerList();
59+
}, []);
60+
61+
return (
62+
<Dialog>
63+
<DialogTrigger asChild>
64+
<Button variant="ghost" title="Edit STS Info" className="h-8 w-8 p-0">
65+
<EditIcon className="h-4 w-4" />
66+
</Button>
67+
</DialogTrigger>
68+
<DialogContent className="max-w-[425px]">
69+
<DialogHeader>
70+
<DialogTitle className="mt-4 text-xl sm:text-2xl">
71+
Edit STS Details
72+
</DialogTitle>
73+
<DialogDescription>
74+
<div className="mt-4 flex flex-col justify-center items-start text-left p-4 rounded-lg border shadow-xl text-md">
75+
<h1>
76+
<span className="font-bold">ID: </span>
77+
{stsData.id}
78+
</h1>
79+
<p>
80+
<span className="font-bold">Name: </span>
81+
{stsData.name}
82+
</p>
83+
<p>
84+
<span className="font-bold">Capacity: </span>
85+
{stsData.capacity}
86+
</p>
87+
<p>
88+
<span className="font-bold">Ward: </span>
89+
{stsData.wardNumber}
90+
</p>
91+
</div>
92+
</DialogDescription>
93+
</DialogHeader>
94+
<form>
95+
<div className="grid gap-4 py-4">
96+
<div className="grid grid-cols-4 items-center gap-4">
97+
<Label htmlFor="stsName" className="text-right">
98+
STS Name
99+
</Label>
100+
<Input
101+
id="stsName"
102+
type="text"
103+
value={stsData.name}
104+
onChange={(e) =>
105+
setSTSData({ ...stsData, name: e.target.value })
106+
}
107+
className="col-span-3"
108+
required
109+
/>
110+
</div>
111+
<div className="grid grid-cols-4 items-center gap-4">
112+
<Label htmlFor="capacity" className="text-right">
113+
Capacity
114+
</Label>
115+
<Input
116+
id="capacity"
117+
type="number"
118+
value={stsData.capacity}
119+
onChange={(e) =>
120+
setSTSData({ ...stsData, capacity: e.target.value })
121+
}
122+
className="col-span-3"
123+
required
124+
/>
125+
</div>
126+
<div className="grid grid-cols-4 items-center gap-4">
127+
<Label htmlFor="ward" className="text-right">
128+
Ward No
129+
</Label>
130+
<Input
131+
id="capacity"
132+
type="number"
133+
value={stsData.wardNumber}
134+
onChange={(e) =>
135+
setSTSData({ ...stsData, wardNumber: e.target.value })
136+
}
137+
className="col-span-3"
138+
required
139+
/>
140+
</div>
141+
<div className="grid grid-cols-4 items-center gap-4">
142+
<Label htmlFor="Manager" className="text-right">
143+
Manager
144+
</Label>
145+
<Select
146+
value={stsManagerData?.id || ""}
147+
onValueChange={(e) => setSTSManagerData(stsManagerList.filter((user) => user.id === e)[0])}
148+
>
149+
<SelectTrigger className="col-span-3">
150+
<SelectValue id="role" placeholder="place holder" />
151+
</SelectTrigger>
152+
<SelectContent>
153+
<SelectGroup>
154+
<SelectLabel>Roles</SelectLabel>
155+
{stsManagerList.map((user) => (
156+
<SelectItem key={user.id} value={user.id}>
157+
{user.username}
158+
</SelectItem>
159+
))}
160+
</SelectGroup>
161+
</SelectContent>
162+
</Select>
163+
</div>
164+
</div>
165+
<DialogFooter>
166+
<Button
167+
type="submit"
168+
onClick={async () => {
169+
const result = await editSTS(stsData, stsManagerData?.id || "");
170+
if (result) return alert(result);
171+
}}
172+
>
173+
Update STS
174+
</Button>
175+
</DialogFooter>
176+
</form>
177+
</DialogContent>
178+
</Dialog>
179+
);
180+
};

client/data/apiRoutes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ export const apiRoutes = {
2323
delete: `${baseUrl}/landfills/`,
2424
edit: `${baseUrl}/landfills/`,
2525
},
26+
rbac: {
27+
create: `${baseUrl}/rbac/create`,
28+
getByRole: `${baseUrl}/rbac/roles/`,
29+
delete: `${baseUrl}/rbac/`,
30+
edit: `${baseUrl}/rbac/`,
31+
},
2632
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { User } from "@/components/dataTables/UserList";
2+
import { apiRoutes } from "@/data/apiRoutes";
3+
import { jwtToken } from "@/data/cookieNames";
4+
import { admin, landfillManager, stsManager, unassigned } from "@/data/roles";
5+
import { getCookie } from "@/lib/cookieFunctions";
6+
import axios from "axios";
7+
import { STS } from "@/components/modals/EditSTSInfoModal";
8+
9+
export default async function editSTS(stsData: STS, manager: string) {
10+
if (stsData && manager) {
11+
try {
12+
const res1 = await axios.put(
13+
apiRoutes.user.edit + manager,
14+
{
15+
stsId: stsData.id,
16+
},
17+
{
18+
headers: {
19+
Authorization: `Bearer ${await getCookie(jwtToken)}`,
20+
},
21+
}
22+
);
23+
const res2 = await axios.put(
24+
apiRoutes.sts.edit + stsData.id,
25+
{
26+
...stsData,
27+
},
28+
{
29+
headers: {
30+
Authorization: `Bearer ${await getCookie(jwtToken)}`,
31+
},
32+
}
33+
);
34+
return "sts updated successfully";
35+
} catch (error: any) {
36+
return (
37+
error.message?.toString() ||
38+
"error updating sts. You may not have the required permissions."
39+
);
40+
}
41+
}
42+
43+
return null;
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use client";
2+
import { UserData } from "@/components/graphs/Data";
3+
import { apiRoutes } from "@/data/apiRoutes";
4+
import { jwtToken } from "@/data/cookieNames";
5+
import { admin, landfillManager, stsManager, unassigned } from "@/data/roles";
6+
import { getCookie } from "@/lib/cookieFunctions";
7+
import axios from "axios";
8+
import { useState, useEffect, use } from "react";
9+
10+
type User = {
11+
id: string;
12+
username: string;
13+
email: string;
14+
role: string;
15+
};
16+
17+
export default async function getUserByRole(roleName: string) {
18+
19+
try {
20+
const res = await axios.get(apiRoutes.rbac.getByRole + roleName, {
21+
headers: {
22+
Authorization: `Bearer ${await getCookie(jwtToken)}`,
23+
},
24+
});
25+
const userList = res.data.map((user: any) => {
26+
return {
27+
id: user.id,
28+
username: user.username,
29+
};
30+
});
31+
console.log(userList);
32+
return userList;
33+
} catch (error: any) {
34+
alert("Error fetching user data... Are you authorized?");
35+
console.log(error.message);
36+
}
37+
38+
return [];
39+
}

0 commit comments

Comments
 (0)