-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(driving-license): start on advanced driving-license #16647
Open
stjanilofts
wants to merge
4
commits into
main
Choose a base branch
from
feat/dl-advanced
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+501
−6
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
157 changes: 157 additions & 0 deletions
157
libs/application/templates/driving-license/src/fields/AdvancedLicense/AdvancedLicense.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,157 @@ | ||
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' | ||
import { joinWithAnd } from '../../lib/utils' | ||
|
||
const AdvancedLicense: 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 s1arr = options.map((option) => { | ||
return option.code | ||
}) | ||
|
||
const s1 = joinWithAnd(s1arr) | ||
|
||
const s2 = options.find((x) => x.minAge)?.minAge | ||
|
||
const requiredAgeText = | ||
s1 && | ||
s2 && | ||
formatMessage(m[`applicationForAdvancedAgeRequired`], { | ||
licenses: s1, | ||
age: String(s2), | ||
}) | ||
|
||
return ( | ||
<Box | ||
key={`license-group-${index}`} | ||
marginTop={index === 0 ? 2 : 5} | ||
marginBottom={5} | ||
> | ||
{requiredAgeText && ( | ||
<Box marginBottom={2}> | ||
<Text variant="medium" as="div"> | ||
{requiredAgeText} | ||
</Text> | ||
</Box> | ||
)} | ||
{options.map((option, index) => { | ||
const name = `field-${option.code}` | ||
|
||
return ( | ||
<Box | ||
key={index} | ||
marginBottom={2} | ||
stjanilofts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paddingX={3} | ||
paddingTop={2} | ||
paddingBottom={3} | ||
background="blue100" | ||
borderRadius="large" | ||
border="standard" | ||
> | ||
<Text as="div" marginBottom={2}> | ||
{formatMessage( | ||
m[`applicationForAdvancedLicenseTitle${option.code}`], | ||
)} | ||
</Text> | ||
<Checkbox | ||
label={formatMessage( | ||
m[`applicationForAdvancedLicenseLabel${option.code}`], | ||
)} | ||
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] | ||
}) | ||
}} | ||
Comment on lines
+94
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Simplify checkbox handlers using callback utilities. The onChange handlers contain complex logic that could be simplified for better maintainability. const handleLicenseToggle = useCallback((code: string, professional?: string) => (checked: boolean) => {
setSelectedLicenses(prev => {
if (!checked) {
return prev.filter(item => item !== code && item !== professional);
}
return professional ? [...prev, professional] : [...prev, code];
});
}, []); Then use it in the JSX: -onChange={() => {
- setSelectedLicenses((prev) => {
- return prev.includes(option.code)
- ? prev
- .filter((item) => item !== option.code)
- .filter(
- (item) => item !== option.professional?.code,
- )
- : [...prev, option.code]
- })
-}}
+onChange={(e) => handleLicenseToggle(option.code, option.professional?.code)(e.target.checked)} Also applies to: 128-138 |
||
/> | ||
{option?.professional?.code && ( | ||
<Box | ||
key={`professional-${option.professional.code}`} | ||
marginTop={2} | ||
paddingX={3} | ||
paddingY={2} | ||
background="blue100" | ||
borderRadius="large" | ||
border="standard" | ||
> | ||
<Checkbox | ||
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 { AdvancedLicense } |
1 change: 1 addition & 0 deletions
1
libs/application/templates/driving-license/src/fields/AdvancedLicense/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 { AdvancedLicense } from './AdvancedLicense' |
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.applicationForAdvancedLicenseTitle, | ||
description: m.applicationForAdvancedLicenseDescription, | ||
children: [ | ||
buildCustomField({ | ||
id: 'advancedLicense', | ||
title: 'Advanced License', | ||
component: 'AdvancedLicense', | ||
}), | ||
stjanilofts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
}), | ||
], | ||
}) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Optimize hook usage to prevent unnecessary rerenders.
The current implementation could cause unnecessary rerenders due to the watch hook and effect combination.
📝 Committable suggestion