diff --git a/libs/application/templates/new-primary-school/src/forms/NewPrimarySchoolForm/primarySchoolSection/newSchoolSubSection.ts b/libs/application/templates/new-primary-school/src/forms/NewPrimarySchoolForm/primarySchoolSection/newSchoolSubSection.ts index fc5449e50716..aeb7a3b51742 100644 --- a/libs/application/templates/new-primary-school/src/forms/NewPrimarySchoolForm/primarySchoolSection/newSchoolSubSection.ts +++ b/libs/application/templates/new-primary-school/src/forms/NewPrimarySchoolForm/primarySchoolSection/newSchoolSubSection.ts @@ -1,6 +1,5 @@ import { buildAsyncSelectField, - buildHiddenInputWithWatchedValue, buildMultiField, buildSubSection, coreErrorMessages, @@ -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, @@ -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, ) @@ -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), ) @@ -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', - }), ], }), ], diff --git a/libs/application/templates/new-primary-school/src/lib/dataSchema.ts b/libs/application/templates/new-primary-school/src/lib/dataSchema.ts index b68780e26358..54ca78c013b7 100644 --- a/libs/application/templates/new-primary-school/src/lib/dataSchema.ts +++ b/libs/application/templates/new-primary-school/src/lib/dataSchema.ts @@ -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( diff --git a/libs/application/templates/new-primary-school/src/lib/newPrimarySchoolUtils.ts b/libs/application/templates/new-primary-school/src/lib/newPrimarySchoolUtils.ts index e243c78bb199..112d2f7b97d5 100644 --- a/libs/application/templates/new-primary-school/src/lib/newPrimarySchoolUtils.ts +++ b/libs/application/templates/new-primary-school/src/lib/newPrimarySchoolUtils.ts @@ -9,11 +9,11 @@ import { Locale } from '@island.is/shared/types' import { Child, ChildInformation, + ContactsRow, FriggChildInformation, Membership, Parents, Person, - ContactsRow, SelectOption, SiblingsRow, } from '../types' @@ -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, @@ -189,7 +181,6 @@ export const getApplicationAnswers = (answers: Application['answers']) => { startDate, schoolMunicipality, selectedSchool, - newSchoolHiddenInput, } }