|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { GenericTable } from "@/components/GenericTable" |
| 4 | +import { DateTooltip } from "@/components/DateTooltip" |
| 5 | +import { mapNotificationPayloadTypeToLabel, mapNotificationTypeToLabel, type Notification } from "@dotkomonline/rpc" |
| 6 | +import { Anchor, Box, Button, Skeleton, Stack, Title } from "@mantine/core" |
| 7 | +import { createColumnHelper, getCoreRowModel, useReactTable } from "@tanstack/react-table" |
| 8 | +import Link from "next/link" |
| 9 | +import { type FC, useMemo } from "react" |
| 10 | +import { useNotificationsByPayloadQuery } from "../../arrangementer/queries" |
| 11 | +import { openCreateGroupNotificationModal } from "../modals/create-group-notification-modal" |
| 12 | +import { useGroupDetailsContext } from "./provider" |
| 13 | + |
| 14 | +export const GroupNotificationPage: FC = () => { |
| 15 | + const { group } = useGroupDetailsContext() |
| 16 | + const { notifications, isLoading } = useNotificationsByPayloadQuery("GROUP", group.slug) |
| 17 | + |
| 18 | + const columnHelper = createColumnHelper<Notification>() |
| 19 | + |
| 20 | + const columns = useMemo( |
| 21 | + () => [ |
| 22 | + columnHelper.accessor((notification) => notification.title, { |
| 23 | + id: "title", |
| 24 | + header: () => "Tittel", |
| 25 | + sortingFn: "alphanumeric", |
| 26 | + cell: (info) => ( |
| 27 | + <Anchor component={Link} size="sm" href={`/varslinger/${info.row.original.id}`}> |
| 28 | + {info.getValue()} |
| 29 | + </Anchor> |
| 30 | + ), |
| 31 | + }), |
| 32 | + columnHelper.accessor((notification) => notification.shortDescription, { |
| 33 | + id: "shortDescription", |
| 34 | + header: () => "Kort beskrivelse", |
| 35 | + cell: (info) => info.getValue(), |
| 36 | + sortingFn: "alphanumeric", |
| 37 | + }), |
| 38 | + columnHelper.accessor((notification) => notification.type, { |
| 39 | + id: "type", |
| 40 | + header: () => "Type", |
| 41 | + cell: (info) => mapNotificationTypeToLabel(info.getValue()), |
| 42 | + sortingFn: "alphanumeric", |
| 43 | + }), |
| 44 | + columnHelper.accessor((notification) => notification.payloadType, { |
| 45 | + id: "payloadType", |
| 46 | + header: () => "Payload type", |
| 47 | + cell: (info) => mapNotificationPayloadTypeToLabel(info.getValue()), |
| 48 | + sortingFn: "alphanumeric", |
| 49 | + }), |
| 50 | + columnHelper.accessor((notification) => notification.createdAt, { |
| 51 | + id: "createdAt", |
| 52 | + header: () => "Opprettet", |
| 53 | + cell: (info) => <DateTooltip date={info.getValue()} />, |
| 54 | + sortingFn: "datetime", |
| 55 | + }), |
| 56 | + ], |
| 57 | + [columnHelper] |
| 58 | + ) |
| 59 | + |
| 60 | + const tableOptions = useMemo( |
| 61 | + () => ({ |
| 62 | + data: notifications, |
| 63 | + getCoreRowModel: getCoreRowModel(), |
| 64 | + columns, |
| 65 | + }), |
| 66 | + [notifications, columns] |
| 67 | + ) |
| 68 | + |
| 69 | + const table = useReactTable(tableOptions) |
| 70 | + |
| 71 | + return ( |
| 72 | + <Skeleton visible={isLoading}> |
| 73 | + <Stack gap="lg"> |
| 74 | + <Box> |
| 75 | + <Title order={3}>Opprett varsling</Title> |
| 76 | + <Button mt="md" onClick={openCreateGroupNotificationModal({ groupSlug: group.slug })}> |
| 77 | + Legg til ny varsling |
| 78 | + </Button> |
| 79 | + </Box> |
| 80 | + |
| 81 | + <Box> |
| 82 | + <Title order={2}>Varslinger</Title> |
| 83 | + <GenericTable table={table} /> |
| 84 | + </Box> |
| 85 | + </Stack> |
| 86 | + </Skeleton> |
| 87 | + ) |
| 88 | +} |
0 commit comments