|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 5 | +import { PersonWithRoles } from "#/lib/types/people"; |
| 6 | +import { CellContext, createColumnHelper, getCoreRowModel, TableOptions } from "@tanstack/table-core"; |
| 7 | +import { Table } from "#/components/cms/table/Table"; |
| 8 | +import { PhoneCell } from "#/components/cms/table/cell/Phone"; |
| 9 | +import { MailCell } from "#/components/cms/table/cell/Mail"; |
| 10 | +import { RolesCell } from "#/components/cms/table/cell/Roles"; |
| 11 | +import { FaRegEdit } from "react-icons/fa"; |
| 12 | +import { CardToolbar } from "#/components/cms/card/Card"; |
| 13 | +import { debounce } from "lodash"; |
| 14 | +import { LuSearch } from "react-icons/lu"; |
| 15 | +import { MdOutlineDelete, MdOutlinePersonAdd } from "react-icons/md"; |
| 16 | +import { Button, IconButton, LinearProgress } from "@mui/material"; |
| 17 | +import { TextField } from "#/components/cms/input/TextField"; |
| 18 | +import { deletePerson, readAllPeople } from "#/app/(cms)/cms/people/actions"; |
| 19 | +import { useRouter } from "next/navigation"; |
| 20 | +import { useHotkeys } from "react-hotkeys-hook"; |
| 21 | + |
| 22 | +const COLUMN_HELPER = createColumnHelper<PersonWithRoles>(); |
| 23 | + |
| 24 | +const COLUMN_FIRSTNAME = COLUMN_HELPER.accessor("firstName", { |
| 25 | + header: "Vorname", |
| 26 | + id: "firstName", |
| 27 | +}); |
| 28 | + |
| 29 | +const COLUMN_LASTNAME = COLUMN_HELPER.accessor("lastName", { |
| 30 | + header: "Nachname", |
| 31 | + id: "lastName", |
| 32 | +}); |
| 33 | + |
| 34 | +const COLUMN_EMAIL = COLUMN_HELPER.accessor("email", { |
| 35 | + header: "E-Mail", |
| 36 | + id: "email", |
| 37 | + cell: MailCell, |
| 38 | +}); |
| 39 | + |
| 40 | +const COLUMN_PHONE = COLUMN_HELPER.accessor("phone", { |
| 41 | + header: "Phone", |
| 42 | + id: "phone", |
| 43 | + cell: PhoneCell, |
| 44 | +}); |
| 45 | + |
| 46 | +const COLUMN_ROLES = COLUMN_HELPER.accessor((person) => person.peopleToRoles.map((p) => p.roles.name), { |
| 47 | + header: "Rollen", |
| 48 | + id: "roles", |
| 49 | + cell: RolesCell, |
| 50 | +}); |
| 51 | + |
| 52 | +export default function PeopleList() { |
| 53 | + const [people, setPeople] = useState<PersonWithRoles[] | undefined>(undefined); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + async function fetchAllPeople() { |
| 57 | + const response = await fetch("/cms/api/people"); |
| 58 | + const data = await response.json(); |
| 59 | + setPeople(data.people); |
| 60 | + } |
| 61 | + fetchAllPeople(); |
| 62 | + }, []); |
| 63 | + |
| 64 | + const router = useRouter(); |
| 65 | + |
| 66 | + const [searchTerm, setSearchTerm] = useState<string | null>(); |
| 67 | + const filteredPeople = useMemo(() => { |
| 68 | + if (people === undefined) return []; |
| 69 | + |
| 70 | + if (!searchTerm || searchTerm.trim() === "") return people; |
| 71 | + |
| 72 | + return people.filter((person) => { |
| 73 | + const firstName = person.firstName?.toLowerCase(); |
| 74 | + const lastName = person.lastName?.toLowerCase(); |
| 75 | + |
| 76 | + if (firstName?.includes(searchTerm)) return true; |
| 77 | + if (lastName?.includes(searchTerm)) return true; |
| 78 | + if (`${firstName} ${lastName}`.includes(searchTerm)) return true; |
| 79 | + if (person.email?.toLowerCase().includes(searchTerm)) return true; |
| 80 | + return person.peopleToRoles.some((p) => p.roles.name?.toLowerCase().includes(searchTerm) ?? false); |
| 81 | + }); |
| 82 | + }, [searchTerm, people]); |
| 83 | + |
| 84 | + const options: TableOptions<PersonWithRoles> = useMemo( |
| 85 | + () => ({ |
| 86 | + data: filteredPeople, |
| 87 | + columns: [ |
| 88 | + COLUMN_LASTNAME, |
| 89 | + COLUMN_FIRSTNAME, |
| 90 | + COLUMN_EMAIL, |
| 91 | + COLUMN_PHONE, |
| 92 | + COLUMN_ROLES, |
| 93 | + COLUMN_HELPER.display({ |
| 94 | + header: "", |
| 95 | + id: "actions", |
| 96 | + cell: (props) => ( |
| 97 | + <Actions |
| 98 | + {...props} |
| 99 | + onDelete={(personId) => setPeople((prev) => prev?.filter((person) => person.id !== personId))} |
| 100 | + /> |
| 101 | + ), |
| 102 | + }), |
| 103 | + ], |
| 104 | + initialState: { |
| 105 | + sorting: [{ id: "lastName", desc: false }], |
| 106 | + }, |
| 107 | + getRowId: (person) => person.id, |
| 108 | + getCoreRowModel: getCoreRowModel(), |
| 109 | + }), |
| 110 | + [filteredPeople], |
| 111 | + ); |
| 112 | + |
| 113 | + const onChange = useCallback( |
| 114 | + debounce((value: string | null) => setSearchTerm(value?.trim().toLowerCase()), 500), |
| 115 | + [], |
| 116 | + ); |
| 117 | + |
| 118 | + const onNewPerson = useCallback(async () => { |
| 119 | + router.push("/cms/people/add"); |
| 120 | + }, [router]); |
| 121 | + useHotkeys("alt+n", onNewPerson); |
| 122 | + |
| 123 | + const refSearch = useRef<HTMLDivElement>(null); |
| 124 | + const focusSearch = useCallback(() => { |
| 125 | + refSearch.current?.focus(); |
| 126 | + }, []); |
| 127 | + useHotkeys("alt+s", focusSearch); |
| 128 | + |
| 129 | + return ( |
| 130 | + <> |
| 131 | + <CardToolbar> |
| 132 | + <TextField inputRef={refSearch} fullWidth={false} onChange={onChange} label="Suche" StartIcon={LuSearch} /> |
| 133 | + <Button variant="contained" aria-label="Neue Person" startIcon={<MdOutlinePersonAdd />} href="/cms/people/add"> |
| 134 | + Neue Person |
| 135 | + </Button> |
| 136 | + </CardToolbar> |
| 137 | + <Table options={options} loading={people === undefined} /> |
| 138 | + </> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +function Actions<TData extends { id: string }>({ |
| 143 | + onDelete, |
| 144 | + ...cellContext |
| 145 | +}: CellContext<TData, unknown> & { onDelete: (personId: string) => void }) { |
| 146 | + const router = useRouter(); |
| 147 | + return ( |
| 148 | + <div className="w-full flex place-content-end gap-2"> |
| 149 | + <IconButton |
| 150 | + size="small" |
| 151 | + onClick={() => { |
| 152 | + router.push(`/cms/people/${cellContext.row.original.id}`); |
| 153 | + }} |
| 154 | + > |
| 155 | + <FaRegEdit /> |
| 156 | + </IconButton> |
| 157 | + <IconButton |
| 158 | + size="small" |
| 159 | + onClick={async () => { |
| 160 | + await deletePerson(cellContext.row.original.id); |
| 161 | + onDelete(cellContext.row.original.id); |
| 162 | + }} |
| 163 | + > |
| 164 | + <MdOutlineDelete /> |
| 165 | + </IconButton> |
| 166 | + </div> |
| 167 | + ); |
| 168 | +} |
0 commit comments