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 5de2c1f commit 3eabdc5
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 109 deletions.
118 changes: 74 additions & 44 deletions apps/admin-ui/gql/gql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ export type Query = {
reservationUnitCancellationRules?: Maybe<ReservationUnitCancellationRuleNodeConnection>;
reservationUnitTypes?: Maybe<ReservationUnitTypeNodeConnection>;
reservationUnits?: Maybe<ReservationUnitNodeConnection>;
reservationUnitsAll?: Maybe<Array<ReservationUnitAllNode>>;
reservations?: Maybe<ReservationNodeConnection>;
resource?: Maybe<ResourceNode>;
resources?: Maybe<ResourceNodeConnection>;
Expand All @@ -1855,6 +1856,7 @@ export type Query = {
unit?: Maybe<UnitNode>;
unitGroups?: Maybe<UnitGroupNodeConnection>;
units?: Maybe<UnitNodeConnection>;
unitsAll?: Maybe<Array<UnitAllNode>>;
user?: Maybe<UserNode>;
};

Expand Down Expand Up @@ -2324,6 +2326,21 @@ export type QueryReservationUnitsArgs = {
unit?: InputMaybe<Array<InputMaybe<Scalars["Int"]["input"]>>>;
};

export type QueryReservationUnitsAllArgs = {
nameEn?: InputMaybe<Scalars["String"]["input"]>;
nameEn_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameEn_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
nameFi?: InputMaybe<Scalars["String"]["input"]>;
nameFi_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameFi_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
nameSv?: InputMaybe<Scalars["String"]["input"]>;
nameSv_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameSv_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
onlyWithPermission?: InputMaybe<Scalars["Boolean"]["input"]>;
orderBy?: InputMaybe<Array<InputMaybe<ReservationUnitOrderingChoices>>>;
unit?: InputMaybe<Array<InputMaybe<Scalars["Int"]["input"]>>>;
};

export type QueryReservationsArgs = {
after?: InputMaybe<Scalars["String"]["input"]>;
applyingForFreeOfCharge?: InputMaybe<Scalars["Boolean"]["input"]>;
Expand Down Expand Up @@ -2471,6 +2488,21 @@ export type QueryUnitsArgs = {
serviceSector?: InputMaybe<Scalars["Decimal"]["input"]>;
};

export type QueryUnitsAllArgs = {
nameEn?: InputMaybe<Scalars["String"]["input"]>;
nameEn_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameEn_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
nameFi?: InputMaybe<Scalars["String"]["input"]>;
nameFi_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameFi_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
nameSv?: InputMaybe<Scalars["String"]["input"]>;
nameSv_Icontains?: InputMaybe<Scalars["String"]["input"]>;
nameSv_Istartswith?: InputMaybe<Scalars["String"]["input"]>;
onlyWithPermission?: InputMaybe<Scalars["Boolean"]["input"]>;
orderBy?: InputMaybe<Array<InputMaybe<UnitOrderingChoices>>>;
unit?: InputMaybe<Array<InputMaybe<Scalars["Int"]["input"]>>>;
};

export type QueryUserArgs = {
id: Scalars["ID"]["input"];
};
Expand Down Expand Up @@ -3553,6 +3585,17 @@ export enum ReservationTypeStaffChoice {
Staff = "STAFF",
}

/** This Node should be kept to the bare minimum and never expose any relations to avoid performance issues. */
export type ReservationUnitAllNode = Node & {
/** The ID of the object */
id: Scalars["ID"]["output"];
name: Scalars["String"]["output"];
nameEn?: Maybe<Scalars["String"]["output"]>;
nameFi?: Maybe<Scalars["String"]["output"]>;
nameSv?: Maybe<Scalars["String"]["output"]>;
pk?: Maybe<Scalars["Int"]["output"]>;
};

export type ReservationUnitCancellationRuleNode = Node & {
canBeCancelledTimeBefore?: Maybe<Scalars["Duration"]["output"]>;
/** The ID of the object */
Expand Down Expand Up @@ -4877,6 +4920,18 @@ export type TimeSlotType = {
end: Scalars["Time"]["output"];
};

/** This Node should be kept to the bare minimum and never expose any relations to avoid performance issues. */
export type UnitAllNode = Node & {
/** The ID of the object */
id: Scalars["ID"]["output"];
name: Scalars["String"]["output"];
nameEn?: Maybe<Scalars["String"]["output"]>;
nameFi?: Maybe<Scalars["String"]["output"]>;
nameSv?: Maybe<Scalars["String"]["output"]>;
pk?: Maybe<Scalars["Int"]["output"]>;
tprekId?: Maybe<Scalars["String"]["output"]>;
};

export type UnitGroupNode = Node & {
/** The ID of the object */
id: Scalars["ID"]["output"];
Expand Down Expand Up @@ -6477,13 +6532,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 @@ -6504,20 +6557,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 @@ -10752,28 +10802,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 @@ -10940,20 +10979,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
47 changes: 11 additions & 36 deletions apps/admin-ui/src/hooks/useReservationUnitOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,33 @@ 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)
Expand Down
35 changes: 6 additions & 29 deletions apps/admin-ui/src/hooks/useUnitOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
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) => ({
Expand Down
Loading

0 comments on commit 3eabdc5

Please sign in to comment.