Add merchant details to “common” hydration path

This commit is contained in:
Philipp Stracker 2024-12-06 19:07:44 +01:00
parent 0462905123
commit c97464d7e2
No known key found for this signature in database
4 changed files with 60 additions and 9 deletions

View file

@ -8,7 +8,7 @@
export const STORE_NAME = 'wc/paypal/common'; export const STORE_NAME = 'wc/paypal/common';
/** /**
* REST path to hydrate data of this module by loading data from the WP DB.. * REST path to hydrate data of this module by loading data from the WP DB.
* *
* Used by resolvers. * Used by resolvers.
* *

View file

@ -17,6 +17,13 @@ const defaultTransient = {
activities: new Map(), activities: new Map(),
// Read only values, provided by the server via hydrate. // Read only values, provided by the server via hydrate.
merchant: {
isConnected: false,
isSandbox: false,
id: '',
email: '',
},
wooSettings: { wooSettings: {
storeCountry: '', storeCountry: '',
storeCurrency: '', storeCurrency: '',
@ -75,12 +82,17 @@ const commonReducer = createReducer( defaultTransient, defaultPersistent, {
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => { [ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
const newState = setPersistent( state, payload.data ); const newState = setPersistent( state, payload.data );
if ( payload.wooSettings ) { // Populate read-only properties.
newState.wooSettings = { [ 'wooSettings', 'merchant' ].forEach( ( key ) => {
...newState.wooSettings, if ( ! payload[ key ] ) {
...payload.wooSettings, return;
}; }
}
newState[ key ] = Object.freeze( {
...newState[ key ],
...payload[ key ],
} );
} );
return newState; return newState;
}, },

View file

@ -16,7 +16,8 @@ export const persistentData = ( state ) => {
}; };
export const transientData = ( state ) => { export const transientData = ( state ) => {
const { data, wooSettings, ...transientState } = getState( state ); const { data, merchant, wooSettings, ...transientState } =
getState( state );
return transientState || EMPTY_OBJ; return transientState || EMPTY_OBJ;
}; };

View file

@ -61,7 +61,27 @@ class CommonRestEndpoint extends RestEndpoint {
); );
/** /**
* Map the internal flags to JS names. * Map merchant details to JS names.
*
* @var array
*/
private array $merchant_info_map = array(
'merchant_connected' => array(
'js_name' => 'isConnected',
),
'sandbox_merchant' => array(
'js_name' => 'isSandbox',
),
'merchant_id' => array(
'js_name' => 'id',
),
'merchant_email' => array(
'js_name' => 'email',
),
);
/**
* Map woo-settings to JS names.
* *
* @var array * @var array
*/ */
@ -124,6 +144,7 @@ class CommonRestEndpoint extends RestEndpoint {
); );
$extra_data = $this->add_woo_settings( array() ); $extra_data = $this->add_woo_settings( array() );
$extra_data = $this->add_merchant_info( $extra_data );
return $this->return_success( $js_data, $extra_data ); return $this->return_success( $js_data, $extra_data );
} }
@ -147,6 +168,23 @@ class CommonRestEndpoint extends RestEndpoint {
return $this->get_details(); return $this->get_details();
} }
/**
* Appends the "merchant" attribute to the extra_data collection, which
* contains details about the merchant's PayPal account, like the merchant ID.
*
* @param array $extra_data Initial extra_data collection.
*
* @return array Updated extra_data collection.
*/
protected function add_merchant_info( array $extra_data ) : array {
$extra_data['merchant'] = $this->sanitize_for_javascript(
$this->settings->to_array(),
$this->merchant_info_map
);
return $extra_data;
}
/** /**
* Appends the "wooSettings" attribute to the extra_data collection to * Appends the "wooSettings" attribute to the extra_data collection to
* provide WooCommerce store details, like the store country and currency. * provide WooCommerce store details, like the store country and currency.