Compare commits
No commits in common. "feat/editable-category" and "main" have entirely different histories.
feat/edita
...
main
@ -4,17 +4,13 @@ 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}>
|
||||||
<CategoryContext.Provider value={await categoriesLoader()}>
|
<RouterProvider router={router} />
|
||||||
<RouterProvider router={router} />
|
|
||||||
</CategoryContext.Provider>
|
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</StrictMode>
|
</StrictMode>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,22 +1,16 @@
|
|||||||
import { useLoaderData } from "react-router";
|
import { useLoaderData } from "react-router";
|
||||||
|
|
||||||
type Category = {
|
export async function loader() {
|
||||||
name: string;
|
return await fetch(`http://localhost:9000/categories`).then((response) => {
|
||||||
group: { name: string } | undefined;
|
return response.json();
|
||||||
};
|
});
|
||||||
|
|
||||||
export async function loader(): Promise<Category[]> {
|
|
||||||
return [
|
|
||||||
{ name: "Books", group: { name: "Media" } },
|
|
||||||
{ name: "Movies", group: { name: "Media" } },
|
|
||||||
{ name: "Groceries", group: { name: "Shopping" } },
|
|
||||||
{ name: "Electronics", group: { name: "Shopping" } },
|
|
||||||
{ name: "Misc", group: undefined }
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Categories() {
|
export default function Categories() {
|
||||||
const data = useLoaderData<typeof loader>();
|
const data: {
|
||||||
|
name: string;
|
||||||
|
group: { name: string } | undefined;
|
||||||
|
}[] = useLoaderData<typeof loader>();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<table>
|
<table>
|
||||||
|
|||||||
@ -7,9 +7,8 @@ import {
|
|||||||
PaginationState,
|
PaginationState,
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from "@tanstack/react-table";
|
} from "@tanstack/react-table";
|
||||||
import { useContext, useMemo, useState } from "react";
|
import { 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;
|
||||||
|
|
||||||
@ -39,47 +38,20 @@ 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) => {
|
|
||||||
const [selectedCategory, setSelectedCategory] = useState(
|
|
||||||
props.getValue() as string
|
|
||||||
);
|
|
||||||
return (
|
|
||||||
<select
|
|
||||||
value={selectedCategory}
|
|
||||||
onChange={(e) => setSelectedCategory(e.target.value)}
|
|
||||||
>
|
|
||||||
{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,
|
||||||
@ -91,51 +63,15 @@ export default function Transactions() {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Static data for local editing
|
const { data, isPending, isError } = useQuery({
|
||||||
const staticTransactions: Transaction[] = [
|
queryKey: ["transactions", pagination.pageIndex, columnFilters],
|
||||||
{
|
queryFn: () =>
|
||||||
id: 1,
|
loader(
|
||||||
date: "2024-06-01",
|
pagination.pageIndex,
|
||||||
description: "Coffee Shop",
|
columnFilters.find((filter) => filter.id == "category")!.value as string
|
||||||
value: 4.5,
|
),
|
||||||
category: "Food",
|
placeholderData: keepPreviousData,
|
||||||
},
|
});
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
date: "2024-06-02",
|
|
||||||
description: "Book Store",
|
|
||||||
value: 15.0,
|
|
||||||
category: "Shopping",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
date: "2024-06-03",
|
|
||||||
description: "Bus Ticket",
|
|
||||||
value: 2.75,
|
|
||||||
category: "Transport",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 4,
|
|
||||||
date: "2024-06-04",
|
|
||||||
description: "Groceries",
|
|
||||||
value: 32.2,
|
|
||||||
category: "Food",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 5,
|
|
||||||
date: "2024-06-05",
|
|
||||||
description: "Movie",
|
|
||||||
value: 12.0,
|
|
||||||
category: "Entertainment",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const data = {
|
|
||||||
transactions: staticTransactions,
|
|
||||||
totalCount: staticTransactions.length,
|
|
||||||
};
|
|
||||||
const isPending = false;
|
|
||||||
const isError = false;
|
|
||||||
|
|
||||||
const table = useReactTable({
|
const table = useReactTable({
|
||||||
columns,
|
columns,
|
||||||
@ -150,7 +86,6 @@ export default function Transactions() {
|
|||||||
pagination,
|
pagination,
|
||||||
columnFilters,
|
columnFilters,
|
||||||
},
|
},
|
||||||
debugTable: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
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