Skip to content

Commit

Permalink
Update new school page to use updateOnSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
veronikasif committed Jan 22, 2025
1 parent 91ed8b9 commit 941f132
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
buildAsyncSelectField,
buildHiddenInputWithWatchedValue,
buildMultiField,
buildSubSection,
coreErrorMessages,
Expand All @@ -24,11 +23,11 @@ export const newSchoolSubSection = buildSubSection({
},
children: [
buildMultiField({
id: 'school',
id: 'newSchool',
title: newPrimarySchoolMessages.primarySchool.newSchoolSubSectionTitle,
children: [
buildAsyncSelectField({
id: 'schools.newSchool.municipality',
id: 'newSchool.municipality',
title: newPrimarySchoolMessages.shared.municipality,
placeholder: newPrimarySchoolMessages.shared.municipalityPlaceholder,
loadingError: coreErrorMessages.failedDataProvider,
Expand All @@ -48,12 +47,13 @@ export const newSchoolSubSection = buildSubSection({
},
}),
buildAsyncSelectField({
id: 'schools.newSchool.school',
id: 'newSchool.school',
title: newPrimarySchoolMessages.shared.school,
placeholder: newPrimarySchoolMessages.shared.schoolPlaceholder,
loadingError: coreErrorMessages.failedDataProvider,
dataTestId: 'new-school-school',
loadOptions: async ({ application, apolloClient }) => {
updateOnSelect: ['newSchool.municipality'],
loadOptions: async ({ application, apolloClient, selectedValue }) => {
const { schoolMunicipality } = getApplicationAnswers(
application.answers,
)
Expand All @@ -68,7 +68,11 @@ export const newSchoolSubSection = buildSubSection({

return (
data?.friggSchoolsByMunicipality
?.find(({ name }) => name === schoolMunicipality)
?.find(
({ name }) =>
name ===
(selectedValue ? selectedValue : schoolMunicipality),
)
?.children?.filter((school) =>
school.gradeLevels?.includes(childGradeLevel),
)
Expand All @@ -79,20 +83,11 @@ export const newSchoolSubSection = buildSubSection({
)
},
condition: (answers) => {
const { schoolMunicipality, newSchoolHiddenInput } =
getApplicationAnswers(answers)
const { schoolMunicipality } = getApplicationAnswers(answers)

return (
!!schoolMunicipality &&
schoolMunicipality === newSchoolHiddenInput
)
return !!schoolMunicipality
},
}),
buildHiddenInputWithWatchedValue({
// Needed to trigger an update on loadOptions in the async select above
id: 'schools.newSchool.hiddenInput',
watchValue: 'schools.newSchool.municipality',
}),
],
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ export const dataSchema = z.object({
path: ['transferOfLegalDomicile', 'postalCode'],
},
),
schools: z.object({
newSchool: z.object({
municipality: z.string(),
school: z.string(),
}),
newSchool: z.object({
municipality: z.string(),
school: z.string(),
}),
siblings: z
.array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { Locale } from '@island.is/shared/types'
import {
Child,
ChildInformation,
ContactsRow,
FriggChildInformation,
Membership,
Parents,
Person,
ContactsRow,
SelectOption,
SiblingsRow,
} from '../types'
Expand Down Expand Up @@ -145,18 +145,10 @@ export const getApplicationAnswers = (answers: Application['answers']) => {

const schoolMunicipality = getValueViaPath(
answers,
'schools.newSchool.municipality',
'newSchool.municipality',
) as string

const selectedSchool = getValueViaPath(
answers,
'schools.newSchool.school',
) as string

const newSchoolHiddenInput = getValueViaPath(
answers,
'schools.newSchool.hiddenInput',
) as string
const selectedSchool = getValueViaPath(answers, 'newSchool.school') as string

return {
childNationalId,
Expand Down Expand Up @@ -189,7 +181,6 @@ export const getApplicationAnswers = (answers: Application['answers']) => {
startDate,
schoolMunicipality,
selectedSchool,
newSchoolHiddenInput,
}
}

Expand Down

0 comments on commit 941f132

Please sign in to comment.