Skip to content

Commit

Permalink
feat(new-primary-school): Implement language page (#17530)
Browse files Browse the repository at this point in the history
* TS-949 Implement language page

* TS-949 Create language selection

* TS-949 Create language selection
- Get child language box working

* TS-949 Create language selection
- Hide child language if no language is selected

* TS-949 Create language selection
- Adding validation

* TS-949 Create language selection
- Updating overview page

* TS-949 Create language selection
- Update after coderabbit

* TS-949 Create language selection
- Fix after coderabbit review

* TS-949 Create language selection
- Fix after coderabbit review

* TS-949 Create language selection
- Fix after coderabbit review

* TS-949 Create language selection
- Fix margin

* TS-949 Create language selection
- Added required

* TS-949 Create language selection
- Added error

* TS-949 Create language selection
- Simplify code

* TS-949 Create language selection
- Fix after review

* TS-949 Create language selection
- Fix after review

* TS-949 Create language selection
- Fix after coderabbit review
  • Loading branch information
birkirkristmunds authored Jan 24, 2025
1 parent 3431314 commit a8c1c32
Show file tree
Hide file tree
Showing 10 changed files with 532 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ export const transformApplicationToNewPrimarySchoolDTO = (
reasonForApplicationStreetAddress,
reasonForApplicationPostalCode,
selectedSchool,
nativeLanguage,
otherLanguagesSpokenDaily,
otherLanguages,
icelandicNotSpokenAroundChild,
language1,
language2,
language3,
language4,
childLanguage,
languageEnvironment,
signLanguage,
interpreter,
developmentalAssessment,
specialSupport,
startDate,
Expand Down Expand Up @@ -77,17 +81,6 @@ export const transformApplicationToNewPrimarySchoolDTO = (
: []),
]

let noIcelandic: boolean
if (otherLanguagesSpokenDaily === YES) {
if (nativeLanguage === 'is' || otherLanguages?.includes('is')) {
noIcelandic = false
} else {
noIcelandic = icelandicNotSpokenAroundChild?.includes(YES)
}
} else {
noIcelandic = nativeLanguage !== 'is'
}

const newPrimarySchoolDTO: FormDto = {
type: FormDtoTypeEnum.Registration,
user: {
Expand Down Expand Up @@ -133,12 +126,11 @@ export const transformApplicationToNewPrimarySchoolDTO = (
social: {
hasHadSupport: specialSupport === YES,
hasDiagnoses: developmentalAssessment === YES,
},
}, // Languages needs to be updated when Juni is ready with the data struccture
language: {
nativeLanguage: nativeLanguage,
noIcelandic,
otherLanguages:
otherLanguagesSpokenDaily === YES ? otherLanguages : undefined,
nativeLanguage: '',
noIcelandic: false,
otherLanguages: undefined,
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { FieldBaseProps } from '@island.is/application/types'
import React, { FC, useEffect, useState } from 'react'

import { getErrorViaPath } from '@island.is/application/core'
import { Box, Button, Stack, Text } from '@island.is/island-ui/core'
import { useLocale } from '@island.is/localization'
import { SelectController } from '@island.is/shared/form-fields'
import { getAllLanguageCodes } from '@island.is/shared/utils'
import { useFormContext } from 'react-hook-form'
import { LanguageEnvironmentOptions } from '../../lib/constants'
import { newPrimarySchoolMessages } from '../../lib/messages'
import { getApplicationAnswers } from '../../lib/newPrimarySchoolUtils'

export type LanguageSelectionProps = {}

const languagesIds = {
language1: 'languages.language1',
language2: 'languages.language2',
language3: 'languages.language3',
language4: 'languages.language4',
childLanguage: 'languages.childLanguage',
}

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

const [visibleLanguagesCount, setVisibleLanguagesCount] = useState(1)
const [selectedLanguages, setSelectedLanguages] = useState<string[]>([
'',
'',
'',
'',
])

const allLanguages = getAllLanguageCodes()
const allLanguageOptions = allLanguages
.filter((language) => {
if (
language.code === 'is' &&
getValues().languages.languageEnvironment ===
LanguageEnvironmentOptions.ONLY_FOREIGN
) {
return false
}
return true
})
.map((language) => ({
label: language.name,
value: language.code,
}))

const languageIdsArray = [
languagesIds.language1,
languagesIds.language2,
languagesIds.language3,
languagesIds.language4,
]

const addLanguage = () => {
setVisibleLanguagesCount((prev) => Math.min(prev + 1, 4))
}

useEffect(() => {
const { language1, language2, language3, language4 } =
getApplicationAnswers(application.answers)

const selected = [language1, language2, language3, language4].filter(
Boolean,
)

// Ensure the visible count is at least 1
const visibleCount = Math.max(1, selected.length)

setSelectedLanguages(selected)
setVisibleLanguagesCount(visibleCount)
}, [application])

return (
<Stack space={2}>
{languageIdsArray.slice(0, visibleLanguagesCount).map((id, index) => (
<SelectController
key={id}
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.languageSelectionTitle,
{ no: `${index + 1}` },
)}
aria-label={formatMessage(
newPrimarySchoolMessages.differentNeeds.languageSelectionAriaLabel,
{ no: `${index + 1}` },
)}
error={errors && getErrorViaPath(errors, id)}
placeholder={formatMessage(
newPrimarySchoolMessages.differentNeeds
.languageSelectionPlaceholder,
)}
id={id}
name={id}
backgroundColor="blue"
options={allLanguageOptions}
onSelect={({ value }) => {
setSelectedLanguages((prevLanguages) => {
const newLanguages = [...prevLanguages]
newLanguages[index] = value
return newLanguages
})
}}
/>
))}
<Box textAlign={'right'} width="full">
<Button
icon="add"
variant="text"
size="small"
onClick={addLanguage}
disabled={visibleLanguagesCount >= 4}
>
{formatMessage(
newPrimarySchoolMessages.differentNeeds.addLanguageButton,
)}
</Button>
</Box>
<Box
hidden={
selectedLanguages.filter((language) => language !== '').length <= 1
}
>
<Text variant="h4" marginTop={3} marginBottom="gutter">
{formatMessage(
newPrimarySchoolMessages.differentNeeds.childLanguageTitle,
)}
</Text>
<SelectController
key={languagesIds.childLanguage}
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.languageSubSectionTitle,
)}
placeholder={formatMessage(
newPrimarySchoolMessages.differentNeeds
.languageSelectionPlaceholder,
)}
error={errors && getErrorViaPath(errors, languagesIds.childLanguage)}
id={languagesIds.childLanguage}
name={languagesIds.childLanguage}
backgroundColor="blue"
options={allLanguageOptions.filter((language) =>
selectedLanguages.includes(language.value),
)}
/>
</Box>
</Stack>
)
}
export default LanguageSelection
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { YES } from '@island.is/application/types'
import {
DataValue,
RadioValue,
Expand All @@ -7,8 +6,12 @@ import {
import { GridColumn, GridRow, Stack } from '@island.is/island-ui/core'
import { useLocale } from '@island.is/localization'
import { getLanguageByCode } from '@island.is/shared/utils'
import { LanguageEnvironmentOptions } from '../../../lib/constants'
import { newPrimarySchoolMessages } from '../../../lib/messages'
import { getApplicationAnswers } from '../../../lib/newPrimarySchoolUtils'
import {
getApplicationAnswers,
getLanguageEnvironments,
} from '../../../lib/newPrimarySchoolUtils'
import { ReviewGroupProps } from './props'

export const Languages = ({
Expand All @@ -18,14 +21,19 @@ export const Languages = ({
}: ReviewGroupProps) => {
const { formatMessage } = useLocale()
const {
nativeLanguage,
otherLanguagesSpokenDaily,
otherLanguages,
icelandicNotSpokenAroundChild,
interpreter,
languageEnvironment,
language1,
language2,
language3,
language4,
childLanguage,
signLanguage,
} = getApplicationAnswers(application.answers)

const icelandicSelected =
nativeLanguage === 'is' || otherLanguages?.includes('is')
const selectedLanguageEnvironment = getLanguageEnvironments().find(
(env) => env.value === languageEnvironment,
)

return (
<ReviewGroup
Expand All @@ -37,53 +45,107 @@ export const Languages = ({
<GridColumn span="12/12">
<DataValue
label={formatMessage(
newPrimarySchoolMessages.overview.nativeLanguage,
newPrimarySchoolMessages.overview.languageEnvironment,
)}
value={getLanguageByCode(nativeLanguage)?.name}
value={formatMessage(selectedLanguageEnvironment?.label || '')}
/>
</GridColumn>
</GridRow>
<GridRow>
<GridColumn span="12/12">
<RadioValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds
.otherLanguagesSpokenDaily,
)}
value={otherLanguagesSpokenDaily}
/>
</GridColumn>
</GridRow>
{otherLanguagesSpokenDaily === YES && (

{languageEnvironment === LanguageEnvironmentOptions.ONLY_ICELANDIC ? (
<GridRow>
<GridColumn span={['12/12', '12/12', '12/12', '12/12']}>
<RadioValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.signLanguage,
)}
value={signLanguage}
/>
</GridColumn>
</GridRow>
) : (
<>
<GridRow>
<GridColumn span="12/12">
<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<DataValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.languageTitle,
newPrimarySchoolMessages.differentNeeds
.languageSelectionTitle,
{ no: `${1}` },
)}
value={otherLanguages
.map((language) => {
return getLanguageByCode(language)?.name
})
.join(', ')}
value={getLanguageByCode(language1)?.name}
/>
</GridColumn>
{language2 && (
<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<DataValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds
.languageSelectionTitle,
{ no: `${2}` },
)}
value={getLanguageByCode(language2)?.name}
/>
</GridColumn>
)}
</GridRow>
{!icelandicSelected &&
icelandicNotSpokenAroundChild?.includes(YES) && (
<GridRow>
<GridColumn span="12/12">
<DataValue
label={formatMessage(
newPrimarySchoolMessages.overview
.icelandicSpokenAroundChild,
)}
value={formatMessage(newPrimarySchoolMessages.shared.no)}
/>
</GridColumn>
</GridRow>
<GridRow>
{language3 && (
<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<DataValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds
.languageSelectionTitle,
{ no: `${3}` },
)}
value={getLanguageByCode(language3)?.name}
/>
</GridColumn>
)}
{language4 && (
<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<DataValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds
.languageSelectionTitle,
{ no: `${4}` },
)}
value={getLanguageByCode(language4)?.name}
/>
</GridColumn>
)}
</GridRow>
{childLanguage && (
<GridRow>
<GridColumn span={['12/12', '12/12', '12/12', '12/12']}>
<DataValue
label={formatMessage(
newPrimarySchoolMessages.overview.childLanguage,
)}
value={getLanguageByCode(childLanguage)?.name}
/>
</GridColumn>
</GridRow>
)}
<GridRow>
<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<RadioValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.interpreter,
)}
value={interpreter}
/>
</GridColumn>

<GridColumn span={['12/12', '6/12', '6/12', '6/12']}>
<RadioValue
label={formatMessage(
newPrimarySchoolMessages.differentNeeds.signLanguage,
)}
value={signLanguage}
/>
</GridColumn>
</GridRow>
</>
)}
</Stack>
Expand Down
Loading

0 comments on commit a8c1c32

Please sign in to comment.