|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useCallback, useMemo, useState } from "react"; |
| 4 | + |
| 5 | +import { type Schema } from "@/amplify/data/resource"; |
| 6 | +import { client } from "@/app/QueryProvider"; |
| 7 | +import tanstackTableHelper from "@/components/TanstackTableHelper"; |
| 8 | + |
| 9 | +import TableSearch from "../teams/components/TableSearch"; |
| 10 | +import TanstackTableBody from "../teams/components/TanstackTableBody"; |
| 11 | +import TableFooter from "../teams/components/TanstackTableFooter"; |
| 12 | +import TanstackTableHead from "../teams/components/TanstackTableHead"; |
| 13 | +import type { User } from "../users/UserTablePage"; |
| 14 | +import { usersColumns } from "./UsersTableSetup"; |
| 15 | + |
| 16 | +export default function UsersTable({ users }: { users: User[] }) { |
| 17 | + const [data, setData] = useState(users); |
| 18 | + const [globalFilter, setGlobalFilter] = useState(""); |
| 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 | + teamId: updatedData.teamId, |
| 28 | + }); |
| 29 | + }; |
| 30 | + const table = tanstackTableHelper({ |
| 31 | + data, |
| 32 | + columns: usersColumns, |
| 33 | + globalFilter, |
| 34 | + setGlobalFilter, |
| 35 | + deleteElement: deleteUser, |
| 36 | + updateElement: updateUser, |
| 37 | + setData, |
| 38 | + typeName: "User", |
| 39 | + }); |
| 40 | + return ( |
| 41 | + <div className="flex flex-1 flex-col justify-between rounded-3xl bg-white p-2 text-xl outline outline-awesomer-purple"> |
| 42 | + <div> |
| 43 | + <TableSearch |
| 44 | + tableDataLength={table.getRowCount()} |
| 45 | + handleSearchChange={useCallback( |
| 46 | + (value: string) => setGlobalFilter(value), |
| 47 | + [], |
| 48 | + )} |
| 49 | + /> |
| 50 | + <table className="w-full border-separate border-spacing-x-0.5 p-2"> |
| 51 | + <TanstackTableHead |
| 52 | + table={useMemo(() => table.getHeaderGroups(), [table])} |
| 53 | + /> |
| 54 | + <TanstackTableBody table={table} /> |
| 55 | + <tfoot> |
| 56 | + <tr> |
| 57 | + <th |
| 58 | + colSpan={table.getAllColumns().length} |
| 59 | + className="rounded-b-xl bg-awesome-purple p-4 text-white" |
| 60 | + /> |
| 61 | + </tr> |
| 62 | + </tfoot> |
| 63 | + </table> |
| 64 | + </div> |
| 65 | + <TableFooter table={table} /> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +} |
0 commit comments