Skip to content

Commit 2be6670

Browse files
committed
Refactor Admin Users Table
1 parent ce35585 commit 2be6670

16 files changed

+273
-550
lines changed

src/app/admin/components/FilterUser.tsx

-65
This file was deleted.

src/app/admin/components/PopupTileUser.tsx

-170
This file was deleted.

src/app/admin/components/UsersTable.tsx

+19-85
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,45 @@
11
"use client";
22

33
import { useCallback, useMemo, useState } from "react";
4-
import { toast } from "react-toastify";
54

65
import { type Schema } from "@/amplify/data/resource";
76
import { client } from "@/app/QueryProvider";
87
import tanstackTableHelper from "@/components/TanstackTableHelper";
9-
import { useMutation, useQueryClient } from "@tanstack/react-query";
108

11-
import SearchTeam from "../teams/components/SearchTeam";
9+
import TableSearch from "../teams/components/TableSearch";
1210
import TanstackTableBody from "../teams/components/TanstackTableBody";
1311
import TableFooter from "../teams/components/TanstackTableFooter";
1412
import TanstackTableHead from "../teams/components/TanstackTableHead";
15-
import { type User, usersColumns } from "./UsersTableSetup";
13+
import type { User } from "../users/UserTablePage";
14+
import { usersColumns } from "./UsersTableSetup";
1615

1716
export default function UsersTable({ users }: { users: User[] }) {
1817
const [data, setData] = useState(users);
1918
const [globalFilter, setGlobalFilter] = useState("");
20-
const queryClient = useQueryClient();
21-
const deleteParticipant = useMutation({
22-
mutationFn: async ({
23-
rowIndex,
24-
participantID,
25-
}: {
26-
rowIndex: number;
27-
participantID: Schema["User"]["deleteType"];
28-
}) => {
29-
const prev = data;
30-
setData((old) => old.filter((_, index) => index !== rowIndex));
31-
try {
32-
const response = await client.models.User.delete(participantID);
33-
if (response.errors) {
34-
throw new Error(response.errors[0].message);
35-
}
36-
} catch (error) {
37-
setData(prev);
38-
throw error;
39-
}
40-
return participantID;
41-
},
42-
onError: (error) => {
43-
toast.error("Error updating teams: " + error.message);
44-
},
45-
onSuccess: (teamID) => {
46-
queryClient.invalidateQueries({ queryKey: ["Teams"] });
47-
toast.success(`Team ${teamID.id} deleted succesfully`);
48-
},
49-
});
50-
const updateParticipant = useMutation({
51-
mutationFn: async (updatedData: Schema["User"]["updateType"]) => {
52-
try {
53-
const response = await client.models.User.update(updatedData);
54-
if (response.errors) {
55-
throw new Error(response.errors[0].message);
56-
}
57-
} catch (error) {
58-
throw error;
59-
}
60-
},
61-
onSuccess: () => {
62-
queryClient.invalidateQueries({ queryKey: ["Participant"] });
63-
toast.success("Table data updated succesfully");
64-
},
65-
onError: () => {
66-
toast.error("Error updating participant");
67-
},
68-
});
19+
const deleteUser = async (id: Schema["User"]["deleteType"]) =>
20+
client.models.User.delete(id);
21+
const updateUser = async (updatedData: Schema["User"]["updateType"]) => {
22+
return client.models.User.update({
23+
id: updatedData.id,
24+
firstName: updatedData.firstName,
25+
lastName: updatedData.lastName,
26+
role: updatedData.role,
27+
});
28+
};
6929
const table = tanstackTableHelper({
7030
data,
71-
columnData: usersColumns,
72-
meta: {
73-
updateData: (rowIndex, columnId, value) => {
74-
setData((old) =>
75-
old.map((row, index) => {
76-
if (index !== rowIndex) return row;
77-
return {
78-
...old[rowIndex]!,
79-
[columnId]: value,
80-
};
81-
}),
82-
);
83-
},
84-
deleteData: (participant, rowIndex) => {
85-
const participantID = {
86-
id: participant.id,
87-
};
88-
deleteParticipant.mutate({ participantID, rowIndex });
89-
},
90-
saveData: (participant) => {
91-
const updatedData = {
92-
id: participant.id,
93-
name: participant.firstName,
94-
lastName: participant.lastName,
95-
role: participant.role,
96-
teamId: participant.teamId,
97-
email: participant.email,
98-
};
99-
updateParticipant.mutate(updatedData);
100-
},
101-
},
31+
columns: usersColumns,
10232
globalFilter,
10333
setGlobalFilter,
34+
deleteElement: deleteUser,
35+
updateElement: updateUser,
36+
setData,
37+
typeName: "User",
10438
});
10539
return (
10640
<div className="flex flex-1 flex-col justify-between rounded-3xl bg-white p-2 text-xl outline outline-awesomer-purple">
10741
<div>
108-
<SearchTeam
42+
<TableSearch
10943
tableDataLength={table.getRowCount()}
11044
handleSearchChange={useCallback(
11145
(value: string) => setGlobalFilter(value),

0 commit comments

Comments
 (0)