-
Notifications
You must be signed in to change notification settings - Fork 166
Validate document capture step completion after page reload #3984
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,81 @@ | ||
| import React, { useState } from 'react'; | ||
| import React, { useEffect, useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import Button from './button'; | ||
| import useI18n from '../hooks/use-i18n'; | ||
| import useHistoryParam from '../hooks/use-history-param'; | ||
|
|
||
| /** | ||
| * @typedef FormStep | ||
| * | ||
| * @prop {string} name Step name, used in history parameter. | ||
| * @prop {import('react').Component} component Step component implementation. | ||
| * @prop {(values:object)=>boolean} isValid Step validity function. Given set of form values, | ||
| * returns true if values satisfy requirements. | ||
| */ | ||
|
|
||
| /** | ||
| * Given a step object and current set of form values, returns true if the form values would satisfy | ||
| * the validity requirements of the step. | ||
| * | ||
| * @param {FormStep} step Form step. | ||
| * @param {object} values Current form values. | ||
| */ | ||
| export function isStepValid(step, values) { | ||
| const { isValid = () => true } = step; | ||
| return isValid(values); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the index of the step in the array which matches the given name. Returns `-1` if there is | ||
| * no step found by that name. | ||
| * | ||
| * @param {FormStep[]} steps Form steps. | ||
| * @param {string} name Step to search. | ||
| * | ||
| * @return {number} Step index. | ||
| */ | ||
| export function getStepIndexByName(steps, name) { | ||
| return steps.findIndex((step) => step.name === name); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurs to me these methods may not be polyfilled correctly in Internet Explorer. I created a ticket LG-3241. |
||
| } | ||
|
|
||
| /** | ||
| * Returns the index of the last step in the array where the values satisfy the requirements of the | ||
| * step. If all steps are valid, returns the index of the last member. Returns `-1` if all steps are | ||
| * invalid, or if the array is empty. | ||
| * | ||
| * @param {FormStep[]} steps Form steps. | ||
| * @param {object} values Current form values. | ||
| * | ||
| * @return {number} Step index. | ||
| */ | ||
| export function getLastValidStepIndex(steps, values) { | ||
| const index = steps.findIndex((step) => !isStepValid(step, values)); | ||
| return index === -1 ? steps.length - 1 : index - 1; | ||
| } | ||
|
|
||
| function FormSteps({ steps, onComplete }) { | ||
| const [values, setValues] = useState({}); | ||
| const [stepName, setStepName] = useHistoryParam('step'); | ||
| const t = useI18n(); | ||
|
|
||
| const stepIndex = stepName ? steps.findIndex((_step) => _step.name === stepName) : 0; | ||
| const step = steps[stepIndex]; | ||
| // An "effective" step is computed in consideration of the facts that (1) there may be no history | ||
| // parameter present, in which case the first step should be used, and (2) the values may not be | ||
| // valid for previous steps, in which case the furthest valid step should be set. | ||
| const effectiveStepIndex = Math.max( | ||
| Math.min(getStepIndexByName(steps, stepName), getLastValidStepIndex(steps, values) + 1), | ||
| 0, | ||
| ); | ||
| const effectiveStep = steps[effectiveStepIndex]; | ||
| useEffect(() => { | ||
| // The effective step is used in the initial render, but since it may be out of sync with the | ||
| // history parameter, it is synced after mount. | ||
| if (effectiveStep && stepName && effectiveStep.name !== stepName) { | ||
| setStepName(effectiveStep.name); | ||
| } | ||
| }, []); | ||
|
|
||
| // An empty steps array is allowed, in which case there is nothing to render. | ||
| if (!step) { | ||
| if (!effectiveStep) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -22,7 +84,7 @@ function FormSteps({ steps, onComplete }) { | |
| * step. | ||
| */ | ||
| function toNextStep() { | ||
| const nextStepIndex = stepIndex + 1; | ||
| const nextStepIndex = effectiveStepIndex + 1; | ||
| const isComplete = nextStepIndex === steps.length; | ||
| if (isComplete) { | ||
| // Clear step parameter from URL. | ||
|
|
@@ -34,10 +96,8 @@ function FormSteps({ steps, onComplete }) { | |
| } | ||
| } | ||
|
|
||
| const { component: Component, name } = step; | ||
| /** @type {{isValid:(values:object)=>boolean}} */ | ||
| const { isValid = () => true } = Component; | ||
| const isLastStep = stepIndex + 1 === steps.length; | ||
| const { component: Component, name } = effectiveStep; | ||
| const isLastStep = effectiveStepIndex + 1 === steps.length; | ||
|
|
||
| return ( | ||
| <> | ||
|
|
@@ -46,7 +106,7 @@ function FormSteps({ steps, onComplete }) { | |
| value={values} | ||
| onChange={(nextValuesPatch) => setValues({ ...values, ...nextValuesPatch })} | ||
| /> | ||
| <Button isPrimary onClick={toNextStep} isDisabled={!isValid(values)}> | ||
| <Button isPrimary onClick={toNextStep} isDisabled={!isStepValid(effectiveStep, values)}> | ||
| {t(isLastStep ? 'forms.buttons.submit.default' : 'forms.buttons.continue')} | ||
| </Button> | ||
| </> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's technically valid to assign a property into an
instanceof Function, but the TypeScript JSDoc flavor doesn't handle this case very well to "re-open" the React component definition for extension. So I opted instead to make this a proper exported member of the module, which retains the benefit of colocality, while integrating a little better with type-checking.