Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: core profiler fetch extensions #38270

Merged
merged 1 commit into from May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 52 additions & 36 deletions plugins/woocommerce-admin/client/core-profiler/index.tsx
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { createMachine, assign, DoneInvokeEvent, actions } from 'xstate';
import { createMachine, assign, DoneInvokeEvent, actions, spawn } from 'xstate';
import { useMachine } from '@xstate/react';
import { useEffect, useMemo } from '@wordpress/element';
import { resolveSelect, dispatch } from '@wordpress/data';
Expand All @@ -11,6 +11,8 @@ import {
OPTIONS_STORE_NAME,
COUNTRIES_STORE_NAME,
Country,
ONBOARDING_STORE_NAME,
Extension,
} from '@woocommerce/data';
import { recordEvent } from '@woocommerce/tracks';
import { getSetting } from '@woocommerce/settings';
Expand All @@ -25,6 +27,7 @@ import { BusinessInfo } from './pages/BusinessInfo';
import { BusinessLocation } from './pages/BusinessLocation';
import { getCountryStateOptions } from './services/country';
import { Loader } from './pages/Loader';
import { Extensions } from './pages/Extensions';

import './style.scss';

Expand Down Expand Up @@ -91,34 +94,6 @@ export type CoreProfilerStateMachineContext = {
};
};

const Extensions = ( {
context,
sendEvent,
}: {
context: CoreProfilerStateMachineContext;
sendEvent: ( payload: ExtensionsEvent ) => void;
} ) => {
return (
// TOOD: we need to fetch the extensions list from the API as part of initializing the profiler
<>
<div>Extensions</div>
<div>{ context.extensionsAvailable }</div>
<button
onClick={ () =>
sendEvent( {
type: 'EXTENSIONS_COMPLETED',
payload: {
extensionsSelected: [ 'woocommerce-payments' ],
},
} )
}
>
Next
</button>
</>
);
};

const getAllowTrackingOption = async () =>
resolveSelect( OPTIONS_STORE_NAME ).getOption(
'woocommerce_allow_tracking'
Expand All @@ -131,6 +106,17 @@ const handleTrackingOption = assign( {
) => event.data !== 'no',
} );

/**
* Prefetch it so that @wp/data caches it and there won't be a loading delay when its used
*/
const preFetchGetCountries = assign( {
spawnGetCountriesRef: () =>
spawn(
resolveSelect( COUNTRIES_STORE_NAME ).getCountries(),
'core-profiler-prefetch-countries'
),
} );

const getCountries = async () =>
resolveSelect( COUNTRIES_STORE_NAME ).getCountries();

Expand Down Expand Up @@ -217,9 +203,38 @@ const assignOptInDataSharing = assign( {
event.payload.optInDataSharing,
} );

/**
* Prefetch it so that @wp/data caches it and there won't be a loading delay when its used
*/
const preFetchGetExtensions = assign( {
extensionsRef: () =>
spawn(
resolveSelect( ONBOARDING_STORE_NAME ).getFreeExtensions(),
'core-profiler-prefetch-extensions'
),
} );

const getExtensions = async () => {
const extensionsBundles = await resolveSelect(
ONBOARDING_STORE_NAME
).getFreeExtensions();
return (
extensionsBundles.find( ( bundle ) => bundle.key === 'obw/grow' )
?.plugins || []
);
};

const handleExtensions = assign( {
extensionsAvailable: ( _context, event: DoneInvokeEvent< Extension[] > ) =>
event.data,
} );

const coreProfilerMachineActions = {
updateTrackingOption,
preFetchGetExtensions,
preFetchGetCountries,
handleTrackingOption,
handleExtensions,
recordTracksIntroCompleted,
recordTracksIntroSkipped,
recordTracksIntroViewed,
Expand All @@ -233,6 +248,7 @@ const coreProfilerMachineActions = {
const coreProfilerMachineServices = {
getAllowTrackingOption,
getCountries,
getExtensions,
};
export const coreProfilerStateMachineDefinition = createMachine( {
id: 'coreProfiler',
Expand All @@ -257,6 +273,7 @@ export const coreProfilerStateMachineDefinition = createMachine( {
target: 'introOptIn',
},
},
entry: [ 'preFetchGetExtensions', 'preFetchGetCountries' ],
invoke: [
{
src: 'getAllowTrackingOption',
Expand Down Expand Up @@ -462,13 +479,12 @@ export const coreProfilerStateMachineDefinition = createMachine( {
},
},
preExtensions: {
always: [
// immediately transition to extensions without any events as long as extensions fetching parallel has completed
{
target: 'extensions',
cond: () => true, // TODO: use a custom function to check on the parallel state using meta when we implement that. https://xstate.js.org/docs/guides/guards.html#guards-condition-functions
},
],
invoke: {
src: 'getExtensions',
onDone: [
{ target: 'extensions', actions: 'handleExtensions' },
],
},
// add exit action to filter the extensions using a custom function here and assign it to context.extensionsAvailable
exit: assign( {
extensionsAvailable: ( context ) => {
Expand Down
@@ -0,0 +1,31 @@
/**
* Internal dependencies
*/
import { CoreProfilerStateMachineContext, ExtensionsEvent } from '../index';

export const Extensions = ( {
context,
sendEvent,
}: {
context: CoreProfilerStateMachineContext;
sendEvent: ( payload: ExtensionsEvent ) => void;
} ) => {
return (
<>
<div>Extensions</div>
<div>{ JSON.stringify( context.extensionsAvailable ) }</div>
<button
onClick={ () =>
sendEvent( {
type: 'EXTENSIONS_COMPLETED',
payload: {
extensionsSelected: [ 'woocommerce-payments' ],
},
} )
}
>
Next
</button>
</>
);
};
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Added async fetching for extensions and countries lists in new core profiler