Compare commits
2 Commits
18c55262d2
...
bd39e6de40
| Author | SHA1 | Date | |
|---|---|---|---|
| bd39e6de40 | |||
| 766149a869 |
@ -7,7 +7,11 @@ import {
|
|||||||
PaginationState,
|
PaginationState,
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from "@tanstack/react-table";
|
} from "@tanstack/react-table";
|
||||||
import { useContext, useState } from "react";
|
<<<<<<< HEAD
|
||||||
|
import { useContext, useMemo, useState } from "react";
|
||||||
|
=======
|
||||||
|
import { useContext, useState, useMemo } from "react";
|
||||||
|
>>>>>>> 43722c0 (feat: send description before category in loader)
|
||||||
import { DebounceInput } from "react-debounce-input";
|
import { DebounceInput } from "react-debounce-input";
|
||||||
import { CategoryContext } from "../store/CategoryContext";
|
import { CategoryContext } from "../store/CategoryContext";
|
||||||
|
|
||||||
@ -21,11 +25,16 @@ type Transaction = {
|
|||||||
category: string;
|
category: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function loader(page = 0, category: string | undefined = "") {
|
async function loader(
|
||||||
|
page = 0,
|
||||||
|
description: string | undefined = "",
|
||||||
|
category: string | undefined = ""
|
||||||
|
) {
|
||||||
const url = new URL("http://localhost:9000/transactions");
|
const url = new URL("http://localhost:9000/transactions");
|
||||||
url.search = new URLSearchParams({
|
url.search = new URLSearchParams({
|
||||||
limit: String(PageSize),
|
limit: String(PageSize),
|
||||||
offset: String(page * PageSize),
|
offset: String(page * PageSize),
|
||||||
|
...(description !== "" && { description: description }),
|
||||||
...(category !== "" && { category: category }),
|
...(category !== "" && { category: category }),
|
||||||
}).toString();
|
}).toString();
|
||||||
|
|
||||||
@ -39,14 +48,18 @@ async function loader(page = 0, category: string | undefined = "") {
|
|||||||
|
|
||||||
const columnHelper = createColumnHelper<Transaction>();
|
const columnHelper = createColumnHelper<Transaction>();
|
||||||
|
|
||||||
const columns = [
|
export default function Transactions() {
|
||||||
|
const categories = useContext(CategoryContext);
|
||||||
|
|
||||||
|
const columns = useMemo(
|
||||||
|
() => [
|
||||||
columnHelper.accessor("date", {
|
columnHelper.accessor("date", {
|
||||||
header: "Date",
|
header: "Date",
|
||||||
enableColumnFilter: false,
|
enableColumnFilter: false,
|
||||||
}),
|
}),
|
||||||
columnHelper.accessor("description", {
|
columnHelper.accessor("description", {
|
||||||
header: "Description",
|
header: "Description",
|
||||||
enableColumnFilter: false,
|
enableColumnFilter: true,
|
||||||
}),
|
}),
|
||||||
columnHelper.accessor("value", {
|
columnHelper.accessor("value", {
|
||||||
header: "Value",
|
header: "Value",
|
||||||
@ -57,8 +70,8 @@ const columns = [
|
|||||||
cell: (props) => {
|
cell: (props) => {
|
||||||
console.log(props.getValue());
|
console.log(props.getValue());
|
||||||
return (
|
return (
|
||||||
<select defaultValue={props.getValue()}>
|
<select value={props.getValue()}>
|
||||||
{useContext(CategoryContext).map((category) => (
|
{categories.map((category) => (
|
||||||
<option key={category.name} value={category.name}>
|
<option key={category.name} value={category.name}>
|
||||||
{category.name}
|
{category.name}
|
||||||
</option>
|
</option>
|
||||||
@ -67,9 +80,10 @@ const columns = [
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
];
|
],
|
||||||
|
[categories]
|
||||||
|
);
|
||||||
|
|
||||||
export default function Transactions() {
|
|
||||||
const [pagination, setPagination] = useState<PaginationState>({
|
const [pagination, setPagination] = useState<PaginationState>({
|
||||||
pageIndex: 0,
|
pageIndex: 0,
|
||||||
pageSize: PageSize,
|
pageSize: PageSize,
|
||||||
@ -81,11 +95,15 @@ export default function Transactions() {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// track description filter separately (or use table filter for description)
|
||||||
|
const [descriptionFilter, setDescriptionFilter] = useState("");
|
||||||
|
|
||||||
const { data, isPending, isError } = useQuery({
|
const { data, isPending, isError } = useQuery({
|
||||||
queryKey: ["transactions", pagination.pageIndex, columnFilters],
|
queryKey: ["transactions", pagination.pageIndex, columnFilters, descriptionFilter],
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
loader(
|
loader(
|
||||||
pagination.pageIndex,
|
pagination.pageIndex,
|
||||||
|
descriptionFilter,
|
||||||
columnFilters.find((filter) => filter.id == "category")!.value as string
|
columnFilters.find((filter) => filter.id == "category")!.value as string
|
||||||
),
|
),
|
||||||
placeholderData: keepPreviousData,
|
placeholderData: keepPreviousData,
|
||||||
@ -133,9 +151,13 @@ export default function Transactions() {
|
|||||||
<div>
|
<div>
|
||||||
<DebounceInput
|
<DebounceInput
|
||||||
debounceTimeout={275}
|
debounceTimeout={275}
|
||||||
onChange={(e) =>
|
onChange={(e) => {
|
||||||
header.column.setFilterValue(e.target.value)
|
if (header.column.id === "description") {
|
||||||
|
setDescriptionFilter(e.target.value);
|
||||||
|
} else {
|
||||||
|
header.column.setFilterValue(e.target.value);
|
||||||
}
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user