Refactor the Change buttons and the Watermark

This commit is contained in:
Daniel Dudzic 2024-09-06 11:33:01 +02:00
parent c2831c2ab0
commit 522b27aea0
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
7 changed files with 207 additions and 147 deletions

View file

@ -0,0 +1,77 @@
import { createElement, useEffect, createRoot } from '@wordpress/element';
const ShippingChangeButton = ( { onChangeShippingAddressClick } ) =>
createElement(
'button',
{
className: 'wc-block-checkout-axo-block-card__edit',
'aria-label': 'Change shipping details',
type: 'button',
onClick: ( event ) => {
event.preventDefault();
onChangeShippingAddressClick();
},
},
'Change'
);
const ShippingChangeButtonManager = ( { onChangeShippingAddressClick } ) => {
useEffect( () => {
const shippingTitle = document.querySelector(
'#shipping-fields h2.wc-block-components-title'
);
if ( shippingTitle ) {
if (
! shippingTitle.nextElementSibling?.classList?.contains(
'wc-block-checkout-axo-block-card__edit'
)
) {
const buttonContainer = document.createElement( 'span' );
shippingTitle.parentNode.insertBefore(
buttonContainer,
shippingTitle.nextSibling
);
const root = createRoot( buttonContainer );
root.render(
createElement( ShippingChangeButton, {
onChangeShippingAddressClick,
} )
);
}
}
return () => {
const button = document.querySelector(
'#shipping-fields .wc-block-checkout-axo-block-card__edit'
);
if ( button && button.parentNode ) {
button.parentNode.remove();
}
};
}, [ onChangeShippingAddressClick ] );
return null;
};
export const injectShippingChangeButton = ( onChangeShippingAddressClick ) => {
const container = document.createElement( 'div' );
document.body.appendChild( container );
createRoot( container ).render(
createElement( ShippingChangeButtonManager, {
onChangeShippingAddressClick,
} )
);
};
export const removeShippingChangeButton = () => {
const button = document.querySelector(
'#shipping-fields .wc-block-checkout-axo-block-card__edit'
);
if ( button && button.parentNode ) {
button.parentNode.remove();
}
};
export default ShippingChangeButtonManager;