|
1 | 1 | "use client";
|
2 | 2 |
|
3 | 3 | import { useCallback, useMemo, useState } from "react";
|
4 |
| -import { toast } from "react-toastify"; |
5 | 4 |
|
6 | 5 | import { type Schema } from "@/amplify/data/resource";
|
7 | 6 | import { client } from "@/app/QueryProvider";
|
8 | 7 | import tanstackTableHelper from "@/components/TanstackTableHelper";
|
9 |
| -import { useMutation, useQueryClient } from "@tanstack/react-query"; |
10 | 8 |
|
11 |
| -import SearchTeam from "../teams/components/SearchTeam"; |
| 9 | +import TableSearch from "../teams/components/TableSearch"; |
12 | 10 | import TanstackTableBody from "../teams/components/TanstackTableBody";
|
13 | 11 | import TableFooter from "../teams/components/TanstackTableFooter";
|
14 | 12 | 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"; |
16 | 15 |
|
17 | 16 | export default function UsersTable({ users }: { users: User[] }) {
|
18 | 17 | const [data, setData] = useState(users);
|
19 | 18 | 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 | + }; |
69 | 29 | const table = tanstackTableHelper({
|
70 | 30 | 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, |
102 | 32 | globalFilter,
|
103 | 33 | setGlobalFilter,
|
| 34 | + deleteElement: deleteUser, |
| 35 | + updateElement: updateUser, |
| 36 | + setData, |
| 37 | + typeName: "User", |
104 | 38 | });
|
105 | 39 | return (
|
106 | 40 | <div className="flex flex-1 flex-col justify-between rounded-3xl bg-white p-2 text-xl outline outline-awesomer-purple">
|
107 | 41 | <div>
|
108 |
| - <SearchTeam |
| 42 | + <TableSearch |
109 | 43 | tableDataLength={table.getRowCount()}
|
110 | 44 | handleSearchChange={useCallback(
|
111 | 45 | (value: string) => setGlobalFilter(value),
|
|
0 commit comments