2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { assign, DoneInvokeEvent } from 'xstate';
|
2024-05-21 13:45:38 +00:00
|
|
|
import { TaskReferralRecord } from '@woocommerce/onboarding';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { customizeStoreStateMachineEvents } from '..';
|
2023-10-05 13:33:50 +00:00
|
|
|
import {
|
|
|
|
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 './';
|
2024-02-05 11:36:33 +00:00
|
|
|
import { isIframe } from '~/customize-store/utils';
|
2024-04-23 17:38:06 +00:00
|
|
|
import { trackEvent } from '../tracking';
|
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
|
|
|
|
2024-03-13 13:55:04 +00:00
|
|
|
export const assignActiveTheme = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
|
|
|
const activeTheme = (
|
|
|
|
event as DoneInvokeEvent< {
|
|
|
|
activeTheme: string;
|
|
|
|
} >
|
|
|
|
).data.activeTheme;
|
|
|
|
return { ...context.intro, activeTheme };
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-05-21 13:45:38 +00:00
|
|
|
export const assignTaskReferral = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
intro: ( context, event ) => {
|
|
|
|
const taskReferral = (
|
|
|
|
event as DoneInvokeEvent< {
|
|
|
|
taskReferral: TaskReferralRecord | null;
|
|
|
|
} >
|
|
|
|
).data.taskReferral;
|
|
|
|
return { ...context.intro, taskReferral };
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2023-09-29 07:44:22 +00:00
|
|
|
export const recordTracksDesignWithAIClicked = () => {
|
2024-04-23 17:38:06 +00:00
|
|
|
trackEvent( 'customize_your_store_intro_design_with_ai_click' );
|
2023-09-29 07:44:22 +00:00
|
|
|
};
|
|
|
|
|
2024-05-08 06:52:33 +00:00
|
|
|
export const recordTracksDesignWithoutAIClicked = () => {
|
|
|
|
trackEvent( 'customize_your_store_intro_design_without_ai_click' );
|
|
|
|
};
|
|
|
|
|
2023-09-29 07:44:22 +00:00
|
|
|
export const recordTracksThemeSelected = (
|
|
|
|
_context: customizeStoreStateMachineContext,
|
|
|
|
event: Extract<
|
|
|
|
events,
|
|
|
|
{ type: 'SELECTED_ACTIVE_THEME' | 'SELECTED_NEW_THEME' }
|
|
|
|
>
|
|
|
|
) => {
|
2024-04-23 17:38:06 +00:00
|
|
|
trackEvent( '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 = () => {
|
2024-04-23 17:38:06 +00:00
|
|
|
trackEvent( 'customize_your_store_intro_browse_all_themes_click' );
|
2023-09-29 07:44:22 +00:00
|
|
|
};
|
2023-09-29 23:03:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-01-25 11:04:44 +00:00
|
|
|
export const assignNoAIFlowError = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
intro: ( context ) => {
|
|
|
|
return { ...context.intro, hasErrors: true };
|
|
|
|
},
|
|
|
|
} );
|
2024-02-01 11:30:50 +00:00
|
|
|
|
|
|
|
export const assignIsFontLibraryAvailable = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
isFontLibraryAvailable: ( context, event: unknown ) => {
|
|
|
|
return (
|
|
|
|
event as {
|
|
|
|
payload: boolean;
|
|
|
|
}
|
|
|
|
).payload;
|
|
|
|
},
|
|
|
|
} );
|
2024-02-05 11:36:33 +00:00
|
|
|
|
2024-06-05 13:09:35 +00:00
|
|
|
export const assignIsPTKPatternsAPIAvailable = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
isPTKPatternsAPIAvailable: ( context, event: unknown ) => {
|
|
|
|
return (
|
|
|
|
event as {
|
|
|
|
payload: boolean;
|
|
|
|
}
|
|
|
|
).payload;
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-02-22 08:30:11 +00:00
|
|
|
export const assignActiveThemeHasMods = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
activeThemeHasMods: ( context, event: unknown ) => {
|
|
|
|
return (
|
|
|
|
event as DoneInvokeEvent< {
|
|
|
|
activeThemeHasMods: boolean;
|
|
|
|
} >
|
|
|
|
).data.activeThemeHasMods;
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-02-05 11:36:33 +00:00
|
|
|
export const assignFlags = assign<
|
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
customizeStoreStateMachineEvents
|
|
|
|
>( {
|
|
|
|
activeThemeHasMods: () => {
|
|
|
|
if ( ! isIframe( window ) ) {
|
|
|
|
return window.__wcCustomizeStore.activeThemeHasMods;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.parent.__wcCustomizeStore.activeThemeHasMods;
|
|
|
|
},
|
2024-02-06 10:28:26 +00:00
|
|
|
isFontLibraryAvailable: () => {
|
2024-02-05 11:36:33 +00:00
|
|
|
if ( ! isIframe( window ) ) {
|
2024-02-06 10:28:26 +00:00
|
|
|
return window.__wcCustomizeStore.isFontLibraryAvailable;
|
2024-02-05 11:36:33 +00:00
|
|
|
}
|
|
|
|
const isFontLibraryAvailable =
|
|
|
|
window.parent.__wcCustomizeStore.isFontLibraryAvailable || false;
|
|
|
|
return isFontLibraryAvailable;
|
|
|
|
},
|
2024-06-05 13:09:35 +00:00
|
|
|
isPTKPatternsAPIAvailable: () => {
|
|
|
|
if ( ! isIframe( window ) ) {
|
|
|
|
return window.__wcCustomizeStore.isPTKPatternsAPIAvailable;
|
|
|
|
}
|
|
|
|
const isPTKPatternsAPIAvailable =
|
|
|
|
window.parent.__wcCustomizeStore.isPTKPatternsAPIAvailable || false;
|
|
|
|
return isPTKPatternsAPIAvailable;
|
|
|
|
},
|
2024-04-23 08:12:35 +00:00
|
|
|
flowType: ( _context, event ) => {
|
|
|
|
const flowTypeData = event as DoneInvokeEvent< FlowType >;
|
|
|
|
return flowTypeData.data;
|
|
|
|
},
|
2024-02-05 11:36:33 +00:00
|
|
|
} );
|