@@ -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" ;
1615import { type Maybe } from "graphql/jsutils/Maybe" ;
1716import { 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
6363const 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
277280function 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
0 commit comments