Save pay later messaging config to database

This commit is contained in:
Emili Castells Guasch 2025-01-30 16:39:26 +01:00
parent a652f21629
commit 66c6fb17b8
6 changed files with 70 additions and 37 deletions

View file

@ -2,7 +2,15 @@ import React, { useEffect } from 'react';
import { PayLaterMessagingHooks } from '../../../data'; import { PayLaterMessagingHooks } from '../../../data';
const TabPayLaterMessaging = () => { const TabPayLaterMessaging = () => {
const { config } = PayLaterMessagingHooks.usePayLaterMessaging(); const {
config,
setCart,
setCheckout,
setProduct,
setShop,
setHome,
setCustom_placement,
} = PayLaterMessagingHooks.usePayLaterMessaging();
const PcpPayLaterConfigurator = const PcpPayLaterConfigurator =
window.ppcpSettings?.PcpPayLaterConfigurator; window.ppcpSettings?.PcpPayLaterConfigurator;
@ -28,13 +36,12 @@ const TabPayLaterMessaging = () => {
subheader: 'ppcp-r-paylater-configurator__subheader', subheader: 'ppcp-r-paylater-configurator__subheader',
}, },
onSave: ( data ) => { onSave: ( data ) => {
/* setCart( data.config.cart );
TODO: setCheckout( data.config.checkout );
- The saving will be handled in a separate PR. setProduct( data.config.product );
- One option could be: setShop( data.config.shop );
- When saving the settings, programmatically click on the configurator's setHome( data.config.home );
"Save Changes" button and send the request to PHP. setCustom_placement( data.config.custom_placement );
*/
}, },
} ); } );
} }

View file

@ -34,7 +34,7 @@ const useHooks = () => {
}; };
}; };
export const useState = () => { export const useStore = () => {
const { persist, isReady } = useHooks(); const { persist, isReady } = useHooks();
return { persist, isReady }; return { persist, isReady };
}; };

View file

@ -23,47 +23,52 @@ const useHooks = () => {
const [ isReady ] = useTransient( 'isReady' ); const [ isReady ] = useTransient( 'isReady' );
// Persistent accessors. // Persistent accessors.
// TODO: Replace with real property. const [ cart, setCart ] = usePersistent( 'cart' );
const [ sampleValue, setSampleValue ] = usePersistent( 'sampleValue' ); const [ checkout, setCheckout ] = usePersistent( 'checkout' );
const [ product, setProduct ] = usePersistent( 'product' );
const [ cart ] = usePersistent( 'cart' ); const [ shop, setShop ] = usePersistent( 'shop' );
const [ checkout ] = usePersistent( 'checkout' ); const [ home, setHome ] = usePersistent( 'home' );
const [ product ] = usePersistent( 'product' ); const [ custom_placement, setCustom_placement ] =
const [ shop ] = usePersistent( 'shop' ); usePersistent( 'custom_placement' );
const [ home ] = usePersistent( 'home' );
const [ custom_placement ] = usePersistent( 'custom_placement' );
return { return {
persist, persist,
isReady, isReady,
sampleValue,
setSampleValue,
cart, cart,
setCart,
checkout, checkout,
setCheckout,
product, product,
setProduct,
shop, shop,
setShop,
home, home,
setHome,
custom_placement, custom_placement,
setCustom_placement,
}; };
}; };
export const useState = () => { export const useStore = () => {
const { persist, isReady } = useHooks(); const { persist, isReady } = useHooks();
return { persist, isReady }; return { persist, isReady };
}; };
// TODO: Replace with real hook.
export const useSampleValue = () => {
const { sampleValue, setSampleValue } = useHooks();
return {
sampleValue,
setSampleValue,
};
};
export const usePayLaterMessaging = () => { export const usePayLaterMessaging = () => {
const { cart, checkout, product, shop, home, customPlacement } = useHooks(); const {
cart,
setCart,
checkout,
setCheckout,
product,
setProduct,
shop,
setShop,
home,
setHome,
custom_placement,
setCustom_placement,
} = useHooks();
return { return {
config: { config: {
@ -72,7 +77,13 @@ export const usePayLaterMessaging = () => {
product, product,
shop, shop,
home, home,
customPlacement, custom_placement,
}, },
setCart,
setCheckout,
setProduct,
setShop,
setHome,
setCustom_placement,
}; };
}; };

View file

@ -19,8 +19,6 @@ const defaultTransient = Object.freeze( {
// Persistent: Values that are loaded from the DB. // Persistent: Values that are loaded from the DB.
const defaultPersistent = Object.freeze( { const defaultPersistent = Object.freeze( {
// TODO: Add real DB properties here.
sampleValue: 'foo',
cart: {}, cart: {},
checkout: {}, checkout: {},
product: {}, product: {},

View file

@ -2,6 +2,7 @@ import { useCallback } from '@wordpress/element';
import { import {
CommonHooks, CommonHooks,
PayLaterMessagingHooks,
PaymentHooks, PaymentHooks,
SettingsHooks, SettingsHooks,
StylingHooks, StylingHooks,
@ -13,8 +14,13 @@ export const useSaveSettings = () => {
const { persist: persistPayment } = PaymentHooks.useStore(); const { persist: persistPayment } = PaymentHooks.useStore();
const { persist: persistSettings } = SettingsHooks.useStore(); const { persist: persistSettings } = SettingsHooks.useStore();
const { persist: persistStyling } = StylingHooks.useStore(); const { persist: persistStyling } = StylingHooks.useStore();
const { persist: persistPayLaterMessaging } =
PayLaterMessagingHooks.useStore();
const persistAll = useCallback( () => { const persistAll = useCallback( () => {
// Executes onSave on TabPayLaterMessaging component.
document.getElementById( 'configurator-publishButton' )?.click();
withActivity( withActivity(
'persist-methods', 'persist-methods',
'Save payment methods', 'Save payment methods',
@ -30,7 +36,18 @@ export const useSaveSettings = () => {
'Save styling details', 'Save styling details',
persistStyling persistStyling
); );
}, [ persistPayment, persistSettings, persistStyling, withActivity ] ); withActivity(
'persist-pay-later-messaging',
'Save pay later messaging details',
persistPayLaterMessaging
);
}, [
persistPayment,
persistSettings,
persistStyling,
persistPayLaterMessaging,
withActivity,
] );
return { persistAll }; return { persistAll };
}; };

View file

@ -103,7 +103,7 @@ class PayLaterMessagingEndpoint extends RestEndpoint {
* @return WP_REST_Response The updated Pay Later Messaging configuration details. * @return WP_REST_Response The updated Pay Later Messaging configuration details.
*/ */
public function update_details( WP_REST_Request $request ) : WP_REST_Response { public function update_details( WP_REST_Request $request ) : WP_REST_Response {
$this->save_config->save_config( $request->get_params() ); $this->save_config->save_config( $request->get_json_params() );
return $this->get_details(); return $this->get_details();
} }