|
| 1 | +import { DataTable } from "@/components/DataTable"; |
| 2 | +import useRoles from "@/features/bot/hooks/useRoles"; |
| 3 | +import { ApiError, RsvpUser, UpdateRsvpDto } from "@/generated"; |
| 4 | +import { Checkbox, filter, Select, TableContainer } from "@chakra-ui/react"; |
| 5 | +import { createColumnHelper } from "@tanstack/react-table"; |
| 6 | +import { DateTime } from "luxon"; |
| 7 | +import React, { useMemo, useState } from "react"; |
| 8 | +import useEventRSVPStatuses from "../hooks/useEventRSVPStatuses"; |
| 9 | +import Autocomplete from "@/components/Autocomplete"; |
| 10 | +import useEditUserRSVP from "../hooks/useEditUserRSVP"; |
| 11 | +import api from "@/services/api"; |
| 12 | +import { rsvpKeys } from "../hooks/keys"; |
| 13 | +import { useQuery } from "@tanstack/react-query"; |
| 14 | + |
| 15 | +function RSVPCell(rsvp: RsvpUser) { |
| 16 | + const editRsvp = useEditUserRSVP(); |
| 17 | + const rsvpStatus = useQuery<RsvpUser[], ApiError, RsvpUser | undefined>({ |
| 18 | + queryFn: () => api.event.getEventRsvps(rsvp.eventId), |
| 19 | + queryKey: rsvpKeys.event(rsvp.eventId), |
| 20 | + select: (data) => data.find((r) => r.id === rsvp.id), |
| 21 | + }); |
| 22 | + |
| 23 | + const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => { |
| 24 | + const value = e.target.value as UpdateRsvpDto["status"] | undefined; |
| 25 | + editRsvp.mutate({ |
| 26 | + rsvpId: rsvp.id, |
| 27 | + rsvp: { |
| 28 | + status: value, |
| 29 | + }, |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + return ( |
| 34 | + <Select |
| 35 | + value={rsvpStatus.data?.status ?? ""} |
| 36 | + onChange={handleChange} |
| 37 | + disabled={editRsvp.isLoading} |
| 38 | + > |
| 39 | + <option value={""}>Unknown</option> |
| 40 | + <option value={UpdateRsvpDto["status"].LATE}>Late</option> |
| 41 | + <option value={UpdateRsvpDto["status"].MAYBE}>Maybe</option> |
| 42 | + <option value={UpdateRsvpDto["status"].NO}>No</option> |
| 43 | + <option value={UpdateRsvpDto["status"].YES}>Yes</option> |
| 44 | + </Select> |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function AttendedCell(rsvp: RsvpUser) { |
| 49 | + const editRsvp = useEditUserRSVP(); |
| 50 | + const rsvpStatus = useQuery<RsvpUser[], ApiError, RsvpUser | undefined>({ |
| 51 | + queryFn: () => api.event.getEventRsvps(rsvp.eventId), |
| 52 | + queryKey: rsvpKeys.event(rsvp.eventId), |
| 53 | + select: (data) => data.find((r) => r.id === rsvp.id), |
| 54 | + }); |
| 55 | + |
| 56 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 57 | + const value = e.target.checked; |
| 58 | + editRsvp.mutate({ |
| 59 | + rsvpId: rsvp.id, |
| 60 | + rsvp: { |
| 61 | + attended: value, |
| 62 | + }, |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <Checkbox |
| 68 | + onChange={handleChange} |
| 69 | + isChecked={rsvpStatus.data?.attended ?? false} |
| 70 | + disabled={editRsvp.isLoading} |
| 71 | + /> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +const columnHelper = createColumnHelper<RsvpUser>(); |
| 76 | + |
| 77 | +const columns = [ |
| 78 | + columnHelper.accessor("user.username", { |
| 79 | + header: "Name", |
| 80 | + // footer: "First Name", |
| 81 | + }), |
| 82 | + |
| 83 | + columnHelper.group({ |
| 84 | + id: "status", |
| 85 | + header: "Status", |
| 86 | + columns: [ |
| 87 | + columnHelper.accessor("status", { |
| 88 | + id: "status", |
| 89 | + header: "RSVP Status", |
| 90 | + // footer: "RSVP Status", |
| 91 | + cell: (props) => <RSVPCell {...props.row.original} />, |
| 92 | + }), |
| 93 | + columnHelper.accessor("attended", { |
| 94 | + id: "attended", |
| 95 | + header: "Attended", |
| 96 | + // footer: "Attended", |
| 97 | + cell: (props) => <AttendedCell {...props.row.original} />, |
| 98 | + }), |
| 99 | + ], |
| 100 | + }), |
| 101 | + columnHelper.group({ |
| 102 | + id: "meta", |
| 103 | + columns: [ |
| 104 | + columnHelper.accessor("updatedAt", { |
| 105 | + id: "updatedAt", |
| 106 | + header: "Updated At", |
| 107 | + // footer: "Updated At", |
| 108 | + cell: (props) => |
| 109 | + DateTime.fromISO(props.getValue()).toLocaleString( |
| 110 | + DateTime.DATETIME_MED |
| 111 | + ), |
| 112 | + }), |
| 113 | + columnHelper.accessor("createdAt", { |
| 114 | + id: "createdAt", |
| 115 | + header: "Created At", |
| 116 | + // footer: "Created At", |
| 117 | + cell: (props) => |
| 118 | + DateTime.fromISO(props.getValue()).toLocaleString( |
| 119 | + DateTime.DATETIME_MED |
| 120 | + ), |
| 121 | + }), |
| 122 | + ], |
| 123 | + }), |
| 124 | +]; |
| 125 | + |
| 126 | +const readableStatus = (status: RsvpUser.status | null) => { |
| 127 | + if (status === null) { |
| 128 | + return "Unknown"; |
| 129 | + } else if (status === "YES") { |
| 130 | + return "Coming"; |
| 131 | + } else if (status === "MAYBE") { |
| 132 | + return "Maybe"; |
| 133 | + } else if (status === "LATE") { |
| 134 | + return "Late"; |
| 135 | + } else { |
| 136 | + return "Not Coming"; |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +export interface RSVPListProps { |
| 141 | + eventId?: string; |
| 142 | +} |
| 143 | + |
| 144 | +interface RoleItem { |
| 145 | + label: string; |
| 146 | + value: string; |
| 147 | +} |
| 148 | + |
| 149 | +export const RSVPList: React.FC<RSVPListProps> = ({ eventId }) => { |
| 150 | + const { data: rsvps } = useEventRSVPStatuses(eventId); |
| 151 | + const roles = useRoles(); |
| 152 | + |
| 153 | + const options = useMemo(() => { |
| 154 | + if (!roles.data) return []; |
| 155 | + return roles.data.map((role) => ({ label: role.name, value: role.id })); |
| 156 | + }, [roles.data]); |
| 157 | + |
| 158 | + const handleSelectedItemsChange = (selectedItems?: RoleItem[]) => { |
| 159 | + if (selectedItems) { |
| 160 | + setSelected(selectedItems); |
| 161 | + } |
| 162 | + }; |
| 163 | + // const [sorting, setSorting] = React.useState<SortingState>([]); |
| 164 | + // const table = useReactTable({ |
| 165 | + // data: rsvps ?? [], |
| 166 | + // state: { |
| 167 | + // sorting, |
| 168 | + // }, |
| 169 | + // onSortingChange: setSorting, |
| 170 | + // columns, |
| 171 | + // getCoreRowModel: getCoreRowModel(), |
| 172 | + // }); |
| 173 | + const [selected, setSelected] = useState<Array<RoleItem>>([]); |
| 174 | + |
| 175 | + const filteredRsvps = useMemo(() => { |
| 176 | + if (!rsvps) return []; |
| 177 | + if (!selected.length) return rsvps; |
| 178 | + |
| 179 | + return rsvps.filter((rsvp) => |
| 180 | + selected.every((v) => rsvp.user.roles.includes(v.value)) |
| 181 | + ); |
| 182 | + }, [selected, rsvps]); |
| 183 | + |
| 184 | + return ( |
| 185 | + <TableContainer> |
| 186 | + <Autocomplete |
| 187 | + options={options} |
| 188 | + filter={(items, filterValue) => |
| 189 | + items.filter((item) => |
| 190 | + item.label.toLowerCase().includes(filterValue.toLowerCase()) |
| 191 | + ) |
| 192 | + } |
| 193 | + itemToString={(item) => item?.label ?? ""} |
| 194 | + onChange={setSelected} |
| 195 | + value={selected} |
| 196 | + /> |
| 197 | + <DataTable columns={columns} data={filteredRsvps ?? []} /> |
| 198 | + </TableContainer> |
| 199 | + ); |
| 200 | +}; |
| 201 | + |
| 202 | +export default RSVPList; |
0 commit comments