Added shipping partner suggestions data handling in JS

This commit is contained in:
Ilyas Foo 2023-05-25 22:48:09 +08:00
parent 4db856db05
commit 6a87ef2658
7 changed files with 74 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -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( {

View File

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

View File

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

View File

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