diff --git a/src/main.tsx b/src/main.tsx index a5a082b..a6962a9 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,13 +4,17 @@ import { createRoot } from "react-dom/client"; import { RouterProvider } from "react-router"; import "./index.css"; import { router } from "./routes.ts"; +import { loader as categoriesLoader } from "./routes/categories"; +import { CategoryContext } from "./store/CategoryContext.ts"; const queryClient = new QueryClient(); createRoot(document.getElementById("root")!).render( - + + + ); diff --git a/src/routes/categories.tsx b/src/routes/categories.tsx index f1e1f49..1a43516 100644 --- a/src/routes/categories.tsx +++ b/src/routes/categories.tsx @@ -1,16 +1,18 @@ import { useLoaderData } from "react-router"; -export async function loader() { - return await fetch(`http://localhost:9000/categories`).then((response) => { +type Category = { + name: string; + group: { name: string } | undefined; +}; + +export async function loader(): Promise { + return fetch(`http://localhost:9000/categories`).then((response) => { return response.json(); }); } export default function Categories() { - const data: { - name: string; - group: { name: string } | undefined; - }[] = useLoaderData(); + const data = useLoaderData(); return ( diff --git a/src/routes/transactions.tsx b/src/routes/transactions.tsx index d84d31a..76c436a 100644 --- a/src/routes/transactions.tsx +++ b/src/routes/transactions.tsx @@ -7,8 +7,9 @@ import { PaginationState, useReactTable, } from "@tanstack/react-table"; -import { useState } from "react"; +import { useContext, useMemo, useState } from "react"; import { DebounceInput } from "react-debounce-input"; +import { CategoryContext } from "../store/CategoryContext"; const PageSize = 30; @@ -38,20 +39,42 @@ async function loader(page = 0, category: string | undefined = "") { const columnHelper = createColumnHelper(); -const columns = [ - columnHelper.accessor("date", { - enableColumnFilter: false, - }), - columnHelper.accessor("description", { - enableColumnFilter: false, - }), - columnHelper.accessor("value", { - enableColumnFilter: false, - }), - columnHelper.accessor("category", {}), -]; - export default function Transactions() { + const categories = useContext(CategoryContext); + + const columns = useMemo( + () => [ + columnHelper.accessor("date", { + header: "Date", + enableColumnFilter: false, + }), + columnHelper.accessor("description", { + header: "Description", + enableColumnFilter: false, + }), + columnHelper.accessor("value", { + header: "Value", + enableColumnFilter: false, + }), + columnHelper.accessor("category", { + header: "Category", + cell: (props) => { + console.log(props.getValue()); + return ( + + ); + }, + }), + ], + [categories] + ); + const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: PageSize, @@ -86,6 +109,7 @@ export default function Transactions() { pagination, columnFilters, }, + debugTable: true, }); if (isPending) { diff --git a/src/store/CategoryContext.ts b/src/store/CategoryContext.ts new file mode 100644 index 0000000..7b8c2fa --- /dev/null +++ b/src/store/CategoryContext.ts @@ -0,0 +1,8 @@ +import { createContext } from "react"; + +type Category = { + name: string; + group: { name: string } | undefined; +}; + +export const CategoryContext = createContext([]);