Skip to content

Commit

Permalink
Move EmailSubmitButton to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldudzic committed Sep 12, 2024
1 parent 26fe369 commit fb0e093
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { STORE_NAME } from '../stores/axoStore';
import { useSelect } from '@wordpress/data';

export const EmailSubmitButton = ( { handleSubmit } ) => {
const { isGuest, isAxoActive, isEmailSubmitted } = useSelect(
( select ) => ( {
isGuest: select( STORE_NAME ).getIsGuest(),
isAxoActive: select( STORE_NAME ).getIsAxoActive(),
isEmailSubmitted: select( STORE_NAME ).isEmailSubmitted(),
} )
);

if ( ! isGuest || ! isAxoActive ) {
return null;
}

return (
<button
type="button"
onClick={ handleSubmit }
className={ `wc-block-components-button wp-element-button ${
isEmailSubmitted ? 'is-loading' : ''
}` }
disabled={ isEmailSubmitted }
>
<span
className="wc-block-components-button__text"
style={ {
visibility: isEmailSubmitted ? 'hidden' : 'visible',
} }
>
Submit
</span>
{ isEmailSubmitted && (
<span
className="wc-block-components-spinner"
aria-hidden="true"
style={ {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
} }
/>
) }
</button>
);
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// EmailSubmissionManager.js

import { createElement, createRoot } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { STORE_NAME } from '../stores/axoStore';
import { EmailSubmitButton } from '../components/EmailSubmitButton';

let emailInput = null;
let submitButtonReference = {
container: null,
root: null,
unsubscribe: null,
};
let isLoading = false;
let keydownHandler = null;

const getEmailInput = () => {
Expand All @@ -20,47 +17,6 @@ const getEmailInput = () => {
return emailInput;
};

const EmailSubmitButton = ( { handleSubmit } ) => {
const { isGuest, isAxoActive } = useSelect( ( select ) => ( {
isGuest: select( STORE_NAME ).getIsGuest(),
isAxoActive: select( STORE_NAME ).getIsAxoActive(),
} ) );

if ( ! isGuest || ! isAxoActive ) {
return null;
}

return (
<button
type="button"
onClick={ handleSubmit }
className={ `wc-block-components-button wp-element-button ${
isLoading ? 'is-loading' : ''
}` }
disabled={ isLoading }
>
<span
className="wc-block-components-button__text"
style={ { visibility: isLoading ? 'hidden' : 'visible' } }
>
Submit
</span>
{ isLoading && (
<span
className="wc-block-components-spinner"
aria-hidden="true"
style={ {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
} }
/>
) }
</button>
);
};

export const setupEmailFunctionality = ( onEmailSubmit ) => {
const input = getEmailInput();
if ( ! input ) {
Expand All @@ -71,20 +27,24 @@ export const setupEmailFunctionality = ( onEmailSubmit ) => {
}

const handleEmailSubmit = async () => {
if ( isLoading || ! input.value ) {
const isEmailSubmitted = wp.data
.select( STORE_NAME )
.isEmailSubmitted();

if ( isEmailSubmitted || ! input.value ) {
return;
}

isLoading = true;
renderButton(); // Re-render button to show loading state
wp.data.dispatch( STORE_NAME ).setIsEmailSubmitted( true );
renderButton();

try {
await onEmailSubmit( input.value );
} catch ( error ) {
console.error( 'Error during email submission:', error );
// Here you might want to show an error message to the user
} finally {
isLoading = false;
wp.data.dispatch( STORE_NAME ).setIsEmailSubmitted( false );
renderButton(); // Re-render button to remove loading state
}
};
Expand Down
8 changes: 8 additions & 0 deletions modules/ppcp-axo-block/resources/js/stores/axoStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DEFAULT_STATE = {
isGuest: true,
isAxoActive: false,
isAxoScriptLoaded: false,
isEmailSubmitted: false,
};

// Actions
Expand All @@ -25,6 +26,10 @@ const actions = {
type: 'SET_IS_AXO_SCRIPT_LOADED',
payload: isAxoScriptLoaded,
} ),
setIsEmailSubmitted: ( isEmailSubmitted ) => ( {
type: 'SET_IS_EMAIL_SUBMITTED',
payload: isEmailSubmitted,
} ),
};

// Reducer
Expand All @@ -36,6 +41,8 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
return { ...state, isAxoActive: action.payload };
case 'SET_IS_AXO_SCRIPT_LOADED':
return { ...state, isAxoScriptLoaded: action.payload };
case 'SET_IS_EMAIL_SUBMITTED':
return { ...state, isEmailSubmitted: action.payload };
default:
return state;
}
Expand All @@ -46,6 +53,7 @@ const selectors = {
getIsGuest: ( state ) => state.isGuest,
getIsAxoActive: ( state ) => state.isAxoActive,
isAxoScriptLoaded: ( state ) => state.isAxoScriptLoaded,
isEmailSubmitted: ( state ) => state.isEmailSubmitted,
};

// Create and register the store
Expand Down

0 comments on commit fb0e093

Please sign in to comment.