Move types to ./types.ts

This commit is contained in:
Chi-Hsuan Huang 2022-04-25 15:35:24 +08:00
parent 843a59c0c4
commit 0919e241ae
4 changed files with 136 additions and 174 deletions

View File

@ -6,8 +6,14 @@ import createSelector from 'rememo';
/**
* Internal dependencies
*/
import { TaskType, TaskListType } from './types';
import { WPDataSelectors } from '../types';
import {
TaskType,
TaskListType,
OnboardingState,
ExtensionList,
ProfileItemsState,
Product,
} from './types';
import { Plugin } from '../plugins/types';
export const getFreeExtensions = (
@ -22,8 +28,6 @@ export const getProfileItems = (
return state.profileItems || {};
};
const EMPTY_ARRAY: Product[] = [];
export const getTaskLists = createSelector(
( state: OnboardingState ): TaskListType[] => {
return Object.values( state.taskLists );
@ -88,122 +92,5 @@ export const getEmailPrefill = ( state: OnboardingState ): string => {
};
export const getProductTypes = ( state: OnboardingState ): Product[] => {
return state.productTypes || EMPTY_ARRAY;
};
// Types
export type OnboardingSelectors = {
getProfileItems: () => ReturnType< typeof getProfileItems >;
getPaymentGatewaySuggestions: () => ReturnType<
typeof getPaymentGatewaySuggestions
>;
getOnboardingError: () => ReturnType< typeof getOnboardingError >;
isOnboardingRequesting: () => ReturnType< typeof isOnboardingRequesting >;
getTaskListsByIds: (
ids: string[]
) => ReturnType< typeof getTaskListsByIds >;
getTaskLists: () => ReturnType< typeof getTaskLists >;
getTaskList: ( id: string ) => ReturnType< typeof getTaskList >;
getFreeExtensions: () => ReturnType< typeof getFreeExtensions >;
} & WPDataSelectors;
export type OnboardingState = {
freeExtensions: ExtensionList[];
profileItems: ProfileItemsState;
taskLists: Record< string, TaskListType >;
paymentMethods: Plugin[];
productTypes: Product[];
emailPrefill: string;
// TODO clarify what the error record's type is
errors: Record< string, unknown >;
requesting: Record< string, boolean >;
};
export type Industry = {
slug: string;
};
export type ProductCount = '0' | '1-10' | '11-100' | '101 - 1000' | '1000+';
export type ProductTypeSlug =
| 'physical'
| 'bookings'
| 'download'
| 'memberships'
| 'product-add-ons'
| 'product-bundles'
| 'subscriptions';
export type OtherPlatformSlug =
| 'shopify'
| 'bigcommerce'
| 'wix'
| 'amazon'
| 'ebay'
| 'etsy'
| 'squarespace'
| 'other';
export type RevenueTypeSlug =
| 'none'
| 'rather-not-say'
| 'up-to-2500'
| '2500-10000'
| '10000-50000'
| '50000-250000'
| 'more-than-250000';
export type ProfileItemsState = {
business_extensions: [ ] | null;
completed: boolean | null;
industry: Industry[] | null;
number_employees: string | null;
other_platform: OtherPlatformSlug | null;
other_platform_name: string | null;
product_count: ProductCount | null;
product_types: ProductTypeSlug[] | null;
revenue: RevenueTypeSlug | null;
selling_venues: string | null;
setup_client: boolean | null;
skipped: boolean | null;
theme: string | null;
wccom_connected: boolean | null;
is_agree_marketing: boolean | null;
store_email: string | null;
};
export type FieldLocale = {
locale: string;
label: string;
};
export type MethodFields = {
name: string;
option?: string;
label?: string;
locales?: FieldLocale[];
type?: string;
value?: string;
};
export type Product = {
default?: boolean;
label: string;
product?: number;
};
export type ExtensionList = {
key: string;
title: string;
plugins: Extension[];
};
export type Extension = {
description: string;
key: string;
image_url: string;
manage_url: string;
name: string;
is_built_by_wc: boolean;
is_visible: boolean;
return state.productTypes || [];
};

View File

@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import { Plugin } from '../plugins/types';
export type TaskType = {
actionLabel?: string;
actionUrl?: string;
@ -51,3 +56,103 @@ export type TaskListType = {
isCollapsible?: boolean;
isExpandable?: boolean;
};
export type OnboardingState = {
freeExtensions: ExtensionList[];
profileItems: ProfileItemsState;
taskLists: Record< string, TaskListType >;
paymentMethods: Plugin[];
productTypes: Product[];
emailPrefill: string;
// TODO clarify what the error record's type is
errors: Record< string, unknown >;
requesting: Record< string, boolean >;
};
export type Industry = {
slug: string;
};
export type ProductCount = '0' | '1-10' | '11-100' | '101 - 1000' | '1000+';
export type ProductTypeSlug =
| 'physical'
| 'bookings'
| 'download'
| 'memberships'
| 'product-add-ons'
| 'product-bundles'
| 'subscriptions';
export type OtherPlatformSlug =
| 'shopify'
| 'bigcommerce'
| 'wix'
| 'amazon'
| 'ebay'
| 'etsy'
| 'squarespace'
| 'other';
export type RevenueTypeSlug =
| 'none'
| 'rather-not-say'
| 'up-to-2500'
| '2500-10000'
| '10000-50000'
| '50000-250000'
| 'more-than-250000';
export type ProfileItemsState = {
business_extensions: [ ] | null;
completed: boolean | null;
industry: Industry[] | null;
number_employees: string | null;
other_platform: OtherPlatformSlug | null;
other_platform_name: string | null;
product_count: ProductCount | null;
product_types: ProductTypeSlug[] | null;
revenue: RevenueTypeSlug | null;
selling_venues: string | null;
setup_client: boolean | null;
skipped: boolean | null;
theme: string | null;
wccom_connected: boolean | null;
is_agree_marketing: boolean | null;
store_email: string | null;
};
export type FieldLocale = {
locale: string;
label: string;
};
export type MethodFields = {
name: string;
option?: string;
label?: string;
locales?: FieldLocale[];
type?: string;
value?: string;
};
export type Product = {
default?: boolean;
label: string;
product?: number;
};
export type ExtensionList = {
key: string;
title: string;
plugins: Extension[];
};
export type Extension = {
description: string;
key: string;
image_url: string;
manage_url: string;
name: string;
is_built_by_wc: boolean;
};

View File

@ -13,7 +13,7 @@ import { controls } from '@wordpress/data';
/**
* Internal dependencies
*/
import { pluginNames, STORE_NAME } from './constants';
import { STORE_NAME } from './constants';
import { ACTION_TYPES as TYPES } from './action-types';
import { WC_ADMIN_NAMESPACE } from '../constants';
import { WPError } from '../types';
@ -22,6 +22,8 @@ import {
PluginNames,
SelectorKeysWithActions,
RecommendedTypes,
InstallPluginsResponse,
ActivatePluginsResponse,
} from './types';
// Can be removed in WP 5.9, wp.data is supported in >5.7.
@ -30,63 +32,12 @@ const dispatch =
const resolveSelect =
controls && controls.resolveSelect ? controls.resolveSelect : select;
type PluginsResponse< PluginData > = {
data: PluginData;
errors: WPError< PluginNames >;
success: boolean;
message: string;
} & Response;
export type InstallPluginsResponse = PluginsResponse< {
installed: string[];
results: Record< string, boolean >;
install_time?: Record< string, number >;
} >;
type ActivatePluginsResponse = PluginsResponse< {
activated: string[];
active: string[];
} >;
function isWPError(
error: WPError< PluginNames > | Error | string
): error is WPError< PluginNames > {
return ( error as WPError ).errors !== undefined;
}
class PluginError extends Error {
constructor( message: string, public data: unknown ) {
super( message );
}
}
function formatErrors(
response: WPError< PluginNames > | Error | string
): string {
if ( isWPError( response ) ) {
// Replace the slug with a plugin name if a constant exists.
( Object.keys( response.errors ) as PluginNames[] ).forEach(
( plugin ) => {
response.errors[ plugin ] = response.errors[ plugin ].map(
( pluginError ) => {
return pluginNames[ plugin ]
? pluginError.replace(
`\`${ plugin }\``,
pluginNames[ plugin ]
)
: pluginError;
}
);
}
);
} else if ( typeof response === 'string' ) {
return response;
} else {
return response.message;
}
return '';
}
const formatErrorMessage = (
pluginErrors: Record< PluginNames, string[] >,
actionType = 'install'

View File

@ -2,6 +2,7 @@
* Internal dependencies
*/
import { pluginNames } from './constants';
import { WPError } from '../types';
export type RecommendedTypes = 'payments';
@ -61,3 +62,21 @@ export type PaypalOnboardingStatus = {
onboarded: boolean;
};
};
type PluginsResponse< PluginData > = {
data: PluginData;
errors: WPError< PluginNames >;
success: boolean;
message: string;
} & Response;
export type InstallPluginsResponse = PluginsResponse< {
installed: string[];
results: Record< string, boolean >;
install_time?: Record< string, number >;
} >;
export type ActivatePluginsResponse = PluginsResponse< {
activated: string[];
active: string[];
} >;