Skip to content
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

fix(application-system): Fix typing for updateOnSelect #17666

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const newSchoolSubSection = buildSubSection({
placeholder: newPrimarySchoolMessages.shared.schoolPlaceholder,
loadingError: coreErrorMessages.failedDataProvider,
dataTestId: 'new-school-school',
updateOnSelect: ['newSchool.municipality'],
updateOnSelect: 'newSchool.municipality',
loadOptions: async ({ application, apolloClient, selectedValue }) => {
const { schoolMunicipality } = getApplicationAnswers(
application.answers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const asyncSelectSubsection = buildSubSection({
id: 'asyncSelectDescription',
title: 'Value dependent async select',
description:
'Sometimes the options you might want to present to a user must depend on the value of another async select field. Value dependent async select offers just that.',
'Sometimes the options you might want to present to a user must depend on the value of another async select field. Value dependent async select offers just that. Multi select fields are also supported and selected values will be passed as an array.',
titleVariant: 'h3',
marginBottom: [2],
}),
Expand Down Expand Up @@ -108,7 +108,7 @@ export const asyncSelectSubsection = buildSubSection({
title: 'Dependent Async Select',
placeholder: 'Will re-fetch when the primary async select is changed',
loadingError: 'Loading error',
updateOnSelect: ['primaryAsyncSelect'],
updateOnSelect: 'primaryAsyncSelect',
loadOptions: async ({ apolloClient, selectedValue }) => {
const { data } =
await apolloClient.query<FriggSchoolsByMunicipality>({
Expand Down
4 changes: 2 additions & 2 deletions libs/application/types/src/lib/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type Context = {
export type AsyncSelectContext = {
application: Application
apolloClient: ApolloClient<object>
selectedValue?: string[]
selectedValue?: string | string[]
}

export type TagVariant =
Expand Down Expand Up @@ -396,7 +396,7 @@ export interface AsyncSelectField extends InputField {
backgroundColor?: InputBackgroundColor
isSearchable?: boolean
isMulti?: boolean
updateOnSelect?: string[]
updateOnSelect?: string
}

export interface TextField extends InputField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export const AsyncSelectFormField: FC<React.PropsWithChildren<Props>> = ({
const [lastUpdateOnSelectValue, setLastUpdateOnSelectValue] =
useState<string>('')

const watchUpdateOnSelect = updateOnSelect ? watch(updateOnSelect) : []
const load = async (selectedValue?: string[]) => {
const watchUpdateOnSelect = updateOnSelect ? watch(updateOnSelect) : ''
const load = async (selectedValue?: string | string[]) => {
try {
setHasLoadingError(false)
const loaded = await loadOptions({
Expand All @@ -68,20 +68,16 @@ export const AsyncSelectFormField: FC<React.PropsWithChildren<Props>> = ({
}

useEffect(() => {
if (
watchUpdateOnSelect.length > 0 &&
watchUpdateOnSelect[0] !== undefined
) {
setLastUpdateOnSelectValue(watchUpdateOnSelect[0])
if (watchUpdateOnSelect) {
setLastUpdateOnSelectValue(watchUpdateOnSelect)
}
}, [watchUpdateOnSelect])

useEffect(() => {
if (updateOnSelect) {
const [selectedValue] = watchUpdateOnSelect
if (selectedValue !== undefined) {
load(selectedValue)
setLastUpdateOnSelectValue(selectedValue)
if (watchUpdateOnSelect !== undefined) {
load(watchUpdateOnSelect)
setLastUpdateOnSelectValue(watchUpdateOnSelect)
} else {
load()
}
Expand Down
Loading