-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Stepper: Avoid calling the submit function of the process step multiple times #98197
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -13,7 +13,7 @@ import { | |||||
} from '@automattic/onboarding'; | ||||||
import { useSelect } from '@wordpress/data'; | ||||||
import { useI18n } from '@wordpress/react-i18n'; | ||||||
import { useEffect, useState } from 'react'; | ||||||
import { useEffect, useState, useRef } from 'react'; | ||||||
import DocumentHead from 'calypso/components/data/document-head'; | ||||||
import { LoadingBar } from 'calypso/components/loading-bar'; | ||||||
import { LoadingEllipsis } from 'calypso/components/loading-ellipsis'; | ||||||
|
@@ -49,6 +49,28 @@ const ProcessingStep: React.FC< ProcessingStepProps > = function ( props ) { | |||||
const [ hasEmptyActionRun, setHasEmptyActionRun ] = useState( false ); | ||||||
const [ destinationState, setDestinationState ] = useState( {} ); | ||||||
|
||||||
/** | ||||||
* There is a long-term bug here that the `submit` function will be called multiple times if we | ||||||
* call `resetOnboardStoreWithSkipFlags` after the submit function (e.g.: exitFlow) to reset states | ||||||
* that are listed as the dependencies of the `recordSignupComplete`, e.g.: goals, selectedDesign, etc. | ||||||
* | ||||||
* Here is a possible flow: | ||||||
* 1. The Design Picker step submits, sets a pending action, and goes to this step | ||||||
* 2. Run the pending action, and then submit first | ||||||
* 3. The `submit` may trigger the `exitFlow` function that is defined by each flow | ||||||
* 4. The `exitFlow` function may set another pending action, and call `resetOnboardStoreWithSkipFlags` function | ||||||
* 5. The effect to call the `submit` runs again since the `recordSignupComplete` function changes | ||||||
* | ||||||
* It's also a reason why we have a hacky to set a pending action to return a Promise that is never resolved. | ||||||
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.
Suggested change
|
||||||
* | ||||||
* To resolve this issue, we define a flag to avoid calling the submit function multiple times. | ||||||
* | ||||||
* Another way is to remove the recordSignupComplete function from the dependencies of the effect that | ||||||
* is called when the hasActionSuccessfullyRun flag turns on. But it seems to be better to use an explicit | ||||||
* flag to avoid the issue and describe it here. | ||||||
*/ | ||||||
const isSubmittedRef = useRef( false ); | ||||||
|
||||||
const recordSignupComplete = useRecordSignupComplete( flow ); | ||||||
|
||||||
useInterval( | ||||||
|
@@ -104,15 +126,18 @@ const ProcessingStep: React.FC< ProcessingStepProps > = function ( props ) { | |||||
|
||||||
// As for hasActionSuccessfullyRun, in this case we submit the no action result. | ||||||
useEffect( () => { | ||||||
if ( hasEmptyActionRun ) { | ||||||
if ( hasEmptyActionRun && ! isSubmittedRef.current ) { | ||||||
// Let's ensure the submit function is called only once, | ||||||
// but only for the onboarding flow to mitigate risks. | ||||||
isSubmittedRef.current = flow === 'site-setup' ? true : false; | ||||||
submit?.( {}, ProcessingResult.NO_ACTION ); | ||||||
} | ||||||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
}, [ hasEmptyActionRun ] ); | ||||||
|
||||||
// When the hasActionSuccessfullyRun flag turns on, run submit() and fire the sign-up completion event. | ||||||
useEffect( () => { | ||||||
if ( hasActionSuccessfullyRun ) { | ||||||
if ( hasActionSuccessfullyRun && ! isSubmittedRef.current ) { | ||||||
// We should only trigger signup completion for signup flows, so check if we have one. | ||||||
if ( availableFlows[ flow ] ) { | ||||||
availableFlows[ flow ]().then( ( flowExport ) => { | ||||||
|
@@ -134,6 +159,10 @@ const ProcessingStep: React.FC< ProcessingStepProps > = function ( props ) { | |||||
wccom_from: getWccomFrom( destinationState ), | ||||||
} ); | ||||||
|
||||||
// Let's ensure the submit function is called only once, | ||||||
// but only for the onboarding flow to mitigate risks. | ||||||
isSubmittedRef.current = flow === 'site-setup' ? true : false; | ||||||
|
||||||
// Default processing handler. | ||||||
submit?.( destinationState, ProcessingResult.SUCCESS ); | ||||||
} | ||||||
|
Oops, something went wrong.
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.
Note to myself: like here