Skip to content

Commit

Permalink
fix: submit only dirty form fields when evaluating visibility rules (#…
Browse files Browse the repository at this point in the history
…139)

* fix: submit only dirty form fields when evaluating visibility rules
We should not submit hidden fields or not yet touched fields when evaluating visiblity conditions,
because default values for those fields will lead wrongly evaluated conditions, espicially for is_none_of operator

* version bump
  • Loading branch information
danijel authored Mar 28, 2024
1 parent 9b40596 commit 4e9ab26
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@awell-health/ui-library",
"version": "0.1.52",
"version": "0.1.53",
"private": false,
"description": "UI components to integrate with Awell Health",
"repository": {
Expand Down Expand Up @@ -193,4 +193,4 @@
]
},
"homepage": "https://github.com/awell-health/ui-library#readme"
}
}
15 changes: 15 additions & 0 deletions src/hooks/useForm/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,18 @@ export const getErrorsForQuestion = (

return []
}

export const getDirtyFieldValues = (formMethods: UseFormReturn) => {
const {
formState: { dirtyFields },
getValues,
} = formMethods
const allValues = getValues()
const dirtyValues = Object.keys(dirtyFields).reduce((acc, key) => {
if (dirtyFields[key]) {
acc[key] = allValues[key]
}
return acc
}, {} as Record<string, AnswerValue>)
return dirtyValues
}
5 changes: 4 additions & 1 deletion src/hooks/useForm/useConversationalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
calculatePercentageCompleted,
convertToAwellInput,
convertToFormFormat,
getDirtyFieldValues,
getErrorsForQuestion,
getInitialValues,
isEmpty,
Expand Down Expand Up @@ -53,7 +54,9 @@ const useConversationalForm = ({

const updateQuestionVisibility = useCallback(async () => {
setIsEvaluatingQuestionVisibility(true)
const formValuesInput = convertToAwellInput(formMethods.getValues())
const formValuesInput = convertToAwellInput(
getDirtyFieldValues(formMethods)
)
const evaluationResults = await evaluateDisplayConditions(formValuesInput)
const updatedQuestions = updateVisibility(
questions,
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useForm/useTraditionalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useValidate } from '../useValidate'
import {
convertToAwellInput,
convertToFormFormat,
getDirtyFieldValues,
getErrorsForQuestion,
getInitialValues,
isEmpty,
Expand Down Expand Up @@ -49,7 +50,9 @@ const useTraditionalForm = ({
useValidate()

const updateQuestionVisibility = useCallback(async () => {
const formValuesInput = convertToAwellInput(formMethods.getValues())
const formValuesInput = convertToAwellInput(
getDirtyFieldValues(formMethods)
)
const evaluationResults = await evaluateDisplayConditions(formValuesInput)
const updatedQuestions = updateVisibility(
questions,
Expand Down

0 comments on commit 4e9ab26

Please sign in to comment.