Merge pull request #2624 from woocommerce/add-support-for-advanced-style-settings

Axo Blocks: Add support for Advanced Style Settings
This commit is contained in:
Danny Dudzic 2024-09-25 15:39:16 +02:00 committed by GitHub
commit 09341f8475
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 3 deletions

View file

@ -0,0 +1,27 @@
import { useCallback } from '@wordpress/element';
const isObject = ( value ) => typeof value === 'object' && value !== null;
const isNonEmptyString = ( value ) => value !== '';
const removeEmptyValues = ( obj ) => {
if ( ! isObject( obj ) ) {
return obj;
}
return Object.fromEntries(
Object.entries( obj )
.map( ( [ key, value ] ) => [
key,
isObject( value ) ? removeEmptyValues( value ) : value,
] )
.filter( ( [ _, value ] ) =>
isObject( value )
? Object.keys( value ).length > 0
: isNonEmptyString( value )
)
);
};
export const useDeleteEmptyKeys = () => {
return useCallback( removeEmptyValues, [] );
};

View file

@ -1,11 +1,17 @@
import { useEffect, useRef, useState } from '@wordpress/element'; import { useEffect, useRef, useState, useMemo } from '@wordpress/element';
import Fastlane from '../../../../ppcp-axo/resources/js/Connection/Fastlane'; import Fastlane from '../../../../ppcp-axo/resources/js/Connection/Fastlane';
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug'; import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
import { useDeleteEmptyKeys } from './useDeleteEmptyKeys';
const useFastlaneSdk = ( axoConfig, ppcpConfig ) => { const useFastlaneSdk = ( axoConfig, ppcpConfig ) => {
const [ fastlaneSdk, setFastlaneSdk ] = useState( null ); const [ fastlaneSdk, setFastlaneSdk ] = useState( null );
const initializingRef = useRef( false ); const initializingRef = useRef( false );
const configRef = useRef( { axoConfig, ppcpConfig } ); const configRef = useRef( { axoConfig, ppcpConfig } );
const deleteEmptyKeys = useDeleteEmptyKeys();
const styleOptions = useMemo( () => {
return deleteEmptyKeys( configRef.current.axoConfig.style_options );
}, [ deleteEmptyKeys ] );
useEffect( () => { useEffect( () => {
const initFastlane = async () => { const initFastlane = async () => {
@ -25,7 +31,7 @@ const useFastlaneSdk = ( axoConfig, ppcpConfig ) => {
await fastlane.connect( { await fastlane.connect( {
locale: configRef.current.ppcpConfig.locale, locale: configRef.current.ppcpConfig.locale,
styles: configRef.current.ppcpConfig.styles, styles: styleOptions,
} ); } );
fastlane.setLocale( 'en_us' ); fastlane.setLocale( 'en_us' );
@ -39,7 +45,7 @@ const useFastlaneSdk = ( axoConfig, ppcpConfig ) => {
}; };
initFastlane(); initFastlane();
}, [] ); }, [ fastlaneSdk, styleOptions ] );
useEffect( () => { useEffect( () => {
configRef.current = { axoConfig, ppcpConfig }; configRef.current = { axoConfig, ppcpConfig };