2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { assign, DoneInvokeEvent } from 'xstate';
|
2023-09-29 07:44:22 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { customizeStoreStateMachineEvents } from '..';
|
2023-10-05 13:33:50 +00:00
|
|
|
import {
|
2023-12-06 12:49:28 +00:00
|
|
|
aiStatusResponse,
|
2023-10-05 13:33:50 +00:00
|
|
|
customizeStoreStateMachineContext,
|
2024-01-09 14:49:59 +00:00
|
|
|
FlowType,
|
2023-10-05 13:33:50 +00:00
|
|
|
RecommendThemesAPIResponse,
|
|
|
|
} from '../types';
|
2023-09-29 07:44:22 +00:00
|
|
|
import { events } from './';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
2023-10-05 13:33:50 +00:00
|
|
|
export const assignThemeData = assign<
|
2023-08-18 05:30:25 +00:00
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
2023-10-05 13:33:50 +00:00
|
|
|
const apiResponse = (
|
|
|
|
event as DoneInvokeEvent< {
|
|
|
|
themeData: RecommendThemesAPIResponse;
|
|
|
|
} >
|
|
|
|
).data.themeData;
|
|
|
|
|
2023-09-29 23:03:50 +00:00
|
|
|
// type coercion workaround for now
|
2023-10-05 13:33:50 +00:00
|
|
|
return {
|
|
|
|
...context.intro,
|
|
|
|
themeData: apiResponse,
|
|
|
|
};
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
} );
|
2023-09-29 07:44:22 +00:00
|
|
|
|
|
|
|
export const recordTracksDesignWithAIClicked = () => {
|
|
|
|
recordEvent( 'customize_your_store_intro_design_with_ai_click' );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const recordTracksThemeSelected = (
|
|
|
|
_context: customizeStoreStateMachineContext,
|
|
|
|
event: Extract<
|
|
|
|
events,
|
|
|
|
{ type: 'SELECTED_ACTIVE_THEME' | 'SELECTED_NEW_THEME' }
|
|
|
|
>
|
|
|
|
) => {
|
2023-11-09 01:32:47 +00:00
|
|
|
recordEvent( 'customize_your_store_intro_theme_select', {
|
2023-09-29 07:44:22 +00:00
|
|
|
theme: event.payload.theme,
|
|
|
|
is_active: event.type === 'SELECTED_ACTIVE_THEME' ? 'yes' : 'no',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const recordTracksBrowseAllThemesClicked = () => {
|
|
|
|
recordEvent( 'customize_your_store_intro_browse_all_themes_click' );
|
|
|
|
};
|
2023-09-29 23:03:50 +00:00
|
|
|
|
|
|
|
export const assignActiveThemeHasMods = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
|
|
|
const activeThemeHasMods = (
|
|
|
|
event as DoneInvokeEvent< { activeThemeHasMods: boolean } >
|
|
|
|
).data.activeThemeHasMods;
|
|
|
|
// type coercion workaround for now
|
|
|
|
return { ...context.intro, activeThemeHasMods };
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export const assignCustomizeStoreCompleted = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
2023-10-09 05:19:08 +00:00
|
|
|
customizeStoreStateMachineEvents
|
2023-09-29 23:03:50 +00:00
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
2023-10-09 05:19:08 +00:00
|
|
|
const customizeStoreTaskCompleted = (
|
2023-09-29 23:03:50 +00:00
|
|
|
event as DoneInvokeEvent< {
|
2023-10-09 05:19:08 +00:00
|
|
|
customizeStoreTaskCompleted: boolean;
|
2023-09-29 23:03:50 +00:00
|
|
|
} >
|
2023-10-09 05:19:08 +00:00
|
|
|
).data.customizeStoreTaskCompleted;
|
|
|
|
return { ...context.intro, customizeStoreTaskCompleted };
|
2023-09-29 23:03:50 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export const assignFetchIntroDataError = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
|
|
|
intro: ( context ) => {
|
|
|
|
return { ...context.intro, hasErrors: true };
|
|
|
|
},
|
|
|
|
} );
|
2023-10-09 05:19:08 +00:00
|
|
|
|
|
|
|
export const assignCurrentThemeIsAiGenerated = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
|
|
|
const currentThemeIsAiGenerated = (
|
|
|
|
event as DoneInvokeEvent< {
|
|
|
|
currentThemeIsAiGenerated: boolean;
|
|
|
|
} >
|
|
|
|
).data.currentThemeIsAiGenerated;
|
|
|
|
return { ...context.intro, currentThemeIsAiGenerated };
|
|
|
|
},
|
|
|
|
} );
|
2023-12-06 12:49:28 +00:00
|
|
|
|
|
|
|
export const assignAiStatus = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
2024-01-09 14:49:59 +00:00
|
|
|
flowType: ( _context, _event ) => {
|
2023-12-06 12:49:28 +00:00
|
|
|
const indicator = ( _event as DoneInvokeEvent< aiStatusResponse > ).data
|
|
|
|
.status.indicator;
|
|
|
|
const status = indicator !== 'critical' && indicator !== 'major';
|
|
|
|
// @ts-expect-error temp workaround;
|
|
|
|
window.cys_aiOnline = status;
|
|
|
|
|
|
|
|
recordEvent( 'customize_your_store_ai_status', {
|
|
|
|
online: status ? 'yes' : 'no',
|
|
|
|
} );
|
|
|
|
|
2024-01-09 14:49:59 +00:00
|
|
|
return status ? FlowType.AIOnline : FlowType.AIOffline;
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export const assignAiOffline = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
2024-01-09 14:49:59 +00:00
|
|
|
flowType: () => {
|
2023-12-06 12:49:28 +00:00
|
|
|
// @ts-expect-error temp workaround;
|
|
|
|
window.cys_aiOnline = false;
|
|
|
|
recordEvent( 'customize_your_store_ai_status', {
|
|
|
|
online: 'no',
|
|
|
|
} );
|
|
|
|
|
2024-01-09 14:49:59 +00:00
|
|
|
return FlowType.AIOffline;
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
|
|
|
} );
|
2024-01-09 14:49:59 +00:00
|
|
|
|
|
|
|
export const assignNoAI = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents // this is actually the wrong type for the event but I still don't know how to type this properly
|
|
|
|
>( {
|
|
|
|
flowType: FlowType.noAI,
|
|
|
|
} );
|
2024-01-25 11:04:44 +00:00
|
|
|
|
|
|
|
export const assignNoAIFlowError = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
intro: ( context ) => {
|
|
|
|
return { ...context.intro, hasErrors: true };
|
|
|
|
},
|
|
|
|
} );
|