Add free extensions data store (https://github.com/woocommerce/woocommerce-admin/pull/7420)
* Add free extensions to onboarding data store * Hook up extensions list to onboarding data store
This commit is contained in:
parent
6e2782c391
commit
16e7c8cd55
|
@ -2,12 +2,15 @@
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
import apiFetch from '@wordpress/api-fetch';
|
|
||||||
import { Card, CardHeader, Spinner } from '@wordpress/components';
|
import { Card, CardHeader, Spinner } from '@wordpress/components';
|
||||||
import { PLUGINS_STORE_NAME, WCDataSelector } from '@woocommerce/data';
|
import {
|
||||||
|
ONBOARDING_STORE_NAME,
|
||||||
|
PLUGINS_STORE_NAME,
|
||||||
|
WCDataSelector,
|
||||||
|
} from '@woocommerce/data';
|
||||||
import { recordEvent } from '@woocommerce/tracks';
|
import { recordEvent } from '@woocommerce/tracks';
|
||||||
import { Text } from '@woocommerce/experimental';
|
import { Text } from '@woocommerce/experimental';
|
||||||
import { useEffect, useMemo, useState } from '@wordpress/element';
|
import { useMemo, useState } from '@wordpress/element';
|
||||||
import { useSelect, useDispatch } from '@wordpress/data';
|
import { useSelect, useDispatch } from '@wordpress/data';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,44 +21,33 @@ import { createNoticesFromResponse } from '~/lib/notices';
|
||||||
import { PluginList, PluginListProps } from './PluginList';
|
import { PluginList, PluginListProps } from './PluginList';
|
||||||
import { PluginProps } from './Plugin';
|
import { PluginProps } from './Plugin';
|
||||||
|
|
||||||
type ExtensionList = {
|
|
||||||
key: string;
|
|
||||||
title: string;
|
|
||||||
plugins: Extension[];
|
|
||||||
};
|
|
||||||
|
|
||||||
type Extension = {
|
|
||||||
description: string;
|
|
||||||
key: string;
|
|
||||||
image_url: string;
|
|
||||||
manage_url: string;
|
|
||||||
name: string;
|
|
||||||
slug: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const ALLOWED_PLUGIN_LISTS = [ 'reach', 'grow' ];
|
const ALLOWED_PLUGIN_LISTS = [ 'reach', 'grow' ];
|
||||||
|
|
||||||
export const Marketing: React.FC = () => {
|
export const Marketing: React.FC = () => {
|
||||||
const [ fetchedExtensions, setFetchedExtensions ] = useState<
|
|
||||||
ExtensionList[]
|
|
||||||
>( [] );
|
|
||||||
const [ currentPlugin, setCurrentPlugin ] = useState< string | null >(
|
const [ currentPlugin, setCurrentPlugin ] = useState< string | null >(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
const [ isFetching, setIsFetching ] = useState( true );
|
|
||||||
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
|
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
|
||||||
const { activePlugins, installedPlugins } = useSelect(
|
const {
|
||||||
( select: WCDataSelector ) => {
|
activePlugins,
|
||||||
|
freeExtensions,
|
||||||
|
installedPlugins,
|
||||||
|
isResolving,
|
||||||
|
} = useSelect( ( select: WCDataSelector ) => {
|
||||||
const { getActivePlugins, getInstalledPlugins } = select(
|
const { getActivePlugins, getInstalledPlugins } = select(
|
||||||
PLUGINS_STORE_NAME
|
PLUGINS_STORE_NAME
|
||||||
);
|
);
|
||||||
|
const { getFreeExtensions, hasFinishedResolution } = select(
|
||||||
|
ONBOARDING_STORE_NAME
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
activePlugins: getActivePlugins(),
|
activePlugins: getActivePlugins(),
|
||||||
|
freeExtensions: getFreeExtensions(),
|
||||||
installedPlugins: getInstalledPlugins(),
|
installedPlugins: getInstalledPlugins(),
|
||||||
|
isResolving: ! hasFinishedResolution( 'getFreeExtensions' ),
|
||||||
};
|
};
|
||||||
}
|
} );
|
||||||
);
|
|
||||||
|
|
||||||
const transformExtensionToPlugin = (
|
const transformExtensionToPlugin = (
|
||||||
extension: Extension
|
extension: Extension
|
||||||
|
@ -73,26 +65,10 @@ export const Marketing: React.FC = () => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect( () => {
|
|
||||||
apiFetch( {
|
|
||||||
path: '/wc-admin/onboarding/free-extensions',
|
|
||||||
} )
|
|
||||||
.then( ( results: ExtensionList[] ) => {
|
|
||||||
if ( results?.length ) {
|
|
||||||
setFetchedExtensions( results );
|
|
||||||
}
|
|
||||||
setIsFetching( false );
|
|
||||||
} )
|
|
||||||
.catch( () => {
|
|
||||||
// @todo Handle error checking.
|
|
||||||
setIsFetching( false );
|
|
||||||
} );
|
|
||||||
}, [] );
|
|
||||||
|
|
||||||
const [ installedExtensions, pluginLists ] = useMemo( () => {
|
const [ installedExtensions, pluginLists ] = useMemo( () => {
|
||||||
const installed: PluginProps[] = [];
|
const installed: PluginProps[] = [];
|
||||||
const lists: PluginListProps[] = [];
|
const lists: PluginListProps[] = [];
|
||||||
fetchedExtensions.forEach( ( list ) => {
|
freeExtensions.forEach( ( list ) => {
|
||||||
if ( ! ALLOWED_PLUGIN_LISTS.includes( list.key ) ) {
|
if ( ! ALLOWED_PLUGIN_LISTS.includes( list.key ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +95,7 @@ export const Marketing: React.FC = () => {
|
||||||
} );
|
} );
|
||||||
|
|
||||||
return [ installed, lists ];
|
return [ installed, lists ];
|
||||||
}, [ installedPlugins, activePlugins, fetchedExtensions ] );
|
}, [ installedPlugins, activePlugins, freeExtensions ] );
|
||||||
|
|
||||||
const installAndActivate = ( slug: string ) => {
|
const installAndActivate = ( slug: string ) => {
|
||||||
setCurrentPlugin( slug );
|
setCurrentPlugin( slug );
|
||||||
|
@ -140,7 +116,7 @@ export const Marketing: React.FC = () => {
|
||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( isFetching ) {
|
if ( isResolving ) {
|
||||||
return <Spinner />;
|
return <Spinner />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ const TYPES = {
|
||||||
SET_PROFILE_ITEMS: 'SET_PROFILE_ITEMS',
|
SET_PROFILE_ITEMS: 'SET_PROFILE_ITEMS',
|
||||||
SET_TASKS_STATUS: 'SET_TASKS_STATUS',
|
SET_TASKS_STATUS: 'SET_TASKS_STATUS',
|
||||||
GET_PAYMENT_METHODS_SUCCESS: 'GET_PAYMENT_METHODS_SUCCESS',
|
GET_PAYMENT_METHODS_SUCCESS: 'GET_PAYMENT_METHODS_SUCCESS',
|
||||||
|
GET_FREE_EXTENSIONS_ERROR: 'GET_FREE_EXTENSIONS_ERROR',
|
||||||
|
GET_FREE_EXTENSIONS_SUCCESS: 'GET_FREE_EXTENSIONS_SUCCESS',
|
||||||
};
|
};
|
||||||
|
|
||||||
export default TYPES;
|
export default TYPES;
|
||||||
|
|
|
@ -9,6 +9,20 @@ import { apiFetch } from '@wordpress/data-controls';
|
||||||
import TYPES from './action-types';
|
import TYPES from './action-types';
|
||||||
import { WC_ADMIN_NAMESPACE } from '../constants';
|
import { WC_ADMIN_NAMESPACE } from '../constants';
|
||||||
|
|
||||||
|
export function getFreeExtensionsError( error ) {
|
||||||
|
return {
|
||||||
|
type: TYPES.GET_FREE_EXTENSIONS_ERROR,
|
||||||
|
error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFreeExtensionsSuccess( freeExtensions ) {
|
||||||
|
return {
|
||||||
|
type: TYPES.GET_FREE_EXTENSIONS_SUCCESS,
|
||||||
|
freeExtensions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function setError( selector, error ) {
|
export function setError( selector, error ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.SET_ERROR,
|
type: TYPES.SET_ERROR,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import TYPES from './action-types';
|
||||||
|
|
||||||
export const defaultState = {
|
export const defaultState = {
|
||||||
errors: {},
|
errors: {},
|
||||||
|
freeExtensions: [],
|
||||||
profileItems: {
|
profileItems: {
|
||||||
business_extensions: null,
|
business_extensions: null,
|
||||||
completed: null,
|
completed: null,
|
||||||
|
@ -28,6 +29,7 @@ export const defaultState = {
|
||||||
const onboarding = (
|
const onboarding = (
|
||||||
state = defaultState,
|
state = defaultState,
|
||||||
{
|
{
|
||||||
|
freeExtensions,
|
||||||
type,
|
type,
|
||||||
profileItems,
|
profileItems,
|
||||||
paymentMethods,
|
paymentMethods,
|
||||||
|
@ -72,6 +74,19 @@ const onboarding = (
|
||||||
...state,
|
...state,
|
||||||
paymentMethods,
|
paymentMethods,
|
||||||
};
|
};
|
||||||
|
case TYPES.GET_FREE_EXTENSIONS_ERROR:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
errors: {
|
||||||
|
...state.errors,
|
||||||
|
getFreeExtensions: error,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
case TYPES.GET_FREE_EXTENSIONS_SUCCESS:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
freeExtensions,
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ import { apiFetch } from '@wordpress/data-controls';
|
||||||
*/
|
*/
|
||||||
import { WC_ADMIN_NAMESPACE } from '../constants';
|
import { WC_ADMIN_NAMESPACE } from '../constants';
|
||||||
import {
|
import {
|
||||||
|
getFreeExtensionsError,
|
||||||
|
getFreeExtensionsSuccess,
|
||||||
setProfileItems,
|
setProfileItems,
|
||||||
setError,
|
setError,
|
||||||
setTasksStatus,
|
setTasksStatus,
|
||||||
|
@ -52,3 +54,16 @@ export function* getPaymentGatewaySuggestions() {
|
||||||
yield setError( 'getPaymentGatewaySuggestions', error );
|
yield setError( 'getPaymentGatewaySuggestions', error );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function* getFreeExtensions() {
|
||||||
|
try {
|
||||||
|
const results = yield apiFetch( {
|
||||||
|
path: WC_ADMIN_NAMESPACE + '/onboarding/free-extensions',
|
||||||
|
method: 'GET',
|
||||||
|
} );
|
||||||
|
|
||||||
|
yield getFreeExtensionsSuccess( results );
|
||||||
|
} catch ( error ) {
|
||||||
|
yield getFreeExtensionsError( error );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,12 @@
|
||||||
*/
|
*/
|
||||||
import { WPDataSelectors, RuleProcessor } from '../types';
|
import { WPDataSelectors, RuleProcessor } from '../types';
|
||||||
|
|
||||||
|
export const getFreeExtensions = (
|
||||||
|
state: OnboardingState
|
||||||
|
): ExtensionList[] => {
|
||||||
|
return state.freeExtensions || [];
|
||||||
|
};
|
||||||
|
|
||||||
export const getProfileItems = (
|
export const getProfileItems = (
|
||||||
state: OnboardingState
|
state: OnboardingState
|
||||||
): ProfileItemsState | Record< string, never > => {
|
): ProfileItemsState | Record< string, never > => {
|
||||||
|
@ -47,6 +53,7 @@ export type OnboardingSelectors = {
|
||||||
} & WPDataSelectors;
|
} & WPDataSelectors;
|
||||||
|
|
||||||
export type OnboardingState = {
|
export type OnboardingState = {
|
||||||
|
freeExtensions: ExtensionList[];
|
||||||
profileItems: ProfileItemsState;
|
profileItems: ProfileItemsState;
|
||||||
tasksStatus: TasksStatusState;
|
tasksStatus: TasksStatusState;
|
||||||
paymentMethods: PaymentMethodsState[];
|
paymentMethods: PaymentMethodsState[];
|
||||||
|
@ -150,3 +157,18 @@ export type PaymentMethodsState = {
|
||||||
api_details_url: string;
|
api_details_url: string;
|
||||||
manage_url: string;
|
manage_url: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ExtensionList = {
|
||||||
|
key: string;
|
||||||
|
title: string;
|
||||||
|
plugins: Extension[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Extension = {
|
||||||
|
description: string;
|
||||||
|
key: string;
|
||||||
|
image_url: string;
|
||||||
|
manage_url: string;
|
||||||
|
name: string;
|
||||||
|
slug: string;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue