diff --git a/packages/js/data/src/onboarding/action-types.ts b/packages/js/data/src/onboarding/action-types.ts index 690016fbbb5..83dd57a0268 100644 --- a/packages/js/data/src/onboarding/action-types.ts +++ b/packages/js/data/src/onboarding/action-types.ts @@ -4,6 +4,7 @@ const TYPES = { SET_PROFILE_ITEMS: 'SET_PROFILE_ITEMS', SET_EMAIL_PREFILL: 'SET_EMAIL_PREFILL', GET_PAYMENT_METHODS_SUCCESS: 'GET_PAYMENT_METHODS_SUCCESS', + GET_SHIPPING_METHODS_SUCCESS: 'GET_SHIPPING_METHODS_SUCCESS', GET_PRODUCT_TYPES_SUCCESS: 'GET_PRODUCT_TYPES_SUCCESS', GET_PRODUCT_TYPES_ERROR: 'GET_PRODUCT_TYPES_ERROR', GET_FREE_EXTENSIONS_ERROR: 'GET_FREE_EXTENSIONS_ERROR', diff --git a/packages/js/data/src/onboarding/actions.ts b/packages/js/data/src/onboarding/actions.ts index e46692dfca5..3253ebb1154 100644 --- a/packages/js/data/src/onboarding/actions.ts +++ b/packages/js/data/src/onboarding/actions.ts @@ -17,6 +17,7 @@ import { TaskListType, TaskType, OnboardingProductTypes, + ShippingMethod, } from './types'; import { Plugin } from '../plugins/types'; @@ -238,6 +239,13 @@ export function setPaymentMethods( paymentMethods: Plugin[] ) { }; } +export function setShippingMethods( shippingMethods: ShippingMethod[] ) { + return { + type: TYPES.GET_SHIPPING_METHODS_SUCCESS, + shippingMethods, + }; +} + export function setEmailPrefill( email: string ) { return { type: TYPES.SET_EMAIL_PREFILL, @@ -495,6 +503,7 @@ export type Action = ReturnType< | typeof keepCompletedTaskListSuccess | typeof visitedTask | typeof setPaymentMethods + | typeof setShippingMethods | typeof setEmailPrefill | typeof actionTaskError | typeof actionTaskSuccess diff --git a/packages/js/data/src/onboarding/reducer.ts b/packages/js/data/src/onboarding/reducer.ts index da69fae2c92..4e5b8719875 100644 --- a/packages/js/data/src/onboarding/reducer.ts +++ b/packages/js/data/src/onboarding/reducer.ts @@ -35,6 +35,7 @@ export const defaultState: OnboardingState = { }, emailPrefill: '', paymentMethods: [], + shippingMethods: [], productTypes: {}, requesting: {}, taskLists: {}, @@ -104,6 +105,11 @@ const reducer: Reducer< OnboardingState, Action > = ( ...state, paymentMethods: action.paymentMethods, }; + case TYPES.GET_SHIPPING_METHODS_SUCCESS: + return { + ...state, + shippingMethods: action.shippingMethods, + }; case TYPES.GET_PRODUCT_TYPES_SUCCESS: return { ...state, diff --git a/packages/js/data/src/onboarding/resolvers.ts b/packages/js/data/src/onboarding/resolvers.ts index 8674a4e7d83..fc784df4657 100644 --- a/packages/js/data/src/onboarding/resolvers.ts +++ b/packages/js/data/src/onboarding/resolvers.ts @@ -17,6 +17,7 @@ import { setProfileItems, setError, setPaymentMethods, + setShippingMethods, setEmailPrefill, getProductTypesSuccess, getProductTypesError, @@ -26,6 +27,7 @@ import { ExtensionList, OnboardingProductTypes, ProfileItems, + ShippingMethod, TaskListType, } from './types'; import { Plugin } from '../plugins/types'; @@ -111,6 +113,25 @@ export function* getPaymentGatewaySuggestions( } } +export function* getShippingPartnerSuggestions( + forceDefaultSuggestions = false +) { + let path = WC_ADMIN_NAMESPACE + '/shipping-partner-suggestions'; + if ( forceDefaultSuggestions ) { + path += '?force_default_suggestions=true'; + } + try { + const results: ShippingMethod[] = yield apiFetch( { + path, + method: 'GET', + } ); + + yield setShippingMethods( results ); + } catch ( error ) { + yield setError( 'getPaymentGatewaySuggestions', error ); + } +} + export function* getFreeExtensions() { try { const results: ExtensionList[] = yield apiFetch( { diff --git a/packages/js/data/src/onboarding/selectors.ts b/packages/js/data/src/onboarding/selectors.ts index 508886fbfb7..e9e39912020 100644 --- a/packages/js/data/src/onboarding/selectors.ts +++ b/packages/js/data/src/onboarding/selectors.ts @@ -12,6 +12,7 @@ import { OnboardingState, ExtensionList, ProfileItems, + ShippingMethod, } from './types'; import { WPDataSelectors } from '../types'; import { Plugin } from '../plugins/types'; @@ -73,6 +74,12 @@ export const getPaymentGatewaySuggestions = ( return state.paymentMethods || []; }; +export const getShippingPartnerSuggestions = ( + state: OnboardingState +): ShippingMethod[] => { + return state.shippingMethods || []; +}; + export const getOnboardingError = ( state: OnboardingState, selector: string diff --git a/packages/js/data/src/onboarding/shipping-method-types.ts b/packages/js/data/src/onboarding/shipping-method-types.ts new file mode 100644 index 00000000000..dafd533f574 --- /dev/null +++ b/packages/js/data/src/onboarding/shipping-method-types.ts @@ -0,0 +1,25 @@ +// Types to descript shipping method object. +type Feature = { + icon: string; + title?: string; + description: string; +}; + +type Layout = { + image: string; + features: Feature[]; +}; + +type LayoutType = 'row' | 'column'; + +export type ShippingMethod = { + id: string; + slug: string; + description: string; + learn_more_link: string; + is_visible: boolean; + available_layouts: LayoutType[]; + layout_column?: Layout; + layout_row?: Layout; + dependencies?: string[]; +}; diff --git a/packages/js/data/src/onboarding/types.ts b/packages/js/data/src/onboarding/types.ts index 217cac646b0..82877e69017 100644 --- a/packages/js/data/src/onboarding/types.ts +++ b/packages/js/data/src/onboarding/types.ts @@ -1,7 +1,8 @@ /** * Internal dependencies */ -import { Plugin } from '../plugins/types'; +import type { Plugin } from '../plugins/types'; +import type { ShippingMethod } from './shipping-method-types'; export type TaskType = { actionLabel?: string; @@ -75,6 +76,7 @@ export type OnboardingState = { profileItems: ProfileItems; taskLists: Record< string, TaskListType >; paymentMethods: Plugin[]; + shippingMethods: ShippingMethod[]; productTypes: OnboardingProductTypes; emailPrefill: string; // TODO clarify what the error record's type is @@ -182,3 +184,5 @@ export type Extension = { is_built_by_wc: boolean; is_visible: boolean; }; + +export type { ShippingMethod };