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';
|
2024-02-06 10:28:26 +00:00
|
|
|
import { isFontLibraryAvailable } from './guards';
|
2024-01-11 14:32:16 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2024-02-06 10:28:26 +00:00
|
|
|
isFontLibraryAvailable: false,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2024-02-08 15:08:38 +00:00
|
|
|
setGlobalStyles: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'updateGlobalStylesWithDefaultValues',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-01 17:16:12 +00:00
|
|
|
installFontFamilies: {
|
2024-02-02 15:34:33 +00:00
|
|
|
initial: 'checkFontLibrary',
|
2024-02-01 17:16:12 +00:00
|
|
|
states: {
|
2024-02-02 15:34:33 +00:00
|
|
|
checkFontLibrary: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
cond: {
|
2024-02-06 10:28:26 +00:00
|
|
|
type: 'isFontLibraryAvailable',
|
2024-02-02 15:34:33 +00:00
|
|
|
},
|
|
|
|
target: 'pending',
|
|
|
|
},
|
|
|
|
{ target: 'success' },
|
|
|
|
],
|
|
|
|
},
|
2024-02-01 17:16:12 +00:00
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'installFontFamilies',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-02-13 13:15:52 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-02-01 17:16:12 +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,
|
2024-02-06 10:28:26 +00:00
|
|
|
isFontLibraryAvailable,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|