Add Submit button

This commit is contained in:
Daniel Dudzic 2024-09-10 18:46:27 +02:00
parent bccfe4c436
commit e3996b1f6e
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
5 changed files with 341 additions and 36 deletions

View file

@ -0,0 +1,102 @@
import { createElement, createRoot } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { STORE_NAME } from '../stores/axoStore';
const EmailSubmitButton = ( { onEmailSubmit } ) => {
const { isGuest, isAxoActive } = useSelect( ( select ) => ( {
isGuest: select( STORE_NAME ).getIsGuest(),
isAxoActive: select( STORE_NAME ).getIsAxoActive(),
} ) );
const handleSubmit = () => {
const emailInput = document.getElementById( 'email' );
if ( emailInput && emailInput.value ) {
onEmailSubmit( emailInput.value );
}
};
if ( ! isGuest || ! isAxoActive ) {
return null;
}
return (
<button
type="button"
onClick={ handleSubmit }
className="wc-block-components-button wp-element-button"
>
Submit
</button>
);
};
// Setup and removal functions
let submitButtonReference = {
container: null,
root: null,
};
export const setupEmailSubmitButton = ( onEmailSubmit ) => {
const emailInput = document.getElementById( 'email' );
if ( emailInput ) {
if ( ! submitButtonReference.container ) {
submitButtonReference.container = document.createElement( 'div' );
submitButtonReference.container.setAttribute(
'class',
'wc-block-axo-email-submit-button-container'
);
emailInput.parentNode.insertBefore(
submitButtonReference.container,
emailInput.nextSibling
);
submitButtonReference.root = createRoot(
submitButtonReference.container
);
}
if ( submitButtonReference.root ) {
const renderButton = () => {
submitButtonReference.root.render(
createElement( EmailSubmitButton, { onEmailSubmit } )
);
};
renderButton(); // Initial render
// Subscribe to state changes
const unsubscribe = wp.data.subscribe( () => {
renderButton();
} );
// Store the unsubscribe function for cleanup
submitButtonReference.unsubscribe = unsubscribe;
} else {
console.warn( 'Submit button root not found' );
}
} else {
console.warn( 'Email input not found' );
}
};
export const removeEmailSubmitButton = () => {
if ( submitButtonReference.root ) {
submitButtonReference.root.unmount();
}
if ( submitButtonReference.unsubscribe ) {
submitButtonReference.unsubscribe();
}
if (
submitButtonReference.container &&
submitButtonReference.container.parentNode
) {
submitButtonReference.container.parentNode.removeChild(
submitButtonReference.container
);
}
submitButtonReference = { container: null, root: null, unsubscribe: null };
};
export default EmailSubmitButton;