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

Handle MultiSectionFormValidationContextProvider onSubmit action defined as Promise #43

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -37,8 +37,12 @@ export const MultiSectionFormValidationContextProvider = ({
const onFinishSubmitting = useCallback(() => {
setIsTouched(false);
setIsValidated(false);
setIsSubmitting(false);
onSubmit();
// Execute the onSubmit callback. At last, reset the submitting status.
// If the onSubmit was defined as a lazy Promise, we must chain the reset action to the Promise.
// This will ensure that downstream actions (such as re-enabling the Submit button) are paused
// until we have a success/failure response from the onSubmit call.
const submitted = onSubmit();
submitted instanceof Promise ? submitted.finally(() => { setIsSubmitting(false); }) : setIsSubmitting(false);
krithika369 marked this conversation as resolved.
Show resolved Hide resolved
}, [onSubmit]);

useEffect(() => {
Expand All @@ -48,16 +52,16 @@ export const MultiSectionFormValidationContextProvider = ({
zip(schemas, contexts).map(([schema, ctx]) => {
return !!schema
? new Promise((resolve, reject) => {
schema
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Auto formatting fix.

.validate(formData, {
abortEarly: false,
context: ctx
})
.then(
() => resolve({}),
err => resolve(extractErrors(err))
);
})
schema
.validate(formData, {
abortEarly: false,
context: ctx
})
.then(
() => resolve({}),
err => resolve(extractErrors(err))
);
})
: Promise.resolve({});
})
)
Expand Down
22 changes: 22 additions & 0 deletions ui/packages/lib/src/hooks/usePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useState } from "react";

// usePromise returns a lazy promise (a callback that returns a Promise),
// along with a callback function that will either resolve or reject the promise,
// based on the execution of the promise.
export const usePromise = (callback) => {
// Init state with empty function call
const [resolveOrReject, setResolveOrReject] = useState(() => () => { });

const promise = () =>
new Promise((resolve, reject) => {
try {
callback();
setResolveOrReject(() => resolve);
} catch (error) {
console.error(error);
setResolveOrReject(() => reject);
}
});

return [promise, resolveOrReject];
};