Skip to content

Commit

Permalink
MailChimp Block: refactor Edit component to function (#36746)
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieur-z authored and TimBroddin committed Apr 10, 2024
1 parent 46fd90e commit 3253fc8
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 240 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

MailChimp block: refactor Edit component to function
79 changes: 79 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/mailchimp/body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { InnerBlocks, RichText } from '@wordpress/block-editor';
import { TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import {
BLOCK_CLASS,
NOTIFICATION_ERROR,
NOTIFICATION_PROCESSING,
NOTIFICATION_SUCCESS,
DEFAULT_EMAIL_PLACEHOLDER,
DEFAULT_CONSENT_TEXT,
DEFAULT_PROCESSING_LABEL,
DEFAULT_SUCCESS_LABEL,
DEFAULT_ERROR_LABEL,
} from './constants';

const innerButtonBlock = {
name: 'jetpack/button',
attributes: {
element: 'button',
text: __( 'Join my Mailchimp audience', 'jetpack' ),
uniqueId: 'mailchimp-widget-id',
},
};

const Body = ( { attributes, setAttributes, className, audition } ) => {
const {
emailPlaceholder = DEFAULT_EMAIL_PLACEHOLDER,
consentText = DEFAULT_CONSENT_TEXT,
processingLabel = DEFAULT_PROCESSING_LABEL,
successLabel = DEFAULT_SUCCESS_LABEL,
errorLabel = DEFAULT_ERROR_LABEL,
} = attributes;

const notification = {
[ NOTIFICATION_PROCESSING ]: processingLabel,
[ NOTIFICATION_SUCCESS ]: successLabel,
[ NOTIFICATION_ERROR ]: errorLabel,
}[ audition ];

return (
<div
className={ classnames( className, {
[ `${ BLOCK_CLASS }_notication-audition` ]: audition,
} ) }
>
<TextControl
aria-label={ emailPlaceholder }
className={ `${ BLOCK_CLASS }_text-input` }
disabled
onChange={ () => false }
placeholder={ emailPlaceholder }
title={ __( 'You can edit the email placeholder in the sidebar.', 'jetpack' ) }
type="email"
/>
<InnerBlocks
template={ [ [ innerButtonBlock.name, innerButtonBlock.attributes ] ] }
templateLock="all"
/>
<RichText
tagName="p"
placeholder={ __( 'Write consent text', 'jetpack' ) }
value={ consentText }
onChange={ value => setAttributes( { consentText: value } ) }
inlineToolbar
/>
{ audition && (
<div
className={ `${ BLOCK_CLASS }_notification ${ BLOCK_CLASS }_${ audition }` }
role={ audition === NOTIFICATION_ERROR ? 'alert' : 'status' }
>
{ notification }
</div>
) }
</div>
);
};

export default Body;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export const NOTIFICATION_PROCESSING = 'processing';
export const NOTIFICATION_SUCCESS = 'success';
export const NOTIFICATION_ERROR = 'error';

export const BLOCK_CLASS = 'wp-block-jetpack-mailchimp';

export const API_STATE_LOADING = 0;
export const API_STATE_CONNECTED = 1;
export const API_STATE_NOTCONNECTED = 2;

export const DEFAULT_EMAIL_PLACEHOLDER = __( 'Enter your email', 'jetpack' );
export const DEFAULT_CONSENT_TEXT = __(
'By clicking submit, you agree to share your email address with the site owner and Mailchimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.',
Expand Down
55 changes: 52 additions & 3 deletions projects/plugins/jetpack/extensions/blocks/mailchimp/controls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InspectorControls } from '@wordpress/block-editor';
import { ExternalLink, PanelBody, TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useRef } from 'react';
import {
NOTIFICATION_PROCESSING,
NOTIFICATION_SUCCESS,
Expand All @@ -11,7 +13,7 @@ import {
} from './constants';
import MailchimpGroups from './mailchimp-groups';

export function MailChimpBlockControls( {
export const MailChimpBlockControls = ( {
auditionNotification,
clearAudition,
setAttributes,
Expand All @@ -23,7 +25,7 @@ export function MailChimpBlockControls( {
signupFieldTag,
signupFieldValue,
connectURL,
} ) {
} ) => {
const updateProcessingText = processing => {
setAttributes( { processingLabel: processing } );
auditionNotification( NOTIFICATION_PROCESSING );
Expand Down Expand Up @@ -111,4 +113,51 @@ export function MailChimpBlockControls( {
</PanelBody>
</>
);
}
};

export const MailChimpInspectorControls = ( {
connectURL,
attributes,
setAttributes,
setAudition,
} ) => {
const {
emailPlaceholder = DEFAULT_EMAIL_PLACEHOLDER,
processingLabel = DEFAULT_PROCESSING_LABEL,
successLabel = DEFAULT_SUCCESS_LABEL,
errorLabel = DEFAULT_ERROR_LABEL,
interests,
signupFieldTag,
signupFieldValue,
} = attributes;

const timeout = useRef( null );

const clearAudition = () => setAudition( null );
const auditionNotification = notification => {
setAudition( notification );

if ( timeout.current ) {
clearTimeout( timeout.current );
}
timeout.current = setTimeout( clearAudition, 3000 );
};

return (
<InspectorControls>
<MailChimpBlockControls
auditionNotification={ auditionNotification }
clearAudition={ clearAudition }
emailPlaceholder={ emailPlaceholder }
processingLabel={ processingLabel }
successLabel={ successLabel }
errorLabel={ errorLabel }
interests={ interests }
setAttributes={ setAttributes }
signupFieldTag={ signupFieldTag }
signupFieldValue={ signupFieldValue }
connectURL={ connectURL }
/>
</InspectorControls>
);
};
Loading

0 comments on commit 3253fc8

Please sign in to comment.