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

feat(form-v2): add error handling for deleted logic fields #4440

Merged
merged 11 commits into from
Aug 11, 2022

Conversation

justynoh
Copy link
Contributor

@justynoh justynoh commented Aug 1, 2022

Problem

Builder logic breaks when a user deletes a field that was used in logic. This PR fixes that by adding error handling to the various components.

Closes #4341

Solution

LogicFieldBadge.tsx, InactiveLogicBlock.tsx

  • Added error/info state props to LogicFieldBadge to display in case the field data cannot be found.
  • InactiveLogicBlock provides these props to the LogicFieldBadge component.

useAdminFormLogic.ts, LogicContent.tsx

  • Added computation to determine globally if any errors have occured within all the logics. Added an error box to display message in such case.

EditConditionBlock.tsx, ThenShowBlock.tsx

  • Add effects to set form errors and values for deleted fields, as well as display info boxes where necessary.

Breaking Changes

  • No - this PR is backwards compatible

Screenshots

Screen.Recording.2022-08-01.at.12.10.12.PM.mov

@justynoh justynoh requested review from karrui, timotheeg and wanlingt and removed request for timotheeg, karrui and wanlingt August 1, 2022 05:22
@justynoh justynoh marked this pull request as draft August 1, 2022 10:21
@justynoh justynoh marked this pull request as ready for review August 2, 2022 01:59
Comment on lines 12 to 16
interface FieldLogicBadgeProps {
field: FormFieldWithQuestionNo
field: FormFieldWithQuestionNo | null
errorType?: 'error' | 'info'
errorString?: string
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'info' is not really an errorType, maybe 'variant'? optionally you could also type this more strongly:

type FieldLogicBadgeProps = 
  | { type: 'info', field: FormFieldWithQuestionNo }
  | { type: 'error', message: string }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact if you look at it this way, the props required for the two cases are completely different, which is forcing us to use multiple switch statements and ternary operators all over the place. can consider splitting this into separate components. although it will result in a couple of lines of repeated code, it will reduce the case splits required everywhere else.

arguably in future we might need more types of LogicBadge and at some point it might make sense to refactor them into the same component, but for now imo these make more sense separately. up to you though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed, 'error' | 'info' are the default fallbacks when field is null. This type is not ideal but I think any other way introduces a lot of complexity into the code. For example, another possibility is that we can have another component that deals with error cases specifically (so FieldLogicBadge only takes in a field, and ErrorLogicBadge takes in the { variant: 'error' | 'info', message: string }) but then the error-handling behavior is repeated everywhere you want a logic badge anyway, so it boils down to the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll change the names to variant and message though so its less confusing!

* Compute whether any/all fields in the show fields are deleted, then run
* effect to delete fields on render and show appropriate error/infobox.
*/
const showValueWatch = useWatchDependency(watch, 'show')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to watch this? does the component not automatically update when formFields updates?

Copy link
Contributor Author

@justynoh justynoh Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formFields only contains the fields in the form, not the logics. Also, we need to useWatch here since the effect right after sets the show value again, resulting in infinite rerender.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i might be wrong / there might be a better way to achieve this though!

@justynoh justynoh linked an issue Aug 3, 2022 that may be closed by this pull request
@justynoh justynoh changed the title feat(form-v2): added error handling for deleted logic fields feat(form-v2): add error handling for deleted logic fields Aug 4, 2022
@justynoh justynoh requested a review from timotheeg August 10, 2022 02:18
Copy link
Contributor

@timotheeg timotheeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 again

@justynoh justynoh merged commit ca86584 into form-v2/develop Aug 11, 2022
@justynoh justynoh deleted the form-v2/fix/logic-deleted-fields branch August 11, 2022 01:41
@justynoh justynoh mentioned this pull request Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logic page is not accessible when a field with logic has been deleted
3 participants