71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
/**
|
|
* External dependencies
|
|
*/
|
|
import { Sender } from 'xstate';
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
/**
|
|
* Internal dependencies
|
|
*/
|
|
import { updateTemplate } from '../data/actions';
|
|
import { HOMEPAGE_TEMPLATES } from '../data/homepageTemplates';
|
|
import { installAndActivateTheme as setTheme } from '../data/service';
|
|
import { THEME_SLUG } from '../data/constants';
|
|
|
|
const assembleSite = async () => {
|
|
await updateTemplate( {
|
|
homepageTemplateId: 'template1' as keyof typeof HOMEPAGE_TEMPLATES,
|
|
} );
|
|
};
|
|
|
|
const browserPopstateHandler =
|
|
() => ( sendBack: Sender< { type: 'EXTERNAL_URL_UPDATE' } > ) => {
|
|
const popstateHandler = () => {
|
|
sendBack( { type: 'EXTERNAL_URL_UPDATE' } );
|
|
};
|
|
window.addEventListener( 'popstate', popstateHandler );
|
|
return () => {
|
|
window.removeEventListener( 'popstate', popstateHandler );
|
|
};
|
|
};
|
|
|
|
const installAndActivateTheme = async () => {
|
|
try {
|
|
await setTheme( THEME_SLUG );
|
|
} catch ( error ) {
|
|
recordEvent(
|
|
'customize_your_store__no_ai_install_and_activate_theme_error',
|
|
{
|
|
theme: THEME_SLUG,
|
|
error: error instanceof Error ? error.message : 'unknown',
|
|
}
|
|
);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const createProducts = async () => {
|
|
try {
|
|
const { success } = await apiFetch< {
|
|
success: boolean;
|
|
} >( {
|
|
path: `/wc-admin/onboarding/products`,
|
|
method: 'POST',
|
|
} );
|
|
|
|
if ( ! success ) {
|
|
throw new Error( 'Product creation failed' );
|
|
}
|
|
} catch ( error ) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const services = {
|
|
assembleSite,
|
|
browserPopstateHandler,
|
|
installAndActivateTheme,
|
|
createProducts,
|
|
};
|