Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 61d5989

Browse files
committed
fix: ui sending a new application
1 parent df8b43e commit 61d5989

3 files changed

Lines changed: 30 additions & 37 deletions

File tree

apps/ui/components/application/Form.tsx

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
type SuitableTimeRangeNode,
1111
Weekday,
1212
Priority,
13-
type ApplicationSectionUpdateMutationInput,
14-
type ApplicationSectionCreateMutationInput,
13+
type UpdateApplicationSectionForApplicationSerializerInput,
1514
} from "common/types/gql-types";
1615
import { type Maybe } from "graphql/jsutils/Maybe";
1716
import { z } from "zod";
@@ -60,6 +59,7 @@ export type SuitableTimeRangeFormValues = z.infer<
6059
typeof SuitableTimeRangeFormTypeSchema
6160
>;
6261

62+
// TODO this should be splitted into separate schemas for each page
6363
const ApplicationSectionFormValueSchema = z
6464
.object({
6565
pk: z.number().optional(),
@@ -78,7 +78,8 @@ const ApplicationSectionFormValueSchema = z
7878
.string()
7979
.optional()
8080
.refine((s) => s, { path: [""], message: "Required" }),
81-
suitableTimeRanges: z.array(SuitableTimeRangeFormTypeSchema),
81+
// optional because it's not present on the first page
82+
suitableTimeRanges: z.array(SuitableTimeRangeFormTypeSchema).optional(),
8283
// TODO do we want to keep the pk of the options? so we can update them when the order changes and not recreate the whole list on save?
8384
reservationUnits: z.array(z.number()).min(1, { message: "Required" }),
8485
// extra page prop, not saved to backend
@@ -273,17 +274,16 @@ function transformSuitableTimeRange(timeRange: SuitableTimeRangeFormValues) {
273274
dayOfTheWeek: timeRange.dayOfTheWeek,
274275
};
275276
}
276-
// create and update mutation are somewhat different so bit logic to handle both
277+
278+
// NOTE this works only for subsections of an application mutation
279+
// if needed without an application mutation needs to use a different SerializerInput
277280
function transformApplicationSection(
278-
ae: ApplicationSectionFormValue,
279-
application: number
280-
):
281-
| ApplicationSectionUpdateMutationInput
282-
| ApplicationSectionCreateMutationInput {
281+
ae: ApplicationSectionFormValue
282+
): UpdateApplicationSectionForApplicationSerializerInput {
283283
const begin = transformDateString(ae.begin);
284284
const end = transformDateString(ae.end);
285285

286-
const commonData = {
286+
const commonData: UpdateApplicationSectionForApplicationSerializerInput = {
287287
...(begin != null ? { reservationsBeginDate: begin } : {}),
288288
...(end != null ? { reservationsEndDate: end } : {}),
289289
name: ae.name,
@@ -293,31 +293,19 @@ function transformApplicationSection(
293293
reservationMinDuration: ae.minDuration ?? 0, // "3600" == 1h
294294
reservationMaxDuration: ae.maxDuration ?? 0, // "7200" == 2h
295295
appliedReservationsPerWeek: ae.appliedReservationsPerWeek,
296-
suitableTimeRanges: ae.suitableTimeRanges.map(transformSuitableTimeRange),
296+
suitableTimeRanges: ae.suitableTimeRanges?.map(transformSuitableTimeRange),
297297
reservationUnitOptions: ae.reservationUnits.map((ruo, ruoIndex) =>
298298
transformEventReservationUnit(ruo, ruoIndex)
299299
),
300300
};
301301
if (ae.pk != null) {
302-
const data: ApplicationSectionUpdateMutationInput = {
302+
return {
303303
...commonData,
304304
pk: ae.pk,
305305
};
306-
return data;
307-
}
308-
// TODO throw is bad (null return is preferable)
309-
// this should be a validation error (it's incorrect date string)
310-
if (begin == null || end == null) {
311-
throw new Error("begin or end cannot be null");
312306
}
313-
const data: ApplicationSectionCreateMutationInput = {
314-
...commonData,
315-
application,
316-
reservationsBeginDate: begin,
317-
reservationsEndDate: end,
318-
};
319307

320-
return data;
308+
return commonData;
321309
}
322310

323311
// For pages 1 and 2
@@ -328,9 +316,7 @@ export const transformApplication = (
328316
return {
329317
pk: values.pk,
330318
applicantType: values.applicantType,
331-
applicationSections: appEvents.map((ae) =>
332-
transformApplicationSection(ae, values.pk)
333-
),
319+
applicationSections: appEvents.map((ae) => transformApplicationSection(ae)),
334320
};
335321
};
336322

apps/ui/components/application/Page2.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,16 @@ const Page2 = ({ application, onNext }: Props): JSX.Element => {
251251
const convertToSchedule = (
252252
b: (typeof applicationSections)[0]
253253
): ApplicationEventScheduleFormType[] => {
254-
return b.suitableTimeRanges.map((range) => {
255-
return {
256-
day: range ? convertWeekday(range.dayOfTheWeek) : 0,
257-
begin: range?.beginTime ?? "",
258-
end: range?.endTime ?? "",
259-
priority: range?.priority === Priority.Primary ? 300 : 200,
260-
};
261-
});
254+
return (
255+
b.suitableTimeRanges?.map((range) => {
256+
return {
257+
day: range ? convertWeekday(range.dayOfTheWeek) : 0,
258+
begin: range?.beginTime ?? "",
259+
end: range?.endTime ?? "",
260+
priority: range?.priority === Priority.Primary ? 300 : 200,
261+
};
262+
}) ?? []
263+
);
262264
};
263265
const selectorData = applicationSections.map((ae) =>
264266
applicationEventSchedulesToCells(convertToSchedule(ae), openingHours)

apps/ui/modules/queries/application.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { gql } from "@apollo/client";
22
import { APPLICATION_ROUND_FRAGMENT } from "./applicationRound";
33
import { APPLICANT_NAME_FRAGMENT } from "common/src/queries/application";
4+
import { ApplicationOrderingChoices } from "common/types/gql-types";
45

6+
// TODO this doesn't have pagination so the orderBy is for development purposes only
7+
// in production the order isn't specified and pagination is not needed
8+
// (there is never even close to 100 applications for single user)
9+
// but in development we need to be able to see the latest applications.
510
export const APPLICATIONS = gql`
611
${APPLICATION_ROUND_FRAGMENT}
712
${APPLICANT_NAME_FRAGMENT}
813
query Applications($user: Int!, $status: [ApplicationStatusChoice]!) {
9-
applications(user: $user, status: $status) {
14+
applications(user: $user, status: $status, orderBy: ${ApplicationOrderingChoices.PkDesc}) {
1015
edges {
1116
node {
1217
pk

0 commit comments

Comments
 (0)