2024-01-11 14:32:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-01-25 11:04:44 +00:00
|
|
|
import { createMachine } from 'xstate';
|
2024-01-11 14:32:16 +00:00
|
|
|
import { getQuery } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
|
2024-01-19 11:03:19 +00:00
|
|
|
import { ApiCallLoader, AssembleHubLoader } from './pages/ApiCallLoader';
|
2024-01-11 14:32:16 +00:00
|
|
|
|
|
|
|
import { FlowType } from '../types';
|
|
|
|
import { DesignWithoutAIStateMachineContext } from './types';
|
|
|
|
import { services } from './services';
|
|
|
|
import { actions } from './actions';
|
|
|
|
|
|
|
|
export const hasStepInUrl = (
|
|
|
|
_ctx: unknown,
|
|
|
|
_evt: unknown,
|
|
|
|
{ cond }: { cond: unknown }
|
|
|
|
) => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === // [0] '', [1] 'customize-store', [2] design step slug
|
|
|
|
( cond as { step: string | undefined } ).step
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-25 11:04:44 +00:00
|
|
|
export type DesignWithoutAIStateMachineEvents =
|
|
|
|
| { type: 'EXTERNAL_URL_UPDATE' }
|
|
|
|
| { type: 'NO_AI_FLOW_ERROR'; payload: { hasError: boolean } };
|
|
|
|
|
2024-01-11 14:32:16 +00:00
|
|
|
export const designWithNoAiStateMachineDefinition = createMachine(
|
|
|
|
{
|
|
|
|
id: 'designWithoutAI',
|
|
|
|
predictableActionArguments: true,
|
|
|
|
preserveActionOrder: true,
|
|
|
|
schema: {
|
|
|
|
context: {} as DesignWithoutAIStateMachineContext,
|
2024-01-25 11:04:44 +00:00
|
|
|
events: {} as DesignWithoutAIStateMachineEvents,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
invoke: {
|
|
|
|
src: 'browserPopstateHandler',
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
EXTERNAL_URL_UPDATE: {
|
|
|
|
target: 'navigate',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
startLoadingTime: null,
|
|
|
|
flowType: FlowType.noAI,
|
|
|
|
apiCallLoader: {
|
|
|
|
hasErrors: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initial: 'navigate',
|
|
|
|
states: {
|
|
|
|
navigate: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'design',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
target: 'preAssembleSite',
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
preAssembleSite: {
|
2024-01-18 14:52:49 +00:00
|
|
|
initial: 'preApiCallLoader',
|
2024-01-16 13:01:31 +00:00
|
|
|
id: 'preAssembleSite',
|
2024-01-11 14:32:16 +00:00
|
|
|
states: {
|
2024-01-18 14:52:49 +00:00
|
|
|
preApiCallLoader: {
|
|
|
|
meta: {
|
|
|
|
// @todo: Move the current component in a common folder or create a new one dedicated to this flow.
|
|
|
|
component: ApiCallLoader,
|
|
|
|
},
|
|
|
|
type: 'parallel',
|
2024-01-11 14:32:16 +00:00
|
|
|
states: {
|
2024-01-18 14:52:49 +00:00
|
|
|
installAndActivateTheme: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'installAndActivateTheme',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
success: { type: 'final' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assembleSite: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'assembleSite',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
success: {
|
|
|
|
type: 'final',
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
createProducts: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'createProducts',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
onDone: {
|
|
|
|
target: '#designWithoutAI.showAssembleHub',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
showAssembleHub: {
|
2024-01-16 13:01:31 +00:00
|
|
|
id: 'showAssembleHub',
|
2024-01-11 14:32:16 +00:00
|
|
|
meta: {
|
|
|
|
component: AssembleHubLoader,
|
|
|
|
},
|
|
|
|
entry: [ 'redirectToAssemblerHub' ],
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
actions,
|
|
|
|
services,
|
|
|
|
guards: {
|
|
|
|
hasStepInUrl,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|