Skip to content

Commit

Permalink
Refactor initial state override to initial filters in EventsTable and…
Browse files Browse the repository at this point in the history
… DDNTable components
  • Loading branch information
jthoward64 committed Dec 9, 2024
1 parent 0b69c7d commit e74a3e9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion packages/portal/src/elements/tables/EventsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const EventsTable = () => {
initPage: 1,
initPageSize: 10,
initSorting: [],
initialStateOverride: {
initialFilters: {
dateFilters: [
{
field: "occurrenceStart",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const DDNTable = () => {
initPage: 1,
initPageSize: 10,
initSorting: [],
initialStateOverride: {},
initialFilters: {},
},
{
allFields: [
Expand Down
89 changes: 31 additions & 58 deletions packages/portal/src/hooks/useListQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
StringFilterItemInterface,
} from "@ukdanceblue/common";
import type { Dispatch, SetStateAction } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useMemo, useState } from "react";

interface FilterObject<
DateFields extends string,
Expand Down Expand Up @@ -108,19 +108,22 @@ export function useListQuery<
initPage,
initPageSize,
initSorting,
initialStateOverride,
initialFilters,
}: {
initPage: number;
initPageSize: number;
initSorting: SortOption<AllFields>[];
initialStateOverride?: Partial<
ListQueryOptions<
DateFields,
IsNullFields,
NumericFields,
OneOfFields,
StringFields,
BooleanFields
initialFilters?: Partial<
Omit<
ListQueryOptions<
DateFields,
IsNullFields,
NumericFields,
OneOfFields,
StringFields,
BooleanFields
>,
"page" | "pageSize" | "sortBy" | "sortDirection"
>
>;
},
Expand Down Expand Up @@ -149,61 +152,38 @@ export function useListQuery<
IsNullFields,
BooleanFields
> {
const [initSortDirection, initSortBy] = useMemo(() => {
const initSortDirection: SortDirection[] = [];
const initSortBy: AllFields[] = [];
for (const sortOption of initSorting) {
initSortDirection.push(sortOption.direction);
initSortBy.push(sortOption.field);
}
return [initSortDirection, initSortBy];
}, [initSorting]);

const [queryOptions, setQueryOptions] = useState<
ListQueryOptions<
DateFields,
IsNullFields,
NumericFields,
OneOfFields,
StringFields,
BooleanFields
>
>({
page: initPage,
pageSize: initPageSize,
sortBy: initSortBy,
sortDirection: initSortDirection,
dateFilters: [],
isNullFilters: [],
numericFilters: [],
oneOfFilters: [],
stringFilters: [],
booleanFilters: [],
});

const [page, setPage] = useState<number>(initPage);
const [pageSize, setPageSize] = useState<number>(initPageSize);
const [sorting, setSorting] = useState<SortOption<AllFields>[]>(initSorting);
const [dateFilters, setDateFilters] = useState<
DateFilterItemInterface<DateFields>[]
>([]);
>(initialFilters?.dateFilters ?? []);
const [isNullFilters, setIsNullFilters] = useState<
IsNullFilterItemInterface<IsNullFields>[]
>([]);
>(initialFilters?.isNullFilters ?? []);
const [numericFilters, setNumericFilters] = useState<
NumericFilterItemInterface<NumericFields>[]
>([]);
>(initialFilters?.numericFilters ?? []);
const [oneOfFilters, setOneOfFilters] = useState<
OneOfFilterItemInterface<OneOfFields>[]
>([]);
>(initialFilters?.oneOfFilters ?? []);
const [stringFilters, setStringFilters] = useState<
StringFilterItemInterface<StringFields>[]
>([]);
>(initialFilters?.stringFilters ?? []);
const [booleanFilters, setBooleanFilters] = useState<
BooleanFilterItemInterface<BooleanFields>[]
>([]);
>(initialFilters?.booleanFilters ?? []);

useEffect(() => {
const queryOptions = useMemo<
ListQueryOptions<
DateFields,
IsNullFields,
NumericFields,
OneOfFields,
StringFields,
BooleanFields
>
>(() => {
const sortDirection: SortDirection[] = [];
const sortBy: AllFields[] = [];

Expand All @@ -212,7 +192,7 @@ export function useListQuery<
sortBy.push(sortOption.field);
}

setQueryOptions({
return {
page,
pageSize,
sortBy,
Expand All @@ -223,7 +203,7 @@ export function useListQuery<
oneOfFilters,
stringFilters,
booleanFilters,
});
};
}, [
page,
pageSize,
Expand Down Expand Up @@ -446,13 +426,6 @@ export function useListQuery<
setBooleanFilters([]);
}, []);

useEffect(() => {
setQueryOptions((prevQueryOptions) => ({
...prevQueryOptions,
...initialStateOverride,
}));
}, []);

return {
queryOptions,
updatePagination,
Expand Down
12 changes: 1 addition & 11 deletions packages/portal/src/routes/fundraising/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,12 @@ export const Route = createFileRoute("/fundraising/")({
});

function RouteComponent() {
const [result] = useQuery({
query: ViewTeamFundraisingDocument,
variables: {
page: 1,
pageSize: 10,
},
});

const [reportDialogOpen, setReportDialogOpen] = useState(false);

useQueryStatusWatcher(result);

const listQuery = useListQuery(
{
initPage: 1,
initPageSize: 10,
initPageSize: 20,
initSorting: [{ field: "donatedOn", direction: "desc" }],
},
{
Expand Down

0 comments on commit e74a3e9

Please sign in to comment.