-
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.
- Loading branch information
1 parent
96b60a1
commit 5c71287
Showing
14 changed files
with
498 additions
and
9 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
150 changes: 150 additions & 0 deletions
150
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,150 @@ | ||
import React, { FC, useEffect, useState } from 'react' | ||
|
||
import { Box, Checkbox, 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>> = ({ | ||
application, | ||
}) => { | ||
console.log('application', application) | ||
|
||
const { formatMessage } = useLocale() | ||
const { setValue, watch } = useFormContext() | ||
|
||
const advancedLicenseValue = watch('advancedLicense') ?? [] | ||
|
||
const [selectedLicenses, setSelectedLicenses] = | ||
useState<Array<keyof typeof AdvancedLicenseEnum>>(advancedLicenseValue) | ||
|
||
useEffect(() => { | ||
if (selectedLicenses.length > 0) { | ||
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`]) | ||
?.replace('%1', s1) | ||
?.replace('%2', String(s2)) | ||
|
||
return ( | ||
<Box 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} | ||
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={(e) => { | ||
const checked = e.target.checked | ||
console.log('checked?', checked) | ||
|
||
setSelectedLicenses((prev) => { | ||
return prev.includes(option.code) | ||
? prev | ||
.filter((item) => item !== option.code) | ||
.filter( | ||
(item) => item !== option.professional?.code, | ||
) | ||
: [...prev, option.code] | ||
}) | ||
}} | ||
/> | ||
{option?.professional && ( | ||
<Box | ||
key={index} | ||
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}` | ||
], | ||
)} | ||
id="hey44" | ||
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> | ||
) | ||
})} | ||
</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
57 changes: 57 additions & 0 deletions
57
...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,57 @@ | ||
import { | ||
buildCustomField, | ||
buildKeyValueField, | ||
buildMultiField, | ||
buildRadioField, | ||
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 === LicenseTypes.B_ADVANCED | ||
}, | ||
children: [ | ||
buildMultiField({ | ||
id: 'advancedLicenseSelectionFields', | ||
title: m.applicationForAdvancedLicenseTitle, | ||
description: m.applicationForAdvancedLicenseDescription, | ||
children: [ | ||
buildCustomField({ | ||
id: 'advancedLicense', | ||
title: 'Advanced License', | ||
component: 'AdvancedLicense', | ||
}), | ||
// buildRadioField({ | ||
// id: 'advancedLicense', | ||
// title: '', | ||
// backgroundColor: 'blue', | ||
// largeButtons: true, | ||
// options: (app) => { | ||
// console.log(app) | ||
// let options = [] | ||
|
||
// options = Object.values(AdvancedLicenses).map((license) => { | ||
// return { | ||
// label: m[`applicationForAdvancedLicenseApplyFor${license}`], | ||
// value: AdvancedLicenses[license], | ||
// disabled: false, | ||
// } | ||
// }) | ||
|
||
// return options | ||
// }, | ||
// }), | ||
], | ||
}), | ||
], | ||
}) |
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
Oops, something went wrong.