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

Return to old flow when prioritization is disabled #318

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 28 additions & 4 deletions src/OnboardingSPA/components/StateHandlers/Flow/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from '@wordpress/element';
import { useEffect, useState } from '@wordpress/element';
import { useLocation } from 'react-router-dom';
import { useSelect, useDispatch } from '@wordpress/data';
import { getFragment } from '@wordpress/url';

import { store as nfdOnboardingStore } from '../../../store';
import { switchFlow } from '../../../utils/api/flow';
Expand All @@ -21,11 +22,15 @@
ACTION_ONBOARDING_STARTED,
} from '../../../utils/analytics/hiive/constants';
import { ECOMMERCE_FLOW } from '../../../data/flows/constants';
import { getQueryParam } from '../../../utils';
import { getQueryParam, removeQueryParam } from '../../../utils';
import { commerce } from '../../../chapters/commerce';
import EcommerceStepLoader from '../../Loaders/Step/Ecommerce';

const FlowStateHandler = ( { children } ) => {
const location = useLocation();

const [ newFlow, setNewFlow ] = useState( false );

const {
brandConfig,
experienceLevel,
Expand Down Expand Up @@ -59,13 +64,20 @@

const handleCommerceFlow = async ( flow, retries = 0 ) => {
if ( retries >= MAX_RETRIES_FLOW_SWITCH ) {
return;
return setNewFlow( false );
}
const response = switchFlow( flow );
if ( response?.error ) {
retries = retries + 1;
return handleCommerceFlow( flow, retries );
}

// TODO: Remove code below once Chapter Prioritization is enabled.
const firstEcommerceStep = commerce.steps[ 0 ];
const fragment = getFragment( window.location.href );
const redirect = removeQueryParam( window.location.href, 'flow' ).replace( fragment, '' );
window.location.replace( `${ redirect }#${ firstEcommerceStep.path }` );
window.location.reload();
};

const switchToNewFlow = async ( flow ) => {
Expand All @@ -78,6 +90,8 @@
case ECOMMERCE_FLOW:
handleCommerceFlow( flow );
break;
default:
setNewFlow( false );
}
};

Expand Down Expand Up @@ -156,11 +170,11 @@
if ( false !== brandConfig?.prioritization ) {
return prioritizeFlow();
}
}, [ experienceLevel, topPriority ] );

Check warning on line 173 in src/OnboardingSPA/components/StateHandlers/Flow/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'brandConfig?.prioritization' and 'prioritizeFlow'. Either include them or remove the dependency array

useEffect( () => {
trackChapters();
}, [ currentStep ] );

Check warning on line 177 in src/OnboardingSPA/components/StateHandlers/Flow/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'trackChapters'. Either include it or remove the dependency array

useEffect( () => {
if ( window.nfdOnboarding?.newFlow ) {
Expand All @@ -168,9 +182,19 @@
switchToNewFlow( flow );
window.nfdOnboarding.newFlow = undefined;
}
}, [ location.pathname ] );

Check warning on line 185 in src/OnboardingSPA/components/StateHandlers/Flow/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'switchToNewFlow'. Either include it or remove the dependency array

return <>{ children }</>;
// TODO: Remove handleRender and replace with only children once Chapter Prioritization is enabled.
const handleRender = () => {
switch ( newFlow ) {
case 'ecommerce':
return <EcommerceStepLoader />;
default:
return children;
}
};

return <>{ handleRender() }</>;
};

export default FlowStateHandler;
21 changes: 16 additions & 5 deletions src/OnboardingSPA/data/flows/ecommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ export const getSteps = ( chapters = initialChapters ) => {
chapters.forEach( ( chapter ) => {
steps = steps.concat( [
...chapter.steps,
// ...chapter.interstitialSteps,
// ...chapter.interstitialSteps, // TODO: Add interstitialSteps once Chapter Prioritization is enabled.
] );
} );
steps = steps.concat( [ stepComplete, stepWhatNext ] );
return steps;
// TODO: Filter to be removed once Chapter Prioritization is enabled.
return filter( steps, ( step ) => {
return (
! step.path.includes( '/wp-setup/step/top-priority' )
);
} );
};

export const getRoutes = ( chapters = initialChapters ) => {
Expand All @@ -49,11 +54,16 @@ export const getRoutes = ( chapters = initialChapters ) => {
routes = routes.concat( [
...chapter.steps,
...chapter.conditionalSteps,
// ...chapter.interstitialSteps,
// ...chapter.interstitialSteps, // TODO: Add interstitialSteps once Chapter Prioritization is enabled.
] );
} );
routes = routes.concat( [ stepComplete, stepWhatNext ] );
return routes;
// TODO: Filter to be removed once Chapter Prioritization is enabled.
return filter( routes, ( route ) => {
return (
! route.path.includes( '/wp-setup/step/top-priority' )
);
} );
};

const getPseudoStepForEcommerce = ( firstEcommerceStep ) => {
Expand All @@ -79,7 +89,8 @@ export const getTopSteps = ( steps ) => {
return filter( steps, ( step ) => {
return (
step instanceof PseudoStep ||
! step.path.includes( '/ecommerce/step' )
( ! step.path.includes( '/ecommerce/step' ) &&
! step.path.includes( '/wp-setup/step/top-priority' ) ) // TODO: Filter to be removed once Chapter Prioritization is enabled.
);
} );
};
Expand Down