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 form reinitialization with formik #26292

Merged
merged 8 commits into from
Nov 10, 2022
Merged

Fix form reinitialization with formik #26292

merged 8 commits into from
Nov 10, 2022

Conversation

ranquild
Copy link
Contributor

@ranquild ranquild commented Nov 8, 2022

Epic #26178

Currently the submit button doesn't show Success message when enableReinitialize is passed to Formik. This happens because Formik resets the internal status field used for submit status tracking. This PR fixes the issue by moving the submit state to its own context.

How to test:

  • Account -> Profile
  • Change any field and click Update
  • Make sure the button displays Success and becomes disabled after submit

Screenshot 2022-11-08 at 15 47 50

@ranquild ranquild self-assigned this Nov 8, 2022
@ranquild ranquild requested a review from a team November 8, 2022 13:50
@codecov
Copy link

codecov bot commented Nov 8, 2022

Codecov Report

Base: 64.28% // Head: 64.30% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (8d6fe44) compared to base (b488f53).
Patch coverage: 83.33% of modified lines in pull request are covered.

❗ Current head 8d6fe44 differs from pull request most recent head 2524f17. Consider uploading reports for the commit 2524f17 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #26292      +/-   ##
==========================================
+ Coverage   64.28%   64.30%   +0.01%     
==========================================
  Files        3148     3148              
  Lines       92202    92204       +2     
  Branches    11699    11699              
==========================================
+ Hits        59270    59289      +19     
+ Misses      28242    28219      -23     
- Partials     4690     4696       +6     
Flag Coverage Δ
front-end 45.16% <83.33%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...e/components/FormSubmitButton/FormSubmitButton.tsx 80.00% <ø> (ø)
.../metabase/core/context/FormContext/FormContext.tsx 50.00% <50.00%> (ø)
...base/core/hooks/use-form-submit/use-form-submit.ts 70.58% <80.00%> (+3.92%) ⬆️
...base/core/components/FormProvider/FormProvider.tsx 100.00% <100.00%> (ø)
...s/use-form-error-message/use-form-error-message.ts 68.75% <100.00%> (+2.08%) ⬆️
...s/use-form-submit-button/use-form-submit-button.ts 95.23% <100.00%> (+0.23%) ⬆️
src/metabase/driver/impl.clj 69.14% <0.00%> (-1.07%) ⬇️
...rc/metabase/setup/components/UserForm/UserForm.tsx 59.09% <0.00%> (ø)
...c/metabase/auth/components/LoginForm/LoginForm.tsx 81.81% <0.00%> (ø)
...c/metabase/core/components/FormInput/FormInput.tsx 100.00% <0.00%> (ø)
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ranquild ranquild added the backport Automatically create PR on current release branch on merge label Nov 8, 2022
Copy link
Member

@kulyk kulyk left a comment

Choose a reason for hiding this comment

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

LGTM! I'd vote for adding some sort of a useForm hook that'd let us access FormContext


const useFormErrorMessage = (): string | undefined => {
const { values, errors } = useFormikContext();
const { status, message } = useFormState();
const { status, message } = useContext(FormContext);
Copy link
Member

Choose a reason for hiding this comment

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

Should we maybe add another hook like useForm that'd basically return useContext(FormContext);? So there's no need to carry over the context here and there all the time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed - added a hook called useFormContext. Decided to use the same naming convention the formik does, i.e. useFormikContext

Comment on lines +23 to +25
setState({ status: "pending" });
await onSubmit(data, helpers);
helpers.setStatus({ status: "fulfilled" });
setState({ status: "fulfilled" });
Copy link
Member

Choose a reason for hiding this comment

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

My first thought was "why is setState called without this?" 😄

giphy

@ranquild ranquild merged commit 5cba39a into master Nov 10, 2022
@ranquild ranquild deleted the 26178-form-context branch November 10, 2022 13:38
github-actions bot pushed a commit that referenced this pull request Nov 10, 2022
metabase-bot bot added a commit that referenced this pull request Nov 10, 2022
Co-authored-by: Alexander Polyankin <alexander.polyankin@metabase.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport Automatically create PR on current release branch on merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants