-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(driving-license): start on advanced driving-license (#16647)
* start on advanced driving-license * code rabbit comment changes * more comment changes * change * minor tweaks - advanced license * tweaks * text tweak --------- Co-authored-by: albinagu <[email protected]> Co-authored-by: albina <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b9cf31e
commit 9d63937
Showing
12 changed files
with
512 additions
and
6 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
130 changes: 130 additions & 0 deletions
130
...emplates/driving-license/src/fields/AdvancedLicenseSelection/AdvancedLicenseSelection.tsx
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,130 @@ | ||
import React, { FC, useEffect, useState } from 'react' | ||
|
||
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 { | ||
organizedAdvancedLicenseMap, | ||
AdvancedLicense as AdvancedLicenseEnum, | ||
} from '../../lib/constants' | ||
import { useLocale } from '@island.is/localization' | ||
import { m } from '../../lib/messages' | ||
|
||
const AdvancedLicenseSelection: FC<React.PropsWithChildren<FieldBaseProps>> = ({ | ||
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(() => { | ||
setValue('advancedLicense', selectedLicenses) | ||
}, [selectedLicenses, setValue]) | ||
|
||
return ( | ||
<Box> | ||
{Object.entries(organizedAdvancedLicenseMap).map(([, options], index) => { | ||
const group = options.find((x) => x.group)?.group | ||
const groupAge = options.find((x) => x.minAge)?.minAge | ||
|
||
return ( | ||
<Box | ||
key={`license-group-${index}`} | ||
marginTop={index === 0 ? 2 : 7} | ||
marginBottom={5} | ||
> | ||
<Box marginBottom={2}> | ||
<Text variant="h4"> | ||
{group ? formatMessage(m[`groupTitle${group}`]) : ''} | ||
</Text> | ||
<Text variant="medium" as="div"> | ||
{formatMessage(m[`applicationForAdvancedAgeRequired`], { | ||
age: String(groupAge), | ||
})} | ||
</Text> | ||
</Box> | ||
{options.map((option) => { | ||
const name = `field-${option.code}` | ||
|
||
return ( | ||
<Box key={`license-option-${option.code}`} marginBottom={4}> | ||
<Checkbox | ||
label={formatMessage( | ||
m[`applicationForAdvancedLicenseTitle${option.code}`], | ||
)} | ||
subLabel={formatMessage( | ||
m[`applicationForAdvancedLicenseLabel${option.code}`], | ||
)} | ||
large | ||
id={name} | ||
name={name} | ||
backgroundColor="blue" | ||
labelVariant="medium" | ||
checked={advancedLicenseValue.includes(option.code)} | ||
onChange={() => { | ||
setSelectedLicenses((prev) => { | ||
return prev.includes(option.code) | ||
? prev | ||
.filter((item) => item !== option.code) | ||
.filter( | ||
(item) => item !== option.professional?.code, | ||
) | ||
: [...prev, option.code] | ||
}) | ||
}} | ||
/> | ||
{option?.professional?.code && ( | ||
<Box marginTop={1}> | ||
<Checkbox | ||
large | ||
key={`professional-${option.professional.code}`} | ||
disabled={!selectedLicenses.includes(option?.code)} | ||
label={formatMessage( | ||
m[ | ||
`applicationForAdvancedLicenseLabel${option.professional.code}` | ||
], | ||
)} | ||
backgroundColor="blue" | ||
labelVariant="small" | ||
checked={advancedLicenseValue.includes( | ||
option?.professional?.code, | ||
)} | ||
onChange={(e) => { | ||
setSelectedLicenses((prev) => { | ||
if (e.target.checked && option.professional?.code) { | ||
return [...prev, option.professional.code] | ||
} | ||
|
||
return prev.filter( | ||
(item) => item !== option.professional?.code, | ||
) | ||
}) | ||
}} | ||
/> | ||
</Box> | ||
)} | ||
</Box> | ||
) | ||
})} | ||
</Box> | ||
) | ||
})} | ||
{!selectedLicenses?.length && requiredMessage && ( | ||
<ErrorMessage> | ||
<div>{requiredMessage}</div> | ||
</ErrorMessage> | ||
)} | ||
</Box> | ||
) | ||
} | ||
|
||
export { AdvancedLicenseSelection } |
1 change: 1 addition & 0 deletions
1
libs/application/templates/driving-license/src/fields/AdvancedLicenseSelection/index.tsx
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 @@ | ||
export { AdvancedLicenseSelection } from './AdvancedLicenseSelection' |
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
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
35 changes: 35 additions & 0 deletions
35
...tion/templates/driving-license/src/forms/prerequisites/sectionAdvancedLicenseSelection.ts
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,35 @@ | ||
import { | ||
buildCustomField, | ||
buildMultiField, | ||
buildSubSection, | ||
getValueViaPath, | ||
} from '@island.is/application/core' | ||
import { m } from '../../lib/messages' | ||
import { LicenseTypes } from '../../lib/constants' | ||
|
||
export const sectionAdvancedLicenseSelection = buildSubSection({ | ||
id: 'sectionAdvancedLicenseSelection', | ||
title: m.applicationForAdvancedLicenseTitle, | ||
condition: (answers) => { | ||
const applicationFor = getValueViaPath<LicenseTypes>( | ||
answers, | ||
'applicationFor', | ||
) | ||
|
||
return applicationFor != null && applicationFor === LicenseTypes.B_ADVANCED | ||
}, | ||
children: [ | ||
buildMultiField({ | ||
id: 'advancedLicenseSelectionFields', | ||
title: m.applicationForAdvancedLicenseSectionTitle, | ||
description: m.applicationForAdvancedLicenseSectionDescription, | ||
children: [ | ||
buildCustomField({ | ||
id: 'advancedLicense', | ||
title: '', | ||
component: 'AdvancedLicenseSelection', | ||
}), | ||
], | ||
}), | ||
], | ||
}) |
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.