Skip to content

Commit

Permalink
fix: uses pageless endpoints for unit and reservationUnit filters
Browse files Browse the repository at this point in the history
  • Loading branch information
vincit-matu committed Nov 15, 2024
1 parent 9f6181e commit 40cff92
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 119 deletions.
63 changes: 19 additions & 44 deletions apps/admin-ui/gql/gql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6627,13 +6627,11 @@ export type ReservationUnitsFilterParamsQueryVariables = Exact<{
}>;

export type ReservationUnitsFilterParamsQuery = {
reservationUnits?: {
totalCount?: number | null;
edges: Array<{
node?: { id: string; nameFi?: string | null; pk?: number | null } | null;
} | null>;
pageInfo: { endCursor?: string | null; hasNextPage: boolean };
} | null;
reservationUnitsAll?: Array<{
id: string;
nameFi?: string | null;
pk?: number | null;
}> | null;
};

export type ReservationUnitTypesFilterQueryVariables = Exact<{
Expand All @@ -6654,20 +6652,17 @@ export type ReservationUnitTypesFilterQuery = {
};

export type UnitsFilterQueryVariables = Exact<{
after?: InputMaybe<Scalars["String"]["input"]>;
orderBy?: InputMaybe<
Array<InputMaybe<UnitOrderingChoices>> | InputMaybe<UnitOrderingChoices>
>;
}>;

export type UnitsFilterQuery = {
units?: {
totalCount?: number | null;
edges: Array<{
node?: { id: string; nameFi?: string | null; pk?: number | null } | null;
} | null>;
pageInfo: { endCursor?: string | null; hasNextPage: boolean };
} | null;
unitsAll?: Array<{
id: string;
nameFi?: string | null;
pk?: number | null;
}> | null;
};

export type CurrentUserQueryVariables = Exact<{ [key: string]: never }>;
Expand Down Expand Up @@ -10902,28 +10897,17 @@ export type ReservationDenyReasonsQueryResult = Apollo.QueryResult<
>;
export const ReservationUnitsFilterParamsDocument = gql`
query ReservationUnitsFilterParams(
$after: String
$unit: [Int]
$orderBy: [ReservationUnitOrderingChoices]
) {
reservationUnits(
after: $after
reservationUnitsAll(
onlyWithPermission: true
unit: $unit
orderBy: $orderBy
) {
edges {
node {
id
nameFi
pk
}
}
pageInfo {
endCursor
hasNextPage
}
totalCount
id
nameFi
pk
}
}
`;
Expand Down Expand Up @@ -11090,20 +11074,11 @@ export type ReservationUnitTypesFilterQueryResult = Apollo.QueryResult<
ReservationUnitTypesFilterQueryVariables
>;
export const UnitsFilterDocument = gql`
query UnitsFilter($after: String, $orderBy: [UnitOrderingChoices]) {
units(onlyWithPermission: true, after: $after, orderBy: $orderBy) {
edges {
node {
id
nameFi
pk
}
}
pageInfo {
endCursor
hasNextPage
}
totalCount
query UnitsFilter($orderBy: [UnitOrderingChoices]) {
unitsAll(onlyWithPermission: true, orderBy: $orderBy) {
id
nameFi
pk
}
}
`;
Expand Down
59 changes: 16 additions & 43 deletions apps/admin-ui/src/hooks/useReservationUnitOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,40 @@ import { useSearchParams } from "react-router-dom";

export const RESERVATION_UNITS_FILTER_PARAMS_QUERY = gql`
query ReservationUnitsFilterParams(
$after: String
$unit: [Int]
$orderBy: [ReservationUnitOrderingChoices]
) {
reservationUnits(
after: $after
reservationUnitsAll(
onlyWithPermission: true
unit: $unit
orderBy: $orderBy
) {
edges {
node {
id
nameFi
pk
}
}
pageInfo {
endCursor
hasNextPage
}
totalCount
id
nameFi
pk
}
}
`;

export function useReservationUnitOptions() {
const [params] = useSearchParams();
const { data, loading, fetchMore, refetch } =
useReservationUnitsFilterParamsQuery({
variables: {
unit: params.getAll("unit").map(Number),
orderBy: [ReservationUnitOrderingChoices.NameFiAsc],
},
});

// auto fetch more (there is no limit, expect number of them would be a few hundred, but in theory this might cause problems)
// NOTE have to useEffect, onComplete stops at 200 items
useEffect(() => {
const { pageInfo } = data?.reservationUnits ?? {};
if (pageInfo?.hasNextPage) {
fetchMore({
variables: {
after: pageInfo.endCursor,
},
});
}
}, [data, fetchMore]);
const { data, loading, refetch } = useReservationUnitsFilterParamsQuery({
variables: {
unit: params.getAll("unit").map(Number),
orderBy: [ReservationUnitOrderingChoices.NameFiAsc],
},
});

useEffect(() => {
refetch();
}, [params]);
}, [refetch, params]);

const resUnits = filterNonNullable(
data?.reservationUnits?.edges.map((x) => x?.node)
const options = filterNonNullable(data?.reservationUnitsAll).map(
(reservationUnit) => ({
label: reservationUnit?.nameFi ?? "",
value: reservationUnit?.pk ?? 0,
})
);

const options = resUnits.map((reservationUnit) => ({
label: reservationUnit?.nameFi ?? "",
value: reservationUnit?.pk ?? 0,
}));

return { options, loading };
}
39 changes: 7 additions & 32 deletions apps/admin-ui/src/hooks/useUnitOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
import { useEffect } from "react";
import { gql } from "@apollo/client";
import { filterNonNullable } from "common/src/helpers";
import { UnitOrderingChoices, useUnitsFilterQuery } from "@gql/gql-types";

// exporting so it doesn't get removed
// TODO combine with other options queries so we only make a single request for all of them
export const UNITS_QUERY = gql`
query UnitsFilter($after: String, $orderBy: [UnitOrderingChoices]) {
units(onlyWithPermission: true, after: $after, orderBy: $orderBy) {
edges {
node {
id
nameFi
pk
}
}
pageInfo {
endCursor
hasNextPage
}
totalCount
query UnitsFilter($orderBy: [UnitOrderingChoices]) {
unitsAll(onlyWithPermission: true, orderBy: $orderBy) {
id
nameFi
pk
}
}
`;

export function useUnitOptions() {
const { data, loading, fetchMore } = useUnitsFilterQuery({
const { data, loading } = useUnitsFilterQuery({
variables: {
orderBy: [UnitOrderingChoices.NameFiAsc],
},
});

// auto fetch more (there is no limit, expect number of them would be a few hundred, but in theory this might cause problems)
// NOTE have to useEffect, onComplete stops at 200 items
useEffect(() => {
const { pageInfo } = data?.units ?? {};
if (pageInfo?.hasNextPage) {
fetchMore({
variables: {
after: pageInfo.endCursor,
},
});
}
}, [data, fetchMore]);

const units = filterNonNullable(data?.units?.edges.map((x) => x?.node));

const options = units.map((unit) => ({
const options = filterNonNullable(data?.unitsAll).map((unit) => ({
label: unit?.nameFi ?? "",
value: unit?.pk ?? 0,
}));
Expand Down

0 comments on commit 40cff92

Please sign in to comment.