Skip to content

Commit

Permalink
Settings UI: Add Settings tab Redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldudzic committed Jan 17, 2025
1 parent d1a6740 commit 6993038
Show file tree
Hide file tree
Showing 17 changed files with 811 additions and 50 deletions.
36 changes: 18 additions & 18 deletions modules/ppcp-onboarding/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer;

return array(
'api.sandbox-host' => static function ( ContainerInterface $container ): string {
'api.sandbox-host' => static function ( ContainerInterface $container ): string {

$state = $container->get( 'onboarding.state' );

Expand All @@ -36,7 +36,7 @@
}
return CONNECT_WOO_SANDBOX_URL;
},
'api.production-host' => static function ( ContainerInterface $container ): string {
'api.production-host' => static function ( ContainerInterface $container ): string {

$state = $container->get( 'onboarding.state' );

Expand All @@ -51,7 +51,7 @@
}
return CONNECT_WOO_URL;
},
'api.host' => static function ( ContainerInterface $container ): string {
'api.host' => static function ( ContainerInterface $container ): string {
$environment = $container->get( 'onboarding.environment' );

/**
Expand All @@ -63,7 +63,7 @@
? (string) $container->get( 'api.sandbox-host' ) : (string) $container->get( 'api.production-host' );

},
'api.paypal-host' => function( ContainerInterface $container ) : string {
'api.paypal-host' => function( ContainerInterface $container ) : string {
$environment = $container->get( 'onboarding.environment' );
/**
* The current environment.
Expand All @@ -76,7 +76,7 @@
return $container->get( 'api.paypal-host-production' );

},
'api.paypal-website-url' => function( ContainerInterface $container ) : string {
'api.paypal-website-url' => function( ContainerInterface $container ) : string {
$environment = $container->get( 'onboarding.environment' );
assert( $environment instanceof Environment );
if ( $environment->current_environment_is( Environment::SANDBOX ) ) {
Expand All @@ -86,7 +86,7 @@

},

'api.bearer' => static function ( ContainerInterface $container ): Bearer {
'api.bearer' => static function ( ContainerInterface $container ): Bearer {

$state = $container->get( 'onboarding.state' );

Expand All @@ -113,16 +113,16 @@
$settings
);
},
'onboarding.state' => function( ContainerInterface $container ) : State {
'onboarding.state' => function( ContainerInterface $container ) : State {
$settings = $container->get( 'wcgateway.settings' );
return new State( $settings );
},
'onboarding.environment' => function( ContainerInterface $container ) : Environment {
'onboarding.environment' => function( ContainerInterface $container ) : Environment {
$settings = $container->get( 'wcgateway.settings' );
return new Environment( $settings );
},

'onboarding.assets' => function( ContainerInterface $container ) : OnboardingAssets {
'onboarding.assets' => function( ContainerInterface $container ) : OnboardingAssets {
$state = $container->get( 'onboarding.state' );
$login_seller_endpoint = $container->get( 'onboarding.endpoint.login-seller' );
return new OnboardingAssets(
Expand All @@ -135,14 +135,14 @@
);
},

'onboarding.url' => static function ( ContainerInterface $container ): string {
'onboarding.url' => static function ( ContainerInterface $container ): string {
return plugins_url(
'/modules/ppcp-onboarding/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},

'onboarding.endpoint.login-seller' => static function ( ContainerInterface $container ) : LoginSellerEndpoint {
'onboarding.endpoint.login-seller' => static function ( ContainerInterface $container ) : LoginSellerEndpoint {

$request_data = $container->get( 'button.request-data' );
$login_seller_production = $container->get( 'api.endpoint.login-seller-production' );
Expand All @@ -162,7 +162,7 @@
new Cache( 'ppcp-client-credentials-cache' )
);
},
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint {
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint {
return new UpdateSignupLinksEndpoint(
$container->get( 'wcgateway.settings' ),
$container->get( 'button.request-data' ),
Expand All @@ -172,23 +172,23 @@
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'onboarding.signup-link-cache' => static function( ContainerInterface $container ): Cache {
'onboarding.signup-link-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-signup-link' );
},
'onboarding.signup-link-ids' => static function ( ContainerInterface $container ): array {
'onboarding.signup-link-ids' => static function ( ContainerInterface $container ): array {
return array(
'production-ppcp',
'production-express_checkout',
'sandbox-ppcp',
'sandbox-express_checkout',
);
},
'onboarding.render-send-only-notice' => static function( ContainerInterface $container ) {
'onboarding.render-send-only-notice' => static function( ContainerInterface $container ) {
return new OnboardingSendOnlyNoticeRenderer(
$container->get( 'wcgateway.send-only-message' )
);
},
'onboarding.render' => static function ( ContainerInterface $container ) : OnboardingRenderer {
'onboarding.render' => static function ( ContainerInterface $container ) : OnboardingRenderer {
$partner_referrals = $container->get( 'api.endpoint.partner-referrals-production' );
$partner_referrals_sandbox = $container->get( 'api.endpoint.partner-referrals-sandbox' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
Expand All @@ -204,14 +204,14 @@
$logger
);
},
'onboarding.render-options' => static function ( ContainerInterface $container ) : OnboardingOptionsRenderer {
'onboarding.render-options' => static function ( ContainerInterface $container ) : OnboardingOptionsRenderer {
return new OnboardingOptionsRenderer(
$container->get( 'onboarding.url' ),
$container->get( 'api.shop.country' ),
$container->get( 'wcgateway.settings' )
);
},
'onboarding.rest' => static function( $container ) : OnboardingRESTController {
'onboarding.rest' => static function( $container ) : OnboardingRESTController {
return new OnboardingRESTController( $container );
},
);
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
import { useState } from '@wordpress/element';
import ConnectionStatus from './TabSettingsElements/ConnectionStatus';
import CommonSettings from './TabSettingsElements/CommonSettings';
import ExpertSettings from './TabSettingsElements/ExpertSettings';
import { useSettings } from '../../../data/settings-tab/hooks';

const TabSettings = () => {
const [ settings, setSettings ] = useState( {
invoicePrefix: '',
authorizeOnly: false,
captureVirtualOnlyOrders: false,
savePaypalAndVenmo: false,
saveCreditCardAndDebitCard: false,
payNowExperience: false,
sandboxAccountCredentials: false,
sandboxMode: null,
sandboxEnabled: false,
sandboxClientId: '',
sandboxSecretKey: '',
sandboxConnected: false,
logging: false,
subtotalMismatchFallback: null,
brandName: '',
softDescriptor: '',
paypalLandingPage: null,
buttonLanguage: '',
} );
const { settings, setSettings } = useSettings();

const updateFormValue = ( key, value ) => {
setSettings( { ...settings, [ key ]: value } );
setSettings( {
...settings,
[ key ]: value,
} );
};

return (
Expand Down
15 changes: 13 additions & 2 deletions modules/ppcp-settings/resources/js/Components/Screens/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import SpinnerOverlay from '../ReusableComponents/SpinnerOverlay';
import Onboarding from './Onboarding/Onboarding';
import SettingsScreen from './SettingsScreen';
import { useMerchantInfo } from '../../data/common/hooks';
import { initStore as initSettingsStore } from '../../data/settings-tab';
import { useSettingsState } from '../../data/settings-tab/hooks';
import SendOnlyMessage from './SendOnlyMessage';

// Initialize the settings store
initSettingsStore();

const Settings = () => {
const onboardingProgress = OnboardingHooks.useSteps();
const { isReady: settingsIsReady } = useSettingsState();
const {
isReady: merchantIsReady,
merchant: { isSendOnlyCountry },
Expand All @@ -32,11 +38,15 @@ const Settings = () => {
}, [] );

const wrapperClass = classNames( 'ppcp-r-app', {
loading: ! onboardingProgress.isReady,
loading: ! onboardingProgress.isReady || ! settingsIsReady,
} );

const Content = useMemo( () => {
if ( ! onboardingProgress.isReady || ! merchantIsReady ) {
if (
! onboardingProgress.isReady ||
! merchantIsReady ||
! settingsIsReady
) {
return (
<SpinnerOverlay
message={ __( 'Loading…', 'woocommerce-paypal-payments' ) }
Expand All @@ -58,6 +68,7 @@ const Settings = () => {
merchantIsReady,
onboardingProgress.completed,
onboardingProgress.isReady,
settingsIsReady,
] );

return <div className={ wrapperClass }>{ Content }</div>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Settings action types
*
* Defines the constants used for dispatching actions in the settings store.
* Each constant represents a unique action type that can be handled by reducers.
*
* @file
*/

export default {
/**
* Represents setting transient (temporary) state data.
* These values are not persisted and will reset on page reload.
*/
SET_TRANSIENT: 'ppcp/settings/SET_TRANSIENT',

/**
* Represents setting persistent state data.
* These values are meant to be saved to the server and persist between page loads.
*/
SET_PERSISTENT: 'ppcp/settings/SET_PERSISTENT',

/**
* Resets the store state to its initial values.
* Used when needing to clear all settings data.
*/
RESET: 'ppcp/settings/RESET',

/**
* Initializes the store with data, typically used during store initialization
* to set up the initial state with data from the server.
*/
HYDRATE: 'ppcp/settings/HYDRATE',

/**
* Triggers the persistence of store data to the server.
* Used when changes need to be saved to the backend.
*/
DO_PERSIST_DATA: 'ppcp/settings/DO_PERSIST_DATA',
};
71 changes: 71 additions & 0 deletions modules/ppcp-settings/resources/js/data/settings-tab/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Action Creators: Define functions to create action objects.
*
* These functions update state or trigger side effects (e.g., async operations).
* Actions are categorized as Transient, Persistent, or Side effect.
*
* @file
*/

import { select } from '@wordpress/data';
import ACTION_TYPES from './action-types';
import { STORE_NAME } from './constants';

/**
* @typedef {Object} Action An action object that is handled by a reducer or control.
* @property {string} type - The action type.
* @property {Object?} payload - Optional payload for the action.
*/

/**
* Special. Resets all values in the store to initial defaults.
*
* @return {Action} The action.
*/
export const reset = () => ( {
type: ACTION_TYPES.RESET,
} );

/**
* Persistent. Sets the full store details during app initialization.
*
* @param {Object} payload Initial store data
* @return {Action} The action.
*/
export const hydrate = ( payload ) => ( {
type: ACTION_TYPES.HYDRATE,
payload,
} );

/**
* Transient. Marks the store as "ready", i.e., fully initialized.
*
* @param {boolean} isReady Whether the store is ready
* @return {Action} The action.
*/
export const setIsReady = ( isReady ) => ( {
type: ACTION_TYPES.SET_TRANSIENT,
payload: { isReady },
} );

/**
* Persistent. Updates the settings data in the store.
*
* @param {Object} settings The settings object to store
* @return {Action} The action.
*/
export const setSettings = ( settings ) => ( {
type: ACTION_TYPES.SET_PERSISTENT,
payload: settings,
} );

/**
* Side effect. Triggers the persistence of store data to the server.
* Yields an action with the current persistent data to be saved.
*
* @return {Action} The action.
*/
export const persist = function* () {
const data = yield select( STORE_NAME ).persistentData();
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
};
28 changes: 28 additions & 0 deletions modules/ppcp-settings/resources/js/data/settings-tab/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Name of the Redux store module.
*
* Used by: Reducer, Selector, Index
*
* @type {string}
*/
export const STORE_NAME = 'wc/paypal/settings';

/**
* REST path to hydrate data of this module by loading data from the WP DB.
*
* Used by: Resolvers
* See: SettingsRestEndpoint.php
*
* @type {string}
*/
export const REST_HYDRATE_PATH = '/wc/v3/wc_paypal/settings';

/**
* REST path to persist data of this module to the WP DB.
*
* Used by: Controls
* See: SettingsRestEndpoint.php
*
* @type {string}
*/
export const REST_PERSIST_PATH = '/wc/v3/wc_paypal/settings';
Loading

0 comments on commit 6993038

Please sign in to comment.