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_PROFILE_ITEMS: 'SET_PROFILE_ITEMS',
SET_EMAIL_PREFILL: 'SET_EMAIL_PREFILL', SET_EMAIL_PREFILL: 'SET_EMAIL_PREFILL',
GET_PAYMENT_METHODS_SUCCESS: 'GET_PAYMENT_METHODS_SUCCESS', 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_SUCCESS: 'GET_PRODUCT_TYPES_SUCCESS',
GET_PRODUCT_TYPES_ERROR: 'GET_PRODUCT_TYPES_ERROR', GET_PRODUCT_TYPES_ERROR: 'GET_PRODUCT_TYPES_ERROR',
GET_FREE_EXTENSIONS_ERROR: 'GET_FREE_EXTENSIONS_ERROR', GET_FREE_EXTENSIONS_ERROR: 'GET_FREE_EXTENSIONS_ERROR',

View File

@ -17,6 +17,7 @@ import {
TaskListType, TaskListType,
TaskType, TaskType,
OnboardingProductTypes, OnboardingProductTypes,
ShippingMethod,
} from './types'; } from './types';
import { Plugin } from '../plugins/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 ) { export function setEmailPrefill( email: string ) {
return { return {
type: TYPES.SET_EMAIL_PREFILL, type: TYPES.SET_EMAIL_PREFILL,
@ -495,6 +503,7 @@ export type Action = ReturnType<
| typeof keepCompletedTaskListSuccess | typeof keepCompletedTaskListSuccess
| typeof visitedTask | typeof visitedTask
| typeof setPaymentMethods | typeof setPaymentMethods
| typeof setShippingMethods
| typeof setEmailPrefill | typeof setEmailPrefill
| typeof actionTaskError | typeof actionTaskError
| typeof actionTaskSuccess | typeof actionTaskSuccess

View File

@ -35,6 +35,7 @@ export const defaultState: OnboardingState = {
}, },
emailPrefill: '', emailPrefill: '',
paymentMethods: [], paymentMethods: [],
shippingMethods: [],
productTypes: {}, productTypes: {},
requesting: {}, requesting: {},
taskLists: {}, taskLists: {},
@ -104,6 +105,11 @@ const reducer: Reducer< OnboardingState, Action > = (
...state, ...state,
paymentMethods: action.paymentMethods, paymentMethods: action.paymentMethods,
}; };
case TYPES.GET_SHIPPING_METHODS_SUCCESS:
return {
...state,
shippingMethods: action.shippingMethods,
};
case TYPES.GET_PRODUCT_TYPES_SUCCESS: case TYPES.GET_PRODUCT_TYPES_SUCCESS:
return { return {
...state, ...state,

View File

@ -17,6 +17,7 @@ import {
setProfileItems, setProfileItems,
setError, setError,
setPaymentMethods, setPaymentMethods,
setShippingMethods,
setEmailPrefill, setEmailPrefill,
getProductTypesSuccess, getProductTypesSuccess,
getProductTypesError, getProductTypesError,
@ -26,6 +27,7 @@ import {
ExtensionList, ExtensionList,
OnboardingProductTypes, OnboardingProductTypes,
ProfileItems, ProfileItems,
ShippingMethod,
TaskListType, TaskListType,
} from './types'; } from './types';
import { Plugin } from '../plugins/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() { export function* getFreeExtensions() {
try { try {
const results: ExtensionList[] = yield apiFetch( { const results: ExtensionList[] = yield apiFetch( {

View File

@ -12,6 +12,7 @@ import {
OnboardingState, OnboardingState,
ExtensionList, ExtensionList,
ProfileItems, ProfileItems,
ShippingMethod,
} from './types'; } from './types';
import { WPDataSelectors } from '../types'; import { WPDataSelectors } from '../types';
import { Plugin } from '../plugins/types'; import { Plugin } from '../plugins/types';
@ -73,6 +74,12 @@ export const getPaymentGatewaySuggestions = (
return state.paymentMethods || []; return state.paymentMethods || [];
}; };
export const getShippingPartnerSuggestions = (
state: OnboardingState
): ShippingMethod[] => {
return state.shippingMethods || [];
};
export const getOnboardingError = ( export const getOnboardingError = (
state: OnboardingState, state: OnboardingState,
selector: string 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 * Internal dependencies
*/ */
import { Plugin } from '../plugins/types'; import type { Plugin } from '../plugins/types';
import type { ShippingMethod } from './shipping-method-types';
export type TaskType = { export type TaskType = {
actionLabel?: string; actionLabel?: string;
@ -75,6 +76,7 @@ export type OnboardingState = {
profileItems: ProfileItems; profileItems: ProfileItems;
taskLists: Record< string, TaskListType >; taskLists: Record< string, TaskListType >;
paymentMethods: Plugin[]; paymentMethods: Plugin[];
shippingMethods: ShippingMethod[];
productTypes: OnboardingProductTypes; productTypes: OnboardingProductTypes;
emailPrefill: string; emailPrefill: string;
// TODO clarify what the error record's type is // TODO clarify what the error record's type is
@ -182,3 +184,5 @@ export type Extension = {
is_built_by_wc: boolean; is_built_by_wc: boolean;
is_visible: boolean; is_visible: boolean;
}; };
export type { ShippingMethod };