-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split up AreaCoordinates file.
- Loading branch information
1 parent
49b8619
commit 69a39c0
Showing
5 changed files
with
194 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* eslint-disable max-lines-per-function */ | ||
import { ModelAreaTypeDto } from '../../api/generated'; | ||
import { AreaCoordinateType } from './AreaCoordinates'; | ||
|
||
export type CoordinateErrorType = { | ||
area?: string; | ||
x0?: string; | ||
y0?: string; | ||
x1?: string; | ||
y1?: string; | ||
}; | ||
|
||
function NotANumber(value: any) { | ||
return isNaN(value); | ||
} | ||
export const validateCoordinates = async ( | ||
area: AreaCoordinateType | undefined, | ||
activeArea: ModelAreaTypeDto, | ||
) => { | ||
const errors: CoordinateErrorType = {}; | ||
if (!activeArea || activeArea.modelAreaTypeId === '') { | ||
errors.area = 'Model area needs to be selected'; | ||
} | ||
|
||
if (area && area.coordinates[0].x === area.coordinates[1].x) { | ||
errors.x0 = 'X coordinates can´t be equal.'; | ||
} | ||
if (area && area.coordinates[0].y === area.coordinates[1].y) { | ||
errors.y0 = 'Y coordinates can´t be equal.'; | ||
} | ||
if (area && NotANumber(area.coordinates[0].x)) { | ||
errors.x0 = 'Coordinates can´t be string, just numbers are allowed.'; | ||
} | ||
if (area && NotANumber(area.coordinates[0].y)) { | ||
errors.y0 = 'Coordinates can´t be string, just numbers are allowed.'; | ||
} | ||
if (area && NotANumber(area.coordinates[1].x)) { | ||
errors.x1 = 'Coordinates can´t be string, just numbers are allowed.'; | ||
} | ||
if (area && NotANumber(area.coordinates[1].y)) { | ||
errors.y1 = 'Coordinates can´t be string, just numbers are allowed.'; | ||
} | ||
if (area && area.coordinates[1].x === 0) { | ||
errors.x1 = 'Bottom right corner can not be 0.'; | ||
} | ||
|
||
if ( | ||
area && | ||
(area.coordinates[0].x === null || | ||
area.coordinates[0].x === undefined || | ||
// @ts-expect-error Autocomplete | ||
area.coordinates[0].x === '') | ||
) { | ||
errors.x0 = 'All fields must be filled in'; | ||
} | ||
|
||
if ( | ||
area && | ||
(area.coordinates[0].y === null || | ||
area.coordinates[0].y === undefined || | ||
// @ts-expect-error Autocomplete | ||
area.coordinates[0].y === '') | ||
) { | ||
errors.y0 = 'All fields must be filled in'; | ||
} | ||
|
||
if ( | ||
area && | ||
(area.coordinates[1].x === null || | ||
area.coordinates[1].x === undefined || | ||
// @ts-expect-error Autocomplete | ||
area.coordinates[1].x === '') | ||
) { | ||
errors.x1 = 'All fields must be filled in'; | ||
} | ||
|
||
if ( | ||
area && | ||
(area.coordinates[1].y === null || | ||
area.coordinates[1].y === undefined || | ||
// @ts-expect-error Autocomplete | ||
area.coordinates[1].y === '') | ||
) { | ||
errors.y1 = 'All fields must be filled in'; | ||
} | ||
|
||
return errors; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.