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
This commit is contained in:
RJ 2024-11-13 17:07:07 +11:00 committed by GitHub
parent 5efefbb71a
commit fd16dc0644
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 88 additions and 40 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix types in profiler api for business info

View File

@ -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' ]
);
}
}

View File

@ -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;

View File

@ -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;
};

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix incorrect industry type in profiler api

View File

@ -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',
},

View File

@ -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;
}

View File

@ -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 );
}
/**