dev: refactor xstate update query params made generic (#51167)

* refactor: make updateQueryParam generic

* changelog
This commit is contained in:
RJ 2024-09-09 10:49:40 +08:00 committed by GitHub
parent 4f9b851316
commit 86c2358d18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 48 additions and 39 deletions

View File

@ -32,6 +32,11 @@ export type LaunchYourStoreComponentProps = {
className?: string; className?: string;
}; };
export type LaunchYourStoreQueryParams = {
sidebar?: 'hub' | 'launch-success';
content?: 'site-preview' | 'launch-store-success';
};
const LaunchStoreController = () => { const LaunchStoreController = () => {
useFullScreen( [ 'woocommerce-launch-your-store' ] ); useFullScreen( [ 'woocommerce-launch-your-store' ] );
useEffect( () => { useEffect( () => {

View File

@ -14,8 +14,14 @@ import { getSetting } from '@woocommerce/settings';
*/ */
import { LoadingPage } from './pages/loading'; import { LoadingPage } from './pages/loading';
import { SitePreviewPage } from './pages/site-preview'; import { SitePreviewPage } from './pages/site-preview';
import type { LaunchYourStoreComponentProps } from '..'; import type {
import { createQueryParamsListener, updateQueryParams } from '../common'; LaunchYourStoreComponentProps,
LaunchYourStoreQueryParams,
} from '..';
import {
updateQueryParams,
createQueryParamsListener,
} from '~/utils/xstate/url-handling';
import { import {
services as congratsServices, services as congratsServices,
events as congratsEvents, events as congratsEvents,
@ -54,11 +60,8 @@ export const mainContentMachine = setup( {
events: MainContentMachineEvents; events: MainContentMachineEvents;
}, },
actions: { actions: {
updateQueryParams: ( updateQueryParams: ( _, params: LaunchYourStoreQueryParams ) => {
_, updateQueryParams< LaunchYourStoreQueryParams >( params );
params: { sidebar?: string; content?: string }
) => {
updateQueryParams( params );
}, },
assignSiteCachedStatus: assign( { assignSiteCachedStatus: assign( {
siteIsShowingCachedContent: true, siteIsShowingCachedContent: true,
@ -92,9 +95,9 @@ export const mainContentMachine = setup( {
guards: { guards: {
hasContentLocation: ( hasContentLocation: (
_, _,
{ contentLocation }: { contentLocation: string } { content: contentLocation }: LaunchYourStoreQueryParams
) => { ) => {
const { content } = getQuery() as { content?: string }; const { content } = getQuery() as LaunchYourStoreQueryParams;
return !! content && content === contentLocation; return !! content && content === contentLocation;
}, },
}, },
@ -125,13 +128,13 @@ export const mainContentMachine = setup( {
{ {
guard: { guard: {
type: 'hasContentLocation', type: 'hasContentLocation',
params: { contentLocation: 'site-preview' }, params: { content: 'site-preview' },
}, },
}, },
{ {
guard: { guard: {
type: 'hasContentLocation', type: 'hasContentLocation',
params: { contentLocation: 'launch-store-success' }, params: { content: 'launch-store-success' },
}, },
target: 'launchStoreSuccess', target: 'launchStoreSuccess',
}, },

View File

@ -29,9 +29,15 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies * Internal dependencies
*/ */
import { LaunchYourStoreHubSidebar } from './components/launch-store-hub'; import { LaunchYourStoreHubSidebar } from './components/launch-store-hub';
import type { LaunchYourStoreComponentProps } from '..'; import type {
LaunchYourStoreComponentProps,
LaunchYourStoreQueryParams,
} from '..';
import type { mainContentMachine } from '../main-content/xstate'; import type { mainContentMachine } from '../main-content/xstate';
import { updateQueryParams, createQueryParamsListener } from '../common'; import {
updateQueryParams,
createQueryParamsListener,
} from '~/utils/xstate/url-handling';
import { taskClickedAction, getLysTasklist } from './tasklist'; import { taskClickedAction, getLysTasklist } from './tasklist';
import { fetchCongratsData } from '../main-content/pages/launch-store-success/services'; import { fetchCongratsData } from '../main-content/pages/launch-store-success/services';
import { getTimeFrame } from '~/utils'; import { getTimeFrame } from '~/utils';
@ -253,11 +259,8 @@ export const sidebarMachine = setup( {
( { context } ) => context.mainContentMachineRef, ( { context } ) => context.mainContentMachineRef,
{ type: 'SHOW_LOADING' } { type: 'SHOW_LOADING' }
), ),
updateQueryParams: ( updateQueryParams: ( _, params: LaunchYourStoreQueryParams ) => {
_, updateQueryParams< LaunchYourStoreQueryParams >( params );
params: { sidebar?: string; content?: string }
) => {
updateQueryParams( params );
}, },
taskClicked: ( { event } ) => { taskClicked: ( { event } ) => {
if ( event.type === 'TASK_CLICKED' ) { if ( event.type === 'TASK_CLICKED' ) {
@ -293,9 +296,9 @@ export const sidebarMachine = setup( {
guards: { guards: {
hasSidebarLocation: ( hasSidebarLocation: (
_, _,
{ sidebarLocation }: { sidebarLocation: string } { sidebar: sidebarLocation }: LaunchYourStoreQueryParams
) => { ) => {
const { sidebar } = getQuery() as { sidebar?: string }; const { sidebar } = getQuery() as LaunchYourStoreQueryParams;
return !! sidebar && sidebar === sidebarLocation; return !! sidebar && sidebar === sidebarLocation;
}, },
hasWooPayments: ( { context } ) => { hasWooPayments: ( { context } ) => {
@ -333,14 +336,14 @@ export const sidebarMachine = setup( {
{ {
guard: { guard: {
type: 'hasSidebarLocation', type: 'hasSidebarLocation',
params: { sidebarLocation: 'hub' }, params: { sidebar: 'hub' },
}, },
target: 'launchYourStoreHub', target: 'launchYourStoreHub',
}, },
{ {
guard: { guard: {
type: 'hasSidebarLocation', type: 'hasSidebarLocation',
params: { sidebarLocation: 'launch-success' }, params: { sidebar: 'launch-success' },
}, },
target: 'storeLaunchSuccessful', target: 'storeLaunchSuccessful',
}, },

View File

@ -5,7 +5,7 @@ import * as navigation from '@woocommerce/navigation';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { updateQueryParams, createQueryParamsListener } from '../common'; import { updateQueryParams, createQueryParamsListener } from '../url-handling';
jest.mock( '@woocommerce/navigation', () => ( { jest.mock( '@woocommerce/navigation', () => ( {
getHistory: jest.fn(), getHistory: jest.fn(),

View File

@ -42,27 +42,21 @@ export const createQueryParamsListener = (
}; };
}; };
type LaunchYourStoreQueryParams = { export const updateQueryParams = < T extends Record< string, string > >(
sidebar?: string; params: Partial< T >
content?: string;
};
export const updateQueryParams = (
params: Partial< LaunchYourStoreQueryParams >
) => { ) => {
const queryParams = getQuery() as LaunchYourStoreQueryParams; const queryParams = getQuery() as T;
const changes = Object.entries( params ).reduce( const changes = Object.entries( params ).reduce(
( acc: Partial< LaunchYourStoreQueryParams >, [ key, value ] ) => { ( acc: Partial< T >, [ key, value ] ) => {
// Check if the value is different from the current queryParams. Include if explicitly passed, even if it's undefined. // Check if the value is different from the current queryParams.
// This approach assumes that `params` can only have keys that are defined in LaunchYourStoreQueryParams. // Include if explicitly passed, even if it's undefined.
if ( if ( queryParams[ key as keyof T ] !== value ) {
queryParams[ key as keyof LaunchYourStoreQueryParams ] !== value acc[ key as keyof T ] = value;
) {
acc[ key as keyof LaunchYourStoreQueryParams ] = value;
} }
return acc; return acc;
}, },
{} {} as Partial< T >
); );
if ( Object.keys( changes ).length > 0 ) { if ( Object.keys( changes ).length > 0 ) {

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Made XState utils updateQueryParam generic so that it can be reused in other XState machines