From 15edb19c9aa56daf47115b91a23b1010efd23331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigr=C3=BAn=20Tinna=20Gissurard=C3=B3ttir?= <39527334+sigruntg@users.noreply.github.com> Date: Thu, 19 Dec 2024 10:24:04 +0000 Subject: [PATCH] feat(register-new-machine): making location required if category is of certain type (#17276) * Making location required if category is of certain type * making const --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../src/fields/LocationInputField/index.tsx | 50 +++++++++++++++++++ .../register-new-machine/src/fields/index.ts | 1 + .../MachineSection/MachineBasicInformation.ts | 6 +-- .../register-new-machine/tsconfig.lib.json | 31 ++++++------ 4 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 libs/application/templates/aosh/register-new-machine/src/fields/LocationInputField/index.tsx diff --git a/libs/application/templates/aosh/register-new-machine/src/fields/LocationInputField/index.tsx b/libs/application/templates/aosh/register-new-machine/src/fields/LocationInputField/index.tsx new file mode 100644 index 000000000000..37a39cf795b5 --- /dev/null +++ b/libs/application/templates/aosh/register-new-machine/src/fields/LocationInputField/index.tsx @@ -0,0 +1,50 @@ +import { FieldBaseProps } from '@island.is/application/types' +import { FC, useState } from 'react' +import { useFormContext } from 'react-hook-form' +import { coreErrorMessages } from '@island.is/application/core' +import { AboutMachine } from '../../lib/dataSchema' +import { Box } from '@island.is/island-ui/core' +import { InputController } from '@island.is/shared/form-fields' +import { useLocale } from '@island.is/localization' +import { machine } from '../../lib/messages' + +export const LocationInputField: FC> = ( + props, +) => { + const { field, setBeforeSubmitCallback } = props + const { formatMessage } = useLocale() + const { watch } = useFormContext() + const [displayError, setDisplayError] = useState(false) + const watchMachine = watch('machine.aboutMachine') as AboutMachine + const location = watch(field.id) as string + const categoryValue = 'Fólkslyftur og vörulyftur' + + setBeforeSubmitCallback?.(async () => { + if ( + watchMachine.category?.nameIs === categoryValue && + location.length === 0 + ) { + setDisplayError(true) + return [false, ''] + } + return [true, null] + }) + + return ( + + setDisplayError(false)} + error={ + displayError && location.length === 0 + ? formatMessage(coreErrorMessages.defaultError) + : undefined + } + /> + + ) +} diff --git a/libs/application/templates/aosh/register-new-machine/src/fields/index.ts b/libs/application/templates/aosh/register-new-machine/src/fields/index.ts index cf08b813c38b..b33672f5ec00 100644 --- a/libs/application/templates/aosh/register-new-machine/src/fields/index.ts +++ b/libs/application/templates/aosh/register-new-machine/src/fields/index.ts @@ -3,3 +3,4 @@ export { Overview } from './Overview' export { AboutMachine } from './AboutMachine' export { TechnicalInfo } from './TechnicalInfo' export { ChangeAnswers } from './ChangeAnswers' +export { LocationInputField } from './LocationInputField' diff --git a/libs/application/templates/aosh/register-new-machine/src/forms/RegisterMachineForm/MachineSection/MachineBasicInformation.ts b/libs/application/templates/aosh/register-new-machine/src/forms/RegisterMachineForm/MachineSection/MachineBasicInformation.ts index 22afa79349c9..716b3af34f9e 100644 --- a/libs/application/templates/aosh/register-new-machine/src/forms/RegisterMachineForm/MachineSection/MachineBasicInformation.ts +++ b/libs/application/templates/aosh/register-new-machine/src/forms/RegisterMachineForm/MachineSection/MachineBasicInformation.ts @@ -122,11 +122,11 @@ export const MachineBasicInformation = buildSubSection({ titleVariant: 'h5', marginTop: 3, }), - buildTextField({ + buildCustomField({ id: 'machine.basicInformation.location', - title: machine.labels.basicMachineInformation.location, + title: '', width: 'half', - maxLength: 255, + component: 'LocationInputField', }), buildTextField({ id: 'machine.basicInformation.cargoFileNumber', diff --git a/libs/application/templates/aosh/register-new-machine/tsconfig.lib.json b/libs/application/templates/aosh/register-new-machine/tsconfig.lib.json index 8d36d2eaf5af..cfc356e7214b 100644 --- a/libs/application/templates/aosh/register-new-machine/tsconfig.lib.json +++ b/libs/application/templates/aosh/register-new-machine/tsconfig.lib.json @@ -2,23 +2,22 @@ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../../../../dist/out-tsc", - "types": [ - "node", - - "@nx/react/typings/cssmodule.d.ts", - "@nx/react/typings/image.d.ts" - ] + "types": ["node"] }, + "files": [ + "../../../../../node_modules/@nx/react/typings/cssmodule.d.ts", + "../../../../../node_modules/@nx/react/typings/image.d.ts" + ], "exclude": [ - "jest.config.ts", - "src/**/*.spec.ts", - "src/**/*.test.ts", - "src/**/*.spec.tsx", - "src/**/*.test.tsx", - "src/**/*.spec.js", - "src/**/*.test.js", - "src/**/*.spec.jsx", - "src/**/*.test.jsx" + "**/*.spec.ts", + "**/*.test.ts", + "**/*.spec.tsx", + "**/*.test.tsx", + "**/*.spec.js", + "**/*.test.js", + "**/*.spec.jsx", + "**/*.test.jsx", + "jest.config.ts" ], - "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] + "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] }