From afe51d1930ac42acd7d6fa7164632d10b93bf463 Mon Sep 17 00:00:00 2001 From: Joonatan Kuosa Date: Mon, 16 Sep 2024 09:38:46 +0300 Subject: [PATCH] refactor: remove extra application rounds query --- .../components/reservation-unit/utils.test.ts | 1 + apps/ui/components/reservation/EditStep0.tsx | 4 ++-- apps/ui/gql/gql-types.ts | 20 +++++++++++++++++++ apps/ui/modules/queries/reservationUnit.tsx | 5 +++++ apps/ui/modules/reservation.ts | 4 ++-- apps/ui/pages/reservation-unit/[id].tsx | 17 ++++------------ apps/ui/pages/reservations/[id]/edit.tsx | 15 +++----------- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/apps/ui/components/reservation-unit/utils.test.ts b/apps/ui/components/reservation-unit/utils.test.ts index 807652db0..c104a2cb9 100644 --- a/apps/ui/components/reservation-unit/utils.test.ts +++ b/apps/ui/components/reservation-unit/utils.test.ts @@ -199,6 +199,7 @@ describe("getNextAvailableTime", () => { uuid: "123", images: [], applicationRoundTimeSlots: [], + applicationRounds: [], equipments: [], pricings: [], }; diff --git a/apps/ui/components/reservation/EditStep0.tsx b/apps/ui/components/reservation/EditStep0.tsx index 071602e90..4c3c3ce00 100644 --- a/apps/ui/components/reservation/EditStep0.tsx +++ b/apps/ui/components/reservation/EditStep0.tsx @@ -1,7 +1,6 @@ import { breakpoints } from "common/src/common/style"; import type { PendingReservation } from "@/modules/types"; import type { - ApplicationRoundFieldsFragment, ReservationQuery, ReservationUnitPageQuery, } from "@gql/gql-types"; @@ -38,6 +37,7 @@ import { getNextAvailableTime } from "../reservation-unit/utils"; import { ReservationInfoCard } from "./ReservationInfoCard"; import { Subheading } from "common/src/reservation-form/styles"; import Sanitize from "@/components/common/Sanitize"; +import { type RoundPeriod } from "@/modules/reservable"; type ReservationUnitNodeT = NonNullable< ReservationUnitPageQuery["reservationUnit"] @@ -47,7 +47,7 @@ type Props = { reservation: ReservationNodeT; reservationUnit: ReservationUnitNodeT; reservationForm: UseFormReturn; - activeApplicationRounds: ApplicationRoundFieldsFragment[]; + activeApplicationRounds: readonly RoundPeriod[]; nextStep: () => void; apiBaseUrl: string; isLoading: boolean; diff --git a/apps/ui/gql/gql-types.ts b/apps/ui/gql/gql-types.ts index 5772a0107..b879b0b3a 100644 --- a/apps/ui/gql/gql-types.ts +++ b/apps/ui/gql/gql-types.ts @@ -6285,6 +6285,11 @@ export type ReservationUnitPageFieldsFragment = { weekday: number; reservableTimes?: Array<{ begin: string; end: string } | null> | null; }>; + applicationRounds: Array<{ + id: string; + reservationPeriodBegin: string; + reservationPeriodEnd: string; + }>; reservationUnitType?: { id: string; pk?: number | null; @@ -6443,6 +6448,11 @@ export type ReservationUnitQuery = { weekday: number; reservableTimes?: Array<{ begin: string; end: string } | null> | null; }>; + applicationRounds: Array<{ + id: string; + reservationPeriodBegin: string; + reservationPeriodEnd: string; + }>; reservationUnitType?: { id: string; pk?: number | null; @@ -6624,6 +6634,11 @@ export type ReservationUnitPageQuery = { weekday: number; reservableTimes?: Array<{ begin: string; end: string } | null> | null; }>; + applicationRounds: Array<{ + id: string; + reservationPeriodBegin: string; + reservationPeriodEnd: string; + }>; reservationUnitType?: { id: string; pk?: number | null; @@ -7982,6 +7997,11 @@ export const ReservationUnitPageFieldsFragmentDoc = gql` end } } + applicationRounds { + id + reservationPeriodBegin + reservationPeriodEnd + } descriptionFi descriptionEn descriptionSv diff --git a/apps/ui/modules/queries/reservationUnit.tsx b/apps/ui/modules/queries/reservationUnit.tsx index fa4c157d3..3472f84ee 100644 --- a/apps/ui/modules/queries/reservationUnit.tsx +++ b/apps/ui/modules/queries/reservationUnit.tsx @@ -64,6 +64,11 @@ const RESERVATION_UNIT_PAGE_FRAGMENT = gql` end } } + applicationRounds { + id + reservationPeriodBegin + reservationPeriodEnd + } descriptionFi descriptionEn descriptionSv diff --git a/apps/ui/modules/reservation.ts b/apps/ui/modules/reservation.ts index 6577e5af1..7eb83529c 100644 --- a/apps/ui/modules/reservation.ts +++ b/apps/ui/modules/reservation.ts @@ -249,7 +249,7 @@ export type CanReservationBeChangedProps = { reservableTimes: ReservableMap; newReservation: PendingReservation; reservationUnit: IsReservableFieldsFragment; - activeApplicationRounds?: RoundPeriod[]; + activeApplicationRounds: readonly RoundPeriod[]; }; export function getWhyReservationCantBeChanged( @@ -326,7 +326,7 @@ export function canReservationTimeBeChanged({ newReservation, reservableTimes, reservationUnit, - activeApplicationRounds = [], + activeApplicationRounds, }: CanReservationBeChangedProps): boolean { if (reservation == null) { return false; diff --git a/apps/ui/pages/reservation-unit/[id].tsx b/apps/ui/pages/reservation-unit/[id].tsx index e40ace57d..feaebe0a6 100644 --- a/apps/ui/pages/reservation-unit/[id].tsx +++ b/apps/ui/pages/reservation-unit/[id].tsx @@ -16,13 +16,10 @@ import { useLocalStorage } from "react-use"; import { breakpoints } from "common/src/common/style"; import { type PendingReservation } from "@/modules/types"; import { - ApplicationRoundStatusChoice, type ApplicationRoundTimeSlotNode, PricingType, type ReservationCreateMutationInput, useCreateReservationMutation, - type ApplicationRoundPeriodsQuery, - ApplicationRoundPeriodsDocument, type ReservationUnitPageQuery, type ReservationUnitPageQueryVariables, ReservationUnitPageDocument, @@ -118,16 +115,6 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) { const commonProps = getCommonServerSideProps(); const apolloClient = createApolloClient(commonProps.apiBaseUrl, ctx); - // TODO does this return only possible rounds or do we need to do frontend filtering on them? - const { data } = await apolloClient.query({ - query: ApplicationRoundPeriodsDocument, - }); - const activeApplicationRounds = filterNonNullable( - data?.applicationRounds?.edges?.map((e) => e?.node) - ) - .filter((n) => n.status === ApplicationRoundStatusChoice.Open) - .filter((n) => n?.reservationUnits?.find((x) => x?.pk === pk)); - if (pk) { const today = new Date(); const startDate = today; @@ -149,6 +136,10 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) { pk, }, }); + // TODO does this include only the active rounds or do we need to filter them? + const activeApplicationRounds = filterNonNullable( + reservationUnitData?.reservationUnit?.applicationRounds + ); const previewPass = uuid === reservationUnitData.reservationUnit?.uuid; diff --git a/apps/ui/pages/reservations/[id]/edit.tsx b/apps/ui/pages/reservations/[id]/edit.tsx index 7cc125bde..44fee5d34 100644 --- a/apps/ui/pages/reservations/[id]/edit.tsx +++ b/apps/ui/pages/reservations/[id]/edit.tsx @@ -16,7 +16,6 @@ import { type Mutation, type MutationAdjustReservationTimeArgs, useAdjustReservationTimeMutation, - useApplicationRoundsUiQuery, } from "@gql/gql-types"; import { base64encode, filterNonNullable } from "common/src/helpers"; import { toApiDate } from "common/src/common/util"; @@ -73,17 +72,6 @@ function ReservationEditPage(props: PropsNarrowed): JSX.Element { const [step, setStep] = useState<0 | 1>(0); - // TODO this should be redundant, use the reservationUnit.applicationRounds instead - // TODO this is bad, we can get the application rounds from the reservationUnit - const { data: applicationRoundsData } = useApplicationRoundsUiQuery({ - fetchPolicy: "no-cache", - }); - const activeApplicationRounds = filterNonNullable( - applicationRoundsData?.applicationRounds?.edges?.map((e) => e?.node) - ).filter((ar) => - ar.reservationUnits?.map((n) => n?.pk).includes(reservationUnit.pk) - ); - const [mutation, { loading: isLoading }] = useAdjustReservationTimeMutation(); // TODO should rework this so we don't pass a string here (use Dates till we do the mutation) @@ -198,6 +186,9 @@ function ReservationEditPage(props: PropsNarrowed): JSX.Element { }, ]; + // TODO does this include non active application rounds? + const activeApplicationRounds = reservationUnit.applicationRounds; + return (