2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-09-12 06:32:50 +00:00
|
|
|
import { Sender, createMachine } from 'xstate';
|
2023-08-18 05:30:25 +00:00
|
|
|
import { useEffect, useMemo, useState } from '@wordpress/element';
|
|
|
|
import { useMachine, useSelector } from '@xstate/react';
|
2023-09-12 06:32:50 +00:00
|
|
|
import { getQuery, updateQueryString } from '@woocommerce/navigation';
|
2023-09-19 11:02:02 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useFullScreen } from '~/utils';
|
|
|
|
import {
|
|
|
|
Intro,
|
|
|
|
events as introEvents,
|
|
|
|
services as introServices,
|
|
|
|
actions as introActions,
|
|
|
|
} from './intro';
|
|
|
|
import { DesignWithAi, events as designWithAiEvents } from './design-with-ai';
|
2023-08-28 01:28:05 +00:00
|
|
|
import { AssemblerHub, events as assemblerHubEvents } from './assembler-hub';
|
2023-09-15 04:01:02 +00:00
|
|
|
import {
|
|
|
|
Transitional,
|
|
|
|
events as transitionalEvents,
|
|
|
|
services as transitionalServices,
|
|
|
|
} from './transitional';
|
2023-08-18 05:30:25 +00:00
|
|
|
import { findComponentMeta } from '~/utils/xstate/find-component';
|
|
|
|
import {
|
|
|
|
CustomizeStoreComponentMeta,
|
|
|
|
CustomizeStoreComponent,
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
} from './types';
|
|
|
|
import { ThemeCard } from './intro/theme-cards';
|
2023-08-29 06:00:54 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
export type customizeStoreStateMachineEvents =
|
|
|
|
| introEvents
|
|
|
|
| designWithAiEvents
|
2023-09-06 06:21:09 +00:00
|
|
|
| assemblerHubEvents
|
2023-09-15 04:01:02 +00:00
|
|
|
| transitionalEvents
|
2023-09-12 06:32:50 +00:00
|
|
|
| { type: 'AI_WIZARD_CLOSED_BEFORE_COMPLETION'; payload: { step: string } }
|
|
|
|
| { type: 'EXTERNAL_URL_UPDATE' };
|
2023-08-18 05:30:25 +00:00
|
|
|
|
2023-09-12 06:32:50 +00:00
|
|
|
const updateQueryStep = (
|
|
|
|
_context: unknown,
|
|
|
|
_evt: unknown,
|
|
|
|
{ action }: { action: unknown }
|
|
|
|
) => {
|
|
|
|
const { path } = getQuery() as { path: string };
|
|
|
|
const step = ( action as { step: string } ).step;
|
|
|
|
const pathFragments = path.split( '/' ); // [0] '', [1] 'customize-store', [2] step slug [3] design-with-ai, assembler-hub path fragments
|
|
|
|
if ( pathFragments[ 1 ] === 'customize-store' ) {
|
|
|
|
if ( pathFragments[ 2 ] !== step ) {
|
|
|
|
// this state machine is only concerned with [2], so we ignore changes to [3]
|
|
|
|
// [1] is handled by router at root of wc-admin
|
|
|
|
updateQueryString( {}, `/customize-store/${ step }` );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-19 11:02:02 +00:00
|
|
|
const markTaskComplete = async () => {
|
|
|
|
return dispatch( OPTIONS_STORE_NAME ).updateOptions( {
|
|
|
|
woocommerce_admin_customize_store_completed: 'yes',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2023-09-12 06:32:50 +00:00
|
|
|
const browserPopstateHandler =
|
|
|
|
() => ( sendBack: Sender< { type: 'EXTERNAL_URL_UPDATE' } > ) => {
|
|
|
|
const popstateHandler = () => {
|
|
|
|
sendBack( { type: 'EXTERNAL_URL_UPDATE' } );
|
|
|
|
};
|
|
|
|
window.addEventListener( 'popstate', popstateHandler );
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'popstate', popstateHandler );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const machineActions = {
|
|
|
|
updateQueryStep,
|
2023-08-18 05:30:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const customizeStoreStateMachineActions = {
|
|
|
|
...introActions,
|
2023-09-12 06:32:50 +00:00
|
|
|
...machineActions,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const customizeStoreStateMachineServices = {
|
|
|
|
...introServices,
|
2023-09-15 04:01:02 +00:00
|
|
|
...transitionalServices,
|
2023-09-12 06:32:50 +00:00
|
|
|
browserPopstateHandler,
|
2023-09-19 11:02:02 +00:00
|
|
|
markTaskComplete,
|
2023-08-18 05:30:25 +00:00
|
|
|
};
|
|
|
|
export const customizeStoreStateMachineDefinition = createMachine( {
|
|
|
|
id: 'customizeStore',
|
2023-09-12 06:32:50 +00:00
|
|
|
initial: 'navigate',
|
2023-08-18 05:30:25 +00:00
|
|
|
predictableActionArguments: true,
|
|
|
|
preserveActionOrder: true,
|
|
|
|
schema: {
|
|
|
|
context: {} as customizeStoreStateMachineContext,
|
|
|
|
events: {} as customizeStoreStateMachineEvents,
|
|
|
|
services: {} as {
|
|
|
|
fetchThemeCards: { data: ThemeCard[] };
|
|
|
|
},
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
intro: {
|
|
|
|
themeCards: [] as ThemeCard[],
|
|
|
|
activeTheme: '',
|
|
|
|
},
|
|
|
|
} as customizeStoreStateMachineContext,
|
2023-09-12 06:32:50 +00:00
|
|
|
invoke: {
|
|
|
|
src: 'browserPopstateHandler',
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
EXTERNAL_URL_UPDATE: {
|
|
|
|
target: 'navigate',
|
|
|
|
},
|
|
|
|
AI_WIZARD_CLOSED_BEFORE_COMPLETION: {
|
|
|
|
target: 'intro',
|
|
|
|
actions: [ { type: 'updateQueryStep', step: 'intro' } ],
|
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
states: {
|
2023-09-12 06:32:50 +00:00
|
|
|
navigate: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
target: 'intro',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'intro',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
target: 'designWithAi',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'design-with-ai',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
target: 'assemblerHub',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'assembler-hub',
|
|
|
|
},
|
|
|
|
},
|
2023-09-18 11:01:12 +00:00
|
|
|
{
|
|
|
|
target: 'transitionalScreen',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'transitional',
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
{
|
|
|
|
target: 'intro',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
intro: {
|
|
|
|
id: 'intro',
|
|
|
|
initial: 'preIntro',
|
|
|
|
states: {
|
|
|
|
preIntro: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchThemeCards',
|
|
|
|
onDone: {
|
|
|
|
target: 'intro',
|
|
|
|
actions: [ 'assignThemeCards' ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
intro: {
|
|
|
|
meta: {
|
|
|
|
component: Intro,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
DESIGN_WITH_AI: {
|
|
|
|
target: 'designWithAi',
|
|
|
|
},
|
|
|
|
SELECTED_ACTIVE_THEME: {
|
|
|
|
target: 'assemblerHub',
|
|
|
|
},
|
|
|
|
CLICKED_ON_BREADCRUMB: {
|
|
|
|
target: 'backToHomescreen',
|
|
|
|
},
|
|
|
|
SELECTED_NEW_THEME: {
|
2023-08-28 01:28:05 +00:00
|
|
|
target: 'appearanceTask',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
SELECTED_BROWSE_ALL_THEMES: {
|
2023-08-28 01:28:05 +00:00
|
|
|
target: 'appearanceTask',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
designWithAi: {
|
|
|
|
initial: 'preDesignWithAi',
|
|
|
|
states: {
|
|
|
|
preDesignWithAi: {
|
|
|
|
always: {
|
|
|
|
target: 'designWithAi',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
designWithAi: {
|
|
|
|
meta: {
|
|
|
|
component: DesignWithAi,
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
entry: [
|
|
|
|
{ type: 'updateQueryStep', step: 'design-with-ai' },
|
|
|
|
],
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
THEME_SUGGESTED: {
|
|
|
|
target: 'assemblerHub',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assemblerHub: {
|
2023-09-12 06:32:50 +00:00
|
|
|
initial: 'assemblerHub',
|
|
|
|
states: {
|
|
|
|
assemblerHub: {
|
|
|
|
entry: [
|
|
|
|
{ type: 'updateQueryStep', step: 'assembler-hub' },
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
component: AssemblerHub,
|
|
|
|
},
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
postAssemblerHub: {
|
2023-09-19 11:02:02 +00:00
|
|
|
invoke: {
|
|
|
|
src: 'markTaskComplete',
|
|
|
|
onDone: {
|
|
|
|
target: 'waitForSitePreview',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
waitForSitePreview: {
|
2023-09-15 04:01:02 +00:00
|
|
|
after: {
|
|
|
|
// Wait for 5 seconds before redirecting to the transitional page. This is to ensure that the site preview image is refreshed.
|
|
|
|
5000: {
|
|
|
|
target: '#customizeStore.transitionalScreen',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-28 01:28:05 +00:00
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
on: {
|
|
|
|
FINISH_CUSTOMIZATION: {
|
2023-09-15 04:01:02 +00:00
|
|
|
// Pre-fetch the site preview image for the site for transitional page.
|
|
|
|
actions: [ 'prefetchSitePreview' ],
|
|
|
|
target: '.postAssemblerHub',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
2023-08-28 01:28:05 +00:00
|
|
|
GO_BACK_TO_DESIGN_WITH_AI: {
|
|
|
|
target: 'designWithAi',
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
transitionalScreen: {
|
2023-09-18 11:01:12 +00:00
|
|
|
entry: [ { type: 'updateQueryStep', step: 'transitional' } ],
|
2023-09-15 04:01:02 +00:00
|
|
|
meta: {
|
|
|
|
component: Transitional,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
GO_BACK_TO_HOME: {
|
|
|
|
target: 'backToHomescreen',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
backToHomescreen: {},
|
2023-08-28 01:28:05 +00:00
|
|
|
appearanceTask: {},
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export const CustomizeStoreController = ( {
|
|
|
|
actionOverrides,
|
|
|
|
servicesOverrides,
|
|
|
|
}: {
|
|
|
|
actionOverrides: Partial< typeof customizeStoreStateMachineActions >;
|
|
|
|
servicesOverrides: Partial< typeof customizeStoreStateMachineServices >;
|
|
|
|
} ) => {
|
|
|
|
useFullScreen( [ 'woocommerce-customize-store' ] );
|
|
|
|
|
|
|
|
const augmentedStateMachine = useMemo( () => {
|
|
|
|
return customizeStoreStateMachineDefinition.withConfig( {
|
|
|
|
services: {
|
|
|
|
...customizeStoreStateMachineServices,
|
|
|
|
...servicesOverrides,
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
...customizeStoreStateMachineActions,
|
|
|
|
...actionOverrides,
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
guards: {
|
|
|
|
hasStepInUrl: ( _ctx, _evt, { cond }: { cond: unknown } ) => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === // [0] '', [1] 'customize-store', [2] step slug
|
|
|
|
( cond as { step: string | undefined } ).step
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
} );
|
|
|
|
}, [ actionOverrides, servicesOverrides ] );
|
|
|
|
|
|
|
|
const [ state, send, service ] = useMachine( augmentedStateMachine, {
|
|
|
|
devTools: process.env.NODE_ENV === 'development',
|
|
|
|
} );
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- false positive due to function name match, this isn't from react std lib
|
|
|
|
const currentNodeMeta = useSelector( service, ( currentState ) =>
|
|
|
|
findComponentMeta< CustomizeStoreComponentMeta >(
|
|
|
|
currentState?.meta ?? undefined
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const [ CurrentComponent, setCurrentComponent ] =
|
|
|
|
useState< CustomizeStoreComponent | null >( null );
|
|
|
|
useEffect( () => {
|
|
|
|
if ( currentNodeMeta?.component ) {
|
|
|
|
setCurrentComponent( () => currentNodeMeta?.component );
|
|
|
|
}
|
|
|
|
}, [ CurrentComponent, currentNodeMeta?.component ] );
|
|
|
|
|
|
|
|
const currentNodeCssLabel =
|
|
|
|
state.value instanceof Object
|
|
|
|
? Object.keys( state.value )[ 0 ]
|
|
|
|
: state.value;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={ `woocommerce-profile-wizard__container woocommerce-profile-wizard__step-${ currentNodeCssLabel }` }
|
|
|
|
>
|
|
|
|
{ CurrentComponent ? (
|
|
|
|
<CurrentComponent
|
2023-09-06 06:21:09 +00:00
|
|
|
parentMachine={ service }
|
2023-08-18 05:30:25 +00:00
|
|
|
sendEvent={ send }
|
|
|
|
context={ state.context }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomizeStoreController;
|