Add fallback for the select/dispatch data-controls for older WP versions (https://github.com/woocommerce/woocommerce-admin/pull/7204)

* Add fallback for the select/dispatch data-controls for older WP versions

* Also update dispatch in payment-gateways and rename oldDispatch to depreciatedDispatch

* Add comments
This commit is contained in:
louwie17 2021-06-18 00:31:37 -03:00 committed by GitHub
parent c558a5bceb
commit 18479c66e5
5 changed files with 51 additions and 29 deletions

View File

@ -1,7 +1,10 @@
/** /**
* External dependencies * External dependencies
*/ */
import { apiFetch } from '@wordpress/data-controls'; import {
apiFetch,
dispatch as depreciatedDispatch,
} from '@wordpress/data-controls';
import { controls } from '@wordpress/data'; import { controls } from '@wordpress/data';
/** /**
@ -19,6 +22,10 @@ import {
import { API_NAMESPACE, STORE_KEY } from './constants'; import { API_NAMESPACE, STORE_KEY } from './constants';
import { PaymentGateway } from './types'; import { PaymentGateway } from './types';
// Can be removed in WP 5.9.
const dispatch =
controls && controls.dispatch ? controls.dispatch : depreciatedDispatch;
export function* getPaymentGateways() { export function* getPaymentGateways() {
yield getPaymentGatewaysRequest(); yield getPaymentGatewaysRequest();
@ -28,7 +35,7 @@ export function* getPaymentGateways() {
} ); } );
yield getPaymentGatewaysSuccess( response ); yield getPaymentGatewaysSuccess( response );
for ( let i = 0; i < response.length; i++ ) { for ( let i = 0; i < response.length; i++ ) {
yield controls.dispatch( yield dispatch(
STORE_KEY, STORE_KEY,
'finishResolution', 'finishResolution',
'getPaymentGateway', 'getPaymentGateway',

View File

@ -1,7 +1,11 @@
/** /**
* External dependencies * External dependencies
*/ */
import { apiFetch } from '@wordpress/data-controls'; import {
apiFetch,
select,
dispatch as depreciatedDispatch,
} from '@wordpress/data-controls';
import { controls } from '@wordpress/data'; import { controls } from '@wordpress/data';
import { _n, sprintf } from '@wordpress/i18n'; import { _n, sprintf } from '@wordpress/i18n';
@ -18,6 +22,12 @@ import {
SelectorKeysWithActions, SelectorKeysWithActions,
} from './types'; } from './types';
// Can be removed in WP 5.9, wp.data is supported in >5.7.
const dispatch =
controls && controls.dispatch ? controls.dispatch : depreciatedDispatch;
const resolveSelect =
controls && controls.resolveSelect ? controls.resolveSelect : select;
type PluginsResponse< PluginData > = { type PluginsResponse< PluginData > = {
data: PluginData; data: PluginData;
errors: WPError< PluginNames >; errors: WPError< PluginNames >;
@ -235,8 +245,8 @@ export function* activatePlugins( plugins: string[] ) {
export function* installAndActivatePlugins( plugins: string[] ) { export function* installAndActivatePlugins( plugins: string[] ) {
try { try {
yield controls.dispatch( STORE_NAME, 'installPlugins', plugins ); yield dispatch( STORE_NAME, 'installPlugins', plugins );
const activations: InstallPluginsResponse = yield controls.dispatch( const activations: InstallPluginsResponse = yield dispatch(
STORE_NAME, STORE_NAME,
'activatePlugins', 'activatePlugins',
plugins plugins
@ -248,25 +258,20 @@ export function* installAndActivatePlugins( plugins: string[] ) {
} }
export const createErrorNotice = ( errorMessage: string ) => { export const createErrorNotice = ( errorMessage: string ) => {
return controls.dispatch( return dispatch( 'core/notices', 'createNotice', 'error', errorMessage );
'core/notices',
'createNotice',
'error',
errorMessage
);
}; };
export function* connectToJetpack( export function* connectToJetpack(
getAdminLink: ( endpoint: string ) => string getAdminLink: ( endpoint: string ) => string
) { ) {
const url: string = yield controls.resolveSelect( const url: string = yield resolveSelect(
STORE_NAME, STORE_NAME,
'getJetpackConnectUrl', 'getJetpackConnectUrl',
{ {
redirect_url: getAdminLink( 'admin.php?page=wc-admin' ), redirect_url: getAdminLink( 'admin.php?page=wc-admin' ),
} }
); );
const error: string = yield controls.resolveSelect( const error: string = yield resolveSelect(
STORE_NAME, STORE_NAME,
'getPluginsError', 'getPluginsError',
'getJetpackConnectUrl' 'getJetpackConnectUrl'
@ -284,10 +289,10 @@ export function* installJetpackAndConnect(
getAdminLink: ( endpoint: string ) => string getAdminLink: ( endpoint: string ) => string
) { ) {
try { try {
yield controls.dispatch( STORE_NAME, 'installPlugins', [ 'jetpack' ] ); yield dispatch( STORE_NAME, 'installPlugins', [ 'jetpack' ] );
yield controls.dispatch( STORE_NAME, 'activatePlugins', [ 'jetpack' ] ); yield dispatch( STORE_NAME, 'activatePlugins', [ 'jetpack' ] );
const url: string = yield controls.dispatch( const url: string = yield dispatch(
STORE_NAME, STORE_NAME,
'connectToJetpack', 'connectToJetpack',
getAdminLink getAdminLink
@ -304,7 +309,7 @@ export function* connectToJetpackWithFailureRedirect(
getAdminLink: ( endpoint: string ) => string getAdminLink: ( endpoint: string ) => string
) { ) {
try { try {
const url: string = yield controls.dispatch( const url: string = yield dispatch(
STORE_NAME, STORE_NAME,
'connectToJetpack', 'connectToJetpack',
getAdminLink getAdminLink

View File

@ -1,7 +1,7 @@
/** /**
* External dependencies * External dependencies
*/ */
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch, select } from '@wordpress/data-controls';
import { controls } from '@wordpress/data'; import { controls } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url'; import { addQueryArgs } from '@wordpress/url';
@ -23,6 +23,9 @@ import {
} from './actions'; } from './actions';
import { PaypalOnboardingStatus, RecommendedTypes } from './types'; import { PaypalOnboardingStatus, RecommendedTypes } from './types';
// Can be removed in WP 5.9, wp.data is supported in >5.7.
const resolveSelect =
controls && controls.resolveSelect ? controls.resolveSelect : select;
type PluginGetResponse = { type PluginGetResponse = {
plugins: string[]; plugins: string[];
} & Response; } & Response;
@ -116,7 +119,7 @@ function* setOnboardingStatusWithOptions() {
merchant_id_production: string; merchant_id_production: string;
client_id_production: string; client_id_production: string;
client_secret_production: string; client_secret_production: string;
} = yield controls.resolveSelect( } = yield resolveSelect(
OPTIONS_STORE_NAME, OPTIONS_STORE_NAME,
'getOption', 'getOption',
'woocommerce-ppcp-settings' 'woocommerce-ppcp-settings'
@ -139,7 +142,7 @@ export function* getPaypalOnboardingStatus() {
const errorData: { const errorData: {
data?: { status: number }; data?: { status: number };
} = yield controls.resolveSelect( } = yield resolveSelect(
STORE_NAME, STORE_NAME,
'getPluginsError', 'getPluginsError',
'getPaypalOnboardingStatus' 'getPaypalOnboardingStatus'

View File

@ -3,7 +3,7 @@
*/ */
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch, select } from '@wordpress/data-controls';
import { controls } from '@wordpress/data'; import { controls } from '@wordpress/data';
import { concat } from 'lodash'; import { concat } from 'lodash';
@ -14,6 +14,10 @@ import { NAMESPACE } from '../constants';
import { STORE_NAME } from './constants'; import { STORE_NAME } from './constants';
import TYPES from './action-types'; import TYPES from './action-types';
// Can be removed in WP 5.9, wp.data is supported in >5.7.
const resolveSelect =
controls && controls.resolveSelect ? controls.resolveSelect : select;
export function updateSettingsForGroup( group, data, time = new Date() ) { export function updateSettingsForGroup( group, data, time = new Date() ) {
return { return {
type: TYPES.UPDATE_SETTINGS_FOR_GROUP, type: TYPES.UPDATE_SETTINGS_FOR_GROUP,
@ -59,11 +63,7 @@ export function* persistSettingsForGroup( group ) {
// first dispatch the is persisting action // first dispatch the is persisting action
yield setIsRequesting( group, true ); yield setIsRequesting( group, true );
// get all dirty keys with select control // get all dirty keys with select control
const dirtyKeys = yield controls.resolveSelect( const dirtyKeys = yield resolveSelect( STORE_NAME, 'getDirtyKeys', group );
STORE_NAME,
'getDirtyKeys',
group
);
// if there is nothing dirty, bail // if there is nothing dirty, bail
if ( dirtyKeys.length === 0 ) { if ( dirtyKeys.length === 0 ) {
yield setIsRequesting( group, false ); yield setIsRequesting( group, false );
@ -71,7 +71,7 @@ export function* persistSettingsForGroup( group ) {
} }
// get data slice for keys // get data slice for keys
const dirtyData = yield controls.resolveSelect( const dirtyData = yield resolveSelect(
STORE_NAME, STORE_NAME,
'getSettingsForGroup', 'getSettingsForGroup',
group, group,

View File

@ -1,7 +1,10 @@
/** /**
* External dependencies * External dependencies
*/ */
import { apiFetch } from '@wordpress/data-controls'; import {
apiFetch,
dispatch as depreciatedDispatch,
} from '@wordpress/data-controls';
import { controls } from '@wordpress/data'; import { controls } from '@wordpress/data';
/** /**
@ -11,6 +14,10 @@ import { NAMESPACE } from '../constants';
import { STORE_NAME } from './constants'; import { STORE_NAME } from './constants';
import { updateSettingsForGroup, updateErrorForGroup } from './actions'; import { updateSettingsForGroup, updateErrorForGroup } from './actions';
// Can be removed in WP 5.9.
const dispatch =
controls && controls.dispatch ? controls.dispatch : depreciatedDispatch;
function settingsToSettingsResource( settings ) { function settingsToSettingsResource( settings ) {
return settings.reduce( ( resource, setting ) => { return settings.reduce( ( resource, setting ) => {
resource[ setting.id ] = setting.value; resource[ setting.id ] = setting.value;
@ -19,7 +26,7 @@ function settingsToSettingsResource( settings ) {
} }
export function* getSettings( group ) { export function* getSettings( group ) {
yield controls.dispatch( STORE_NAME, 'setIsRequesting', group, true ); yield dispatch( STORE_NAME, 'setIsRequesting', group, true );
try { try {
const url = NAMESPACE + '/settings/' + group; const url = NAMESPACE + '/settings/' + group;