-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3198 from Automattic/add/saved-progress
Integrate store state with stepper navigation
- Loading branch information
Showing
16 changed files
with
146 additions
and
122 deletions.
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,73 +1,30 @@ | ||
import { useState, useEffect, useMemo } from '@wordpress/element'; | ||
import { Stepper } from '@woocommerce/components'; | ||
import { get, uniq } from 'lodash'; | ||
|
||
import { useQueryStringRouter } from '../query-string-router'; | ||
|
||
/** | ||
* @typedef {Object} Step | ||
* @property {string} key Step key. | ||
*/ | ||
/** | ||
* @typedef {Object} StepWithNavigationState | ||
* @property {boolean} [isComplete] Flag if it is complete. | ||
* @property {Function} [onClick] Function to navigate to the step (Enables the click on the Stepper). | ||
*/ | ||
/** | ||
* Merge the navigation state into the steps. | ||
* Add isComplete and onClick - when visited. | ||
* Go to route when clicking steps that can be active (completed or next). | ||
* | ||
* @param {Step[]} steps Steps list. | ||
* @param {string[]} visitedSteps Key of the visited steps. | ||
* @param {Function} goTo Function that update the step. | ||
* @param {Array} steps | ||
* @param {Object} deps | ||
* @param {Function} deps.goTo | ||
* | ||
* @return {StepWithNavigationState} Steps with navigation state merged. | ||
* @return {Array} Steps with click handlers. | ||
*/ | ||
const getStepsWithNavigationState = ( steps, visitedSteps, goTo ) => | ||
steps.map( ( step, index ) => { | ||
const nextKey = get( steps, [ index + 1, 'key' ], null ); | ||
|
||
const stepWithNavigationState = { | ||
...step, | ||
isComplete: nextKey && visitedSteps.includes( nextKey ), | ||
}; | ||
|
||
if ( visitedSteps.includes( step.key ) ) { | ||
stepWithNavigationState.onClick = () => { | ||
goTo( step.key ); | ||
}; | ||
} | ||
|
||
return stepWithNavigationState; | ||
} ); | ||
const addClickHandlers = ( steps, { goTo } ) => | ||
steps.map( ( step ) => ( { | ||
...step, | ||
onClick: | ||
step.isComplete || step.isNext ? () => goTo( step.key ) : undefined, | ||
} ) ); | ||
|
||
/** | ||
* Navigation component. | ||
*/ | ||
const Navigation = ( { steps } ) => { | ||
const { currentRoute, goTo } = useQueryStringRouter(); | ||
steps = addClickHandlers( steps, { goTo } ); | ||
|
||
// Visited steps. | ||
const [ visitedSteps, setVisitedSteps ] = useState( [] ); | ||
|
||
useEffect( () => { | ||
setVisitedSteps( ( prevState ) => | ||
uniq( [ ...prevState, currentRoute ] ) | ||
); | ||
}, [ currentRoute ] ); | ||
|
||
// Update steps with navigation state. | ||
const stepsWithNavigationState = useMemo( | ||
() => getStepsWithNavigationState( steps, visitedSteps, goTo ), | ||
[ steps, visitedSteps, goTo ] | ||
); | ||
|
||
return ( | ||
<Stepper | ||
steps={ stepsWithNavigationState } | ||
currentStep={ currentRoute || steps[ 0 ].key } | ||
/> | ||
); | ||
return <Stepper steps={ steps } currentStep={ currentRoute } />; | ||
}; | ||
|
||
export default Navigation; |
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
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
Oops, something went wrong.