@@ -44,6 +44,15 @@ function instanceOfIError(data: IDirectLandings | IError[]): data is IError[] {
4444 return Array . isArray ( data ) && "key" in data [ 0 ] ;
4545}
4646
47+ // helper function to ensure form number is actually a number
48+ function isNumeric ( str : string | number ) {
49+ return (
50+ / ^ - ? [ \d . ] + $ / . test ( str as string ) && // ensure only numbers and dots(negative numbers will be caught later validation)
51+ ! Number . isNaN ( str ) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
52+ ! Number . isNaN ( Number . parseFloat ( str as string ) )
53+ ) ; // ...and ensure strings of whitespace fail
54+ }
55+
4756const saveActionBase : any = async ( values : any , landings : IDirectLandings , isNumeric : ( i : any ) => boolean ) => {
4857 const filterWeights : { [ key : string ] : string } = Object . keys ( values )
4958 . filter ( ( key ) => key . includes ( "weight" ) )
@@ -69,7 +78,7 @@ const saveActionBase: any = async (values: any, landings: IDirectLandings, isNum
6978 // This prevents invalid date errors from causing spurious vessel errors
7079 const isDateValid = isValidDate ( date , [ "YYYY-M-D" , "YYYY-MM-DD" ] ) ;
7180 const dateForVesselLookup = isDateValid ? date : moment ( ) . format ( "YYYY-MM-DD" ) ;
72- const vessels : IVessel [ ] = ! isEmpty ( pln ) ? await getVessels ( pln . toString ( ) , dateForVesselLookup ) : [ ] ;
81+ const vessels : IVessel [ ] = isEmpty ( pln ) ? [ ] : await getVessels ( pln . toString ( ) , dateForVesselLookup ) ;
7382 const selectedVessel : IVessel | undefined = vessels . find ( ( _ : IVessel ) => _ . pln === pln ) ;
7483 const previousVessel : ( IVessel & { isListed ?: boolean } ) | undefined = { } ;
7584 let exclusiveEconomicZones : ICountry [ ] = [ ] ;
@@ -395,7 +404,7 @@ export const DirectLandingsLoader = async (params: Params, request: Request) =>
395404 ( totals : number , weight : IDirectLandingsDetails ) =>
396405 weight . exportWeight === undefined || weight . exportWeight === null || ! isNumber ( weight . exportWeight )
397406 ? totals
398- : totals + parseFloat ( weight . exportWeight ) ,
407+ : totals + Number . parseFloat ( weight . exportWeight ) ,
399408 0
400409 )
401410 : 0 ;
@@ -434,7 +443,7 @@ export const DirectLandingsLoader = async (params: Params, request: Request) =>
434443 selectedRfmo : directLandings ?. rfmo ,
435444 availableExclusiveEconomicZones,
436445 selectedExclusiveEconomicZones : directLandings ?. exclusiveEconomicZones ,
437- maximumEezPerLanding : parseInt ( maximumEezPerLanding , 10 ) ,
446+ maximumEezPerLanding : Number . parseInt ( maximumEezPerLanding , 10 ) ,
438447 isAddAnotherEEZButtonClicked : isAddAnotherEEZButtonClicked ,
439448 faoArea : getSessionData ( session , "selectedFaoArea" ) ,
440449 } ) ,
@@ -461,14 +470,6 @@ export const DirectLandingsAction = async (params: Params, request: Request): Pr
461470 const isValid = await validateCSRFToken ( request , form ) ;
462471 if ( ! isValid ) return redirect ( "/forbidden" ) ;
463472 const saveToSession = ( data : any ) => Object . keys ( data ) . forEach ( ( k ) => session . set ( k , data [ k ] ) ) ;
464- // helper function to ensure form number is actually a number
465- function isNumeric ( str : string | number ) {
466- return (
467- / ^ - ? [ \d . ] + $ / . test ( str as string ) && // ensure only numbers and dots(negative numbers will be caught later validation)
468- ! isNaN ( str as number ) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
469- ! isNaN ( parseFloat ( str as string ) )
470- ) ; // ...and ensure strings of whitespace fail
471- }
472473 const {
473474 dateLandedDay,
474475 dateLandedMonth,
0 commit comments