Skip to content

Commit

Permalink
refactor: remove extra application rounds query
Browse files Browse the repository at this point in the history
  • Loading branch information
joonatank committed Sep 16, 2024
1 parent d7d9a35 commit afe51d1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
1 change: 1 addition & 0 deletions apps/ui/components/reservation-unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ describe("getNextAvailableTime", () => {
uuid: "123",
images: [],
applicationRoundTimeSlots: [],
applicationRounds: [],
equipments: [],
pricings: [],
};
Expand Down
4 changes: 2 additions & 2 deletions apps/ui/components/reservation/EditStep0.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"]
Expand All @@ -47,7 +47,7 @@ type Props = {
reservation: ReservationNodeT;
reservationUnit: ReservationUnitNodeT;
reservationForm: UseFormReturn<PendingReservationFormType>;
activeApplicationRounds: ApplicationRoundFieldsFragment[];
activeApplicationRounds: readonly RoundPeriod[];
nextStep: () => void;
apiBaseUrl: string;
isLoading: boolean;
Expand Down
20 changes: 20 additions & 0 deletions apps/ui/gql/gql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -7982,6 +7997,11 @@ export const ReservationUnitPageFieldsFragmentDoc = gql`
end
}
}
applicationRounds {
id
reservationPeriodBegin
reservationPeriodEnd
}
descriptionFi
descriptionEn
descriptionSv
Expand Down
5 changes: 5 additions & 0 deletions apps/ui/modules/queries/reservationUnit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const RESERVATION_UNIT_PAGE_FRAGMENT = gql`
end
}
}
applicationRounds {
id
reservationPeriodBegin
reservationPeriodEnd
}
descriptionFi
descriptionEn
descriptionSv
Expand Down
4 changes: 2 additions & 2 deletions apps/ui/modules/reservation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export type CanReservationBeChangedProps = {
reservableTimes: ReservableMap;
newReservation: PendingReservation;
reservationUnit: IsReservableFieldsFragment;
activeApplicationRounds?: RoundPeriod[];
activeApplicationRounds: readonly RoundPeriod[];
};

export function getWhyReservationCantBeChanged(
Expand Down Expand Up @@ -326,7 +326,7 @@ export function canReservationTimeBeChanged({
newReservation,
reservableTimes,
reservationUnit,
activeApplicationRounds = [],
activeApplicationRounds,
}: CanReservationBeChangedProps): boolean {
if (reservation == null) {
return false;
Expand Down
17 changes: 4 additions & 13 deletions apps/ui/pages/reservation-unit/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ApplicationRoundPeriodsQuery>({
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;
Expand All @@ -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;

Expand Down
15 changes: 3 additions & 12 deletions apps/ui/pages/reservations/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -198,6 +186,9 @@ function ReservationEditPage(props: PropsNarrowed): JSX.Element {
},
];

// TODO does this include non active application rounds?
const activeApplicationRounds = reservationUnit.applicationRounds;

return (
<ReservationPageWrapper>
<HeadingSection>
Expand Down

0 comments on commit afe51d1

Please sign in to comment.