From fd16dc0644d0b7476c751b26e41bbc6b87c4c072 Mon Sep 17 00:00:00 2001 From: RJ <27843274+rjchow@users.noreply.github.com> Date: Wed, 13 Nov 2024 17:07:07 +1100 Subject: [PATCH] fix: changed core profiler industry types to the correct format (#52660) * fix: changed core profiler industry types to the correct format - changed profiler API to match what we're actually storing, Industry[] - changed some core profiler steps to use the API instead of saving options directly --- .../changelog/fix-core-profiler-api-types | 4 ++ packages/js/data/src/onboarding/actions.ts | 7 ++- packages/js/data/src/onboarding/types.ts | 16 ++++-- packages/js/data/src/types/wp-data.ts | 2 +- .../changelog/fix-core-profiler-industry | 4 ++ .../admin/client/core-profiler/index.tsx | 52 +++++++++---------- .../src/Admin/API/OnboardingProfile.php | 30 ++++++++++- .../api/onboarding-profile.php | 13 +++-- 8 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 packages/js/data/changelog/fix-core-profiler-api-types create mode 100644 plugins/woocommerce/changelog/fix-core-profiler-industry diff --git a/packages/js/data/changelog/fix-core-profiler-api-types b/packages/js/data/changelog/fix-core-profiler-api-types new file mode 100644 index 00000000000..de437d33410 --- /dev/null +++ b/packages/js/data/changelog/fix-core-profiler-api-types @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix types in profiler api for business info diff --git a/packages/js/data/src/onboarding/actions.ts b/packages/js/data/src/onboarding/actions.ts index 686074d3d58..e68162c26b9 100644 --- a/packages/js/data/src/onboarding/actions.ts +++ b/packages/js/data/src/onboarding/actions.ts @@ -2,7 +2,7 @@ * External dependencies */ import { apiFetch } from '@wordpress/data-controls'; -import { controls } from '@wordpress/data'; +import { controls, dispatch } from '@wordpress/data'; /** * Internal dependencies @@ -324,6 +324,11 @@ export function* updateProfileItems( items: ProfileItems ) { yield setError( 'updateProfileItems', error ); yield setIsRequesting( 'updateProfileItems', false ); throw error; + } finally { + yield dispatch( OPTIONS_STORE_NAME ).invalidateResolution( + 'getOption', + [ 'woocommerce_onboarding_profile' ] + ); } } diff --git a/packages/js/data/src/onboarding/types.ts b/packages/js/data/src/onboarding/types.ts index 7e3e3058c73..9c10f388c89 100644 --- a/packages/js/data/src/onboarding/types.ts +++ b/packages/js/data/src/onboarding/types.ts @@ -89,9 +89,16 @@ export type OnboardingState = { jetpackAuthUrls: Record< string, GetJetpackAuthUrlResponse >; }; -export type Industry = { - slug: string; -}; +export type Industry = + | 'clothing_and_accessories' + | 'food_and_drink' + | 'electronics_and_computers' + | 'health_and_beauty' + | 'education_and_learning' + | 'home_furniture_and_garden' + | 'arts_and_crafts' + | 'sports_and_recreation' + | 'other'; export type GetJetpackAuthUrlResponse = { url: string; @@ -143,6 +150,9 @@ export type ProfileItems = { setup_client?: boolean | null; skipped?: boolean | null; is_plugins_page_skipped?: boolean | null; + business_choice?: string | null; + selling_online_answer?: string | null; + selling_platforms?: string[] | null; /** @deprecated This is always null, the theme step has been removed since WC 7.7. */ theme?: string | null; wccom_connected?: boolean | null; diff --git a/packages/js/data/src/types/wp-data.ts b/packages/js/data/src/types/wp-data.ts index f6deb9b4f39..2edec9eb306 100644 --- a/packages/js/data/src/types/wp-data.ts +++ b/packages/js/data/src/types/wp-data.ts @@ -16,7 +16,7 @@ export type WPDataSelectors = { export type WPDataActions = { startResolution: ( selector: string, args?: unknown[] ) => void; finishResolution: ( selector: string, args?: unknown[] ) => void; - invalidateResolution: ( selector: string ) => void; + invalidateResolution: ( selector: string, args?: unknown[] ) => void; invalidateResolutionForStore: ( selector: string ) => void; invalidateResolutionForStoreSelector: ( selector: string ) => void; }; diff --git a/plugins/woocommerce/changelog/fix-core-profiler-industry b/plugins/woocommerce/changelog/fix-core-profiler-industry new file mode 100644 index 00000000000..46a9531d708 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-core-profiler-industry @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix incorrect industry type in profiler api \ No newline at end of file diff --git a/plugins/woocommerce/client/admin/client/core-profiler/index.tsx b/plugins/woocommerce/client/admin/client/core-profiler/index.tsx index 789441357ab..42df6b955a1 100644 --- a/plugins/woocommerce/client/admin/client/core-profiler/index.tsx +++ b/plugins/woocommerce/client/admin/client/core-profiler/index.tsx @@ -379,14 +379,12 @@ const updateOnboardingProfileOption = fromPromise( async ( { input }: { input: CoreProfilerStateMachineContext } ) => { const { businessChoice, sellingOnlineAnswer, sellingPlatforms } = input.userProfile; - - return dispatch( OPTIONS_STORE_NAME ).updateOptions( { - woocommerce_onboarding_profile: { - ...input.onboardingProfile, - business_choice: businessChoice, + return dispatch( ONBOARDING_STORE_NAME ).updateProfileItems( { + ...( businessChoice && { business_choice: businessChoice } ), + ...( sellingOnlineAnswer && { selling_online_answer: sellingOnlineAnswer, - selling_platforms: sellingPlatforms, - }, + } ), + ...( sellingPlatforms && { selling_platforms: sellingPlatforms } ), } ); } ); @@ -471,28 +469,27 @@ const updateBusinessInfo = fromPromise( }: { input: { payload: BusinessInfoPayload; + context: CoreProfilerStateMachineContext; }; } ) => { - const refreshedOnboardingProfile = ( await resolveSelect( - OPTIONS_STORE_NAME - ).getOption( 'woocommerce_onboarding_profile' ) ) as OnboardingProfile; - - await updateStoreCurrency( input.payload.storeLocation ); - - return dispatch( OPTIONS_STORE_NAME ).updateOptions( { - blogname: input.payload.storeName, - woocommerce_default_country: input.payload.storeLocation, - woocommerce_onboarding_profile: { - ...refreshedOnboardingProfile, + return Promise.all( [ + updateStoreCurrency( input.payload.storeLocation ), + dispatch( ONBOARDING_STORE_NAME ).updateProfileItems( { is_store_country_set: true, - industry: [ input.payload.industry ], is_agree_marketing: input.payload.isOptInMarketing, - store_email: - input.payload.storeEmailAddress.length > 0 - ? input.payload.storeEmailAddress - : null, - }, - } ); + ...( input.payload.industry && { + industry: [ input.payload.industry ], + } ), + ...( input.payload.storeEmailAddress !== + input.context.onboardingProfile.store_email && { + store_email: input.payload.storeEmailAddress, + } ), + } ), + dispatch( OPTIONS_STORE_NAME ).updateOptions( { + blogname: input.payload.storeName, + woocommerce_default_country: input.payload.storeLocation, + } ), + ] ); } ); @@ -1175,7 +1172,10 @@ export const coreProfilerStateMachineDefinition = createMachine( { postBusinessInfo: { invoke: { src: 'updateBusinessInfo', - input: ( { event } ) => event, + input: ( { event, context } ) => { + assertEvent( event, 'BUSINESS_INFO_COMPLETED' ); + return { payload: event.payload, context }; + }, onDone: { target: '#plugins', }, diff --git a/plugins/woocommerce/src/Admin/API/OnboardingProfile.php b/plugins/woocommerce/src/Admin/API/OnboardingProfile.php index 7fd8926549e..e5ee44f9ae9 100644 --- a/plugins/woocommerce/src/Admin/API/OnboardingProfile.php +++ b/plugins/woocommerce/src/Admin/API/OnboardingProfile.php @@ -280,9 +280,10 @@ class OnboardingProfile extends \WC_REST_Data_Controller { 'description' => __( 'Industry.', 'woocommerce' ), 'context' => array( 'view' ), 'readonly' => true, + 'nullable' => true, 'validate_callback' => 'rest_validate_request_arg', 'items' => array( - 'type' => 'object', + 'type' => 'string', ), ), 'product_types' => array( @@ -426,6 +427,7 @@ class OnboardingProfile extends \WC_REST_Data_Controller { 'description' => __( 'Store email address.', 'woocommerce' ), 'context' => array( 'view' ), 'readonly' => true, + 'nullable' => true, 'validate_callback' => array( __CLASS__, 'rest_validate_marketing_email' ), ), 'is_store_country_set' => array( @@ -442,6 +444,30 @@ class OnboardingProfile extends \WC_REST_Data_Controller { 'readonly' => true, 'validate_callback' => 'rest_validate_request_arg', ), + 'business_choice' => array( + 'type' => 'string', + 'description' => __( 'Business choice.', 'woocommerce' ), + 'context' => array( 'view' ), + 'readonly' => true, + 'nullable' => true, + ), + 'selling_online_answer' => array( + 'type' => 'string', + 'description' => __( 'Selling online answer.', 'woocommerce' ), + 'context' => array( 'view' ), + 'readonly' => true, + 'nullable' => true, + ), + 'selling_platforms' => array( + 'type' => array( 'array', 'null' ), + 'description' => __( 'Selling platforms.', 'woocommerce' ), + 'context' => array( 'view' ), + 'readonly' => true, + 'nullable' => true, + 'items' => array( + 'type' => array( 'string', 'null' ), + ), + ), ); return apply_filters( 'woocommerce_rest_onboarding_profile_properties', $properties ); @@ -461,7 +487,7 @@ class OnboardingProfile extends \WC_REST_Data_Controller { ( $is_agree_marketing || ! empty( $value ) ) && ! is_email( $value ) ) { return new \WP_Error( 'rest_invalid_email', __( 'Invalid email address', 'woocommerce' ) ); - }; + } return true; } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-profile.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-profile.php index 5c89c1c5ba3..27c7a006915 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-profile.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/onboarding-profile.php @@ -62,11 +62,7 @@ class WC_Admin_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { // Test updating 2 fields separately. $request = new WP_REST_Request( 'POST', $this->endpoint ); $request->set_headers( array( 'content-type' => 'application/json' ) ); - $industry = array( - array( - 'slug' => 'health-beauty', - ), - ); + $industry = array( 'health-beauty' ); $request->set_body( wp_json_encode( array( @@ -114,7 +110,7 @@ class WC_Admin_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'health-beauty', $data['industry'][0]['slug'] ); + $this->assertEquals( 'health-beauty', $data['industry'][0] ); $this->assertEquals( 'storefront', $data['theme'] ); } @@ -131,7 +127,7 @@ class WC_Admin_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 18, $properties ); + $this->assertCount( 21, $properties ); $this->assertArrayHasKey( 'completed', $properties ); $this->assertArrayHasKey( 'skipped', $properties ); $this->assertArrayHasKey( 'industry', $properties ); @@ -150,6 +146,9 @@ class WC_Admin_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { $this->assertArrayHasKey( 'store_email', $properties ); $this->assertArrayHasKey( 'is_store_country_set', $properties ); $this->assertArrayHasKey( 'is_plugins_page_skipped', $properties ); + $this->assertArrayHasKey( 'business_choice', $properties ); + $this->assertArrayHasKey( 'selling_online_answer', $properties ); + $this->assertArrayHasKey( 'selling_platforms', $properties ); } /**