ongoing editable category
This commit is contained in:
parent
f7ddab2510
commit
d3c43c42ae
@ -4,13 +4,17 @@ import { createRoot } from "react-dom/client";
|
|||||||
import { RouterProvider } from "react-router";
|
import { RouterProvider } from "react-router";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { router } from "./routes.ts";
|
import { router } from "./routes.ts";
|
||||||
|
import { loader as categoriesLoader } from "./routes/categories";
|
||||||
|
import { CategoryContext } from "./store/CategoryContext.ts";
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
createRoot(document.getElementById("root")!).render(
|
createRoot(document.getElementById("root")!).render(
|
||||||
<StrictMode>
|
<StrictMode>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<RouterProvider router={router} />
|
<CategoryContext value={await categoriesLoader()}>
|
||||||
|
<RouterProvider router={router} />
|
||||||
|
</CategoryContext>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</StrictMode>
|
</StrictMode>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,16 +1,18 @@
|
|||||||
import { useLoaderData } from "react-router";
|
import { useLoaderData } from "react-router";
|
||||||
|
|
||||||
export async function loader() {
|
type Category = {
|
||||||
return await fetch(`http://localhost:9000/categories`).then((response) => {
|
name: string;
|
||||||
|
group: { name: string } | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function loader(): Promise<Category[]> {
|
||||||
|
return fetch(`http://localhost:9000/categories`).then((response) => {
|
||||||
return response.json();
|
return response.json();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Categories() {
|
export default function Categories() {
|
||||||
const data: {
|
const data = useLoaderData<typeof loader>();
|
||||||
name: string;
|
|
||||||
group: { name: string } | undefined;
|
|
||||||
}[] = useLoaderData<typeof loader>();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<table>
|
<table>
|
||||||
|
|||||||
@ -7,8 +7,9 @@ import {
|
|||||||
PaginationState,
|
PaginationState,
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from "@tanstack/react-table";
|
} from "@tanstack/react-table";
|
||||||
import { useState } from "react";
|
import { useContext, useMemo, useState } from "react";
|
||||||
import { DebounceInput } from "react-debounce-input";
|
import { DebounceInput } from "react-debounce-input";
|
||||||
|
import { CategoryContext } from "../store/CategoryContext";
|
||||||
|
|
||||||
const PageSize = 30;
|
const PageSize = 30;
|
||||||
|
|
||||||
@ -38,20 +39,42 @@ async function loader(page = 0, category: string | undefined = "") {
|
|||||||
|
|
||||||
const columnHelper = createColumnHelper<Transaction>();
|
const columnHelper = createColumnHelper<Transaction>();
|
||||||
|
|
||||||
const columns = [
|
|
||||||
columnHelper.accessor("date", {
|
|
||||||
enableColumnFilter: false,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("description", {
|
|
||||||
enableColumnFilter: false,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("value", {
|
|
||||||
enableColumnFilter: false,
|
|
||||||
}),
|
|
||||||
columnHelper.accessor("category", {}),
|
|
||||||
];
|
|
||||||
|
|
||||||
export default function Transactions() {
|
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 (
|
||||||
|
<select value={props.getValue()}>
|
||||||
|
{categories.map((category) => (
|
||||||
|
<option key={category.name} value={category.name}>
|
||||||
|
{category.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
[categories]
|
||||||
|
);
|
||||||
|
|
||||||
const [pagination, setPagination] = useState<PaginationState>({
|
const [pagination, setPagination] = useState<PaginationState>({
|
||||||
pageIndex: 0,
|
pageIndex: 0,
|
||||||
pageSize: PageSize,
|
pageSize: PageSize,
|
||||||
@ -86,6 +109,7 @@ export default function Transactions() {
|
|||||||
pagination,
|
pagination,
|
||||||
columnFilters,
|
columnFilters,
|
||||||
},
|
},
|
||||||
|
debugTable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
|
|||||||
8
src/store/CategoryContext.ts
Normal file
8
src/store/CategoryContext.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { createContext } from "react";
|
||||||
|
|
||||||
|
type Category = {
|
||||||
|
name: string;
|
||||||
|
group: { name: string } | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const CategoryContext = createContext<Category[]>([]);
|
||||||
Loading…
x
Reference in New Issue
Block a user