Skip to content

Commit

Permalink
more comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stjanilofts committed Oct 31, 2024
1 parent 25e9e21 commit c43faab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC, useEffect, useState } from 'react'

import { Box, Checkbox, Text } from '@island.is/island-ui/core'
import { Box, Checkbox, ErrorMessage, Text } from '@island.is/island-ui/core'
import { FieldBaseProps } from '@island.is/application/types'
import { useFormContext } from 'react-hook-form'
import {
Expand All @@ -12,20 +12,23 @@ import { m } from '../../lib/messages'
import { joinWithAnd } from '../../lib/utils'

const AdvancedLicense: FC<React.PropsWithChildren<FieldBaseProps>> = ({
application,
errors,
}) => {
const { formatMessage } = useLocale()
const { setValue, watch } = useFormContext()

const requiredMessage = (errors as { advancedLicense?: string })
?.advancedLicense
? formatMessage(m.applicationForAdvancedRequiredError)
: ''

const advancedLicenseValue = watch('advancedLicense') ?? []

const [selectedLicenses, setSelectedLicenses] =
useState<Array<keyof typeof AdvancedLicenseEnum>>(advancedLicenseValue)

useEffect(() => {
if (selectedLicenses.length > 0) {
setValue('advancedLicense', selectedLicenses)
}
setValue('advancedLicense', selectedLicenses)
}, [selectedLicenses, setValue])

return (
Expand Down Expand Up @@ -88,9 +91,7 @@ const AdvancedLicense: FC<React.PropsWithChildren<FieldBaseProps>> = ({
backgroundColor="blue"
labelVariant="medium"
checked={advancedLicenseValue.includes(option.code)}
onChange={(e) => {
const checked = e.target.checked

onChange={() => {
setSelectedLicenses((prev) => {
return prev.includes(option.code)
? prev
Expand All @@ -102,9 +103,9 @@ const AdvancedLicense: FC<React.PropsWithChildren<FieldBaseProps>> = ({
})
}}
/>
{option?.professional && (
{option?.professional?.code && (
<Box
key={index}
key={`professional-${option.professional.code}`}
marginTop={2}
paddingX={3}
paddingY={2}
Expand Down Expand Up @@ -144,6 +145,11 @@ const AdvancedLicense: FC<React.PropsWithChildren<FieldBaseProps>> = ({
</Box>
)
})}
{!selectedLicenses?.length && requiredMessage && (
<ErrorMessage>
<div>{requiredMessage}</div>
</ErrorMessage>
)}
</Box>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import { sectionExistingApplication } from './sectionExistingApplication'
import { sectionDigitalLicenseInfo } from './sectionDigitalLicenseInfo'
import { sectionAdvancedLicenseSelection } from './sectionAdvancedLicenseSelection'

interface DrivingLicenseFormConfig {
allowFakeData?: boolean
allowPickLicense?: boolean
allowBELicense?: boolean
allow65Renewal?: boolean
allowAdvanced?: boolean
}

export const getForm = ({
allowFakeData = false,
allowPickLicense = false,
allowBELicense = false,
allow65Renewal = false,
allowAdvanced = false,
}): Form =>
}: DrivingLicenseFormConfig): Form =>
buildForm({
id: 'DrivingLicenseApplicationPrerequisitesForm',
title: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export const dataSchema = z.object({
applicationFor: z.enum([B_FULL, B_TEMP, BE, B_FULL_RENEWAL_65, B_ADVANCED]),
advancedLicense: z
.array(z.enum(Object.values(AdvancedLicense) as [string, ...string[]]))
.nonempty(),
.nonempty()
.refine((value) => {
return value.length > 0
}),
email: z.string().email(),
phone: z.string().refine((v) => isValidPhoneNumber(v)),
drivingInstructor: z.string().min(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,11 @@ export const m = defineMessages({
defaultMessage: 'DE lýsing',
description: 'DE description',
},
applicationForAdvancedRequiredError: {
id: 'dl.application:applicationForAdvancedRequiredError',
defaultMessage: 'Þú verður að velja að minnsta kosti einn valmöguleika',
description: 'You must select at least one option',
},
})

export const requirementsMessages = defineMessages({
Expand Down

0 comments on commit c43faab

Please sign in to comment.