|
| 1 | +import { ImportItemList } from "@/lib/crud/importCsv"; |
| 2 | +import { fetcher } from "@/lib/fetcher"; |
| 3 | +import { Stack, Center, Loader } from "@mantine/core"; |
| 4 | +import { createColumnHelper } from "@tanstack/react-table"; |
| 5 | +import React from "react"; |
| 6 | +import useSWR from "swr"; |
| 7 | + |
| 8 | +import TanstackTable from "./TanStackTable"; |
| 9 | + |
| 10 | +const columnHelper = createColumnHelper<ImportItemList["OrderItem"][number]>(); |
| 11 | + |
| 12 | +const columns = [ |
| 13 | + columnHelper.accessor("Order.orderNo", { |
| 14 | + cell: (info) => info.getValue(), |
| 15 | + header: "Order No", |
| 16 | + id: "orderNo", |
| 17 | + sortingFn: "alphanumeric", |
| 18 | + }), |
| 19 | + // Shortcode |
| 20 | + columnHelper.accessor("Order.ImperialStudent.shortcode", { |
| 21 | + cell: (info) => info.getValue(), |
| 22 | + header: "Shortcode", |
| 23 | + id: "shortcode", |
| 24 | + sortingFn: "alphanumeric", |
| 25 | + }), |
| 26 | + // Item name |
| 27 | + columnHelper.accessor("Variant.RootItem.name", { |
| 28 | + cell: (info) => info.getValue(), |
| 29 | + header: "Item Name", |
| 30 | + id: "itemName", |
| 31 | + sortingFn: "alphanumeric", |
| 32 | + }), |
| 33 | + // Variant name |
| 34 | + columnHelper.accessor("Variant.variantName", { |
| 35 | + cell: (info) => info.getValue(), |
| 36 | + header: "Variant", |
| 37 | + id: "variant", |
| 38 | + sortingFn: "alphanumeric", |
| 39 | + }), |
| 40 | + // Quantity |
| 41 | + columnHelper.accessor("quantity", { |
| 42 | + cell: (info) => info.getValue(), |
| 43 | + header: "Quantity", |
| 44 | + id: "quantity", |
| 45 | + sortingFn: "alphanumeric", |
| 46 | + }), |
| 47 | +]; |
| 48 | + |
| 49 | +export const ImportTable = ({ importId }: { importId: string }) => { |
| 50 | + const { data } = useSWR(`/api/imports/${importId}`, fetcher) satisfies { data: ImportItemList }; |
| 51 | + |
| 52 | + if (!data) |
| 53 | + return ( |
| 54 | + <Stack gap="md"> |
| 55 | + <Center> |
| 56 | + <Loader size={24} /> |
| 57 | + </Center> |
| 58 | + </Stack> |
| 59 | + ); |
| 60 | + return ( |
| 61 | + <TanstackTable |
| 62 | + columns={columns} |
| 63 | + data={data.OrderItem} |
| 64 | + enableSearch={false} |
| 65 | + initialSort={[ |
| 66 | + { |
| 67 | + id: "orderNo", |
| 68 | + desc: false, |
| 69 | + }, |
| 70 | + ]} |
| 71 | + /> |
| 72 | + ); |
| 73 | +}; |
0 commit comments