Skip to content
249 changes: 156 additions & 93 deletions assets/blocks/reader-registration/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@ import { intersection } from 'lodash';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import { Spinner, Notice, TextControl, ToggleControl, PanelBody } from '@wordpress/components';
import { RichText, useBlockProps, InspectorControls } from '@wordpress/block-editor';
import {
useBlockProps,
RichText,
InspectorControls,
useInnerBlocksProps,
InnerBlocks,
} from '@wordpress/block-editor';
import {
Spinner,
Notice,
TextControl,
ToggleControl,
PanelBody,
Button,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import './editor.scss';

const editedStateOptions = [
{ label: __( 'Initial', 'newspack' ), value: 'initial' },
{ label: __( 'Success', 'newspack' ), value: 'success' },
];
export default function ReaderRegistrationEdit( {
setAttributes,
attributes: {
Expand All @@ -36,6 +53,28 @@ export default function ReaderRegistrationEdit( {
},
} ) {
const blockProps = useBlockProps();
const [ editedState, setEditedState ] = useState( editedStateOptions[ 0 ].value );
const isInitial = editedState === 'initial';

const innerBlocksProps = useInnerBlocksProps(
{},
{
renderAppender: InnerBlocks.ButtonBlockAppender,
template: [
// Quirk: this will only get applied to the block (as inner blocks) if it's *rendered* in the editor.
// If the user never switches the state view, it will not be applied, so PHP code contains a fallback.
[
'core/paragraph',
{
content: __(
'Thank you for registering! Check your email for a confirmation link.',
'newspack'
),
},
],
],
}
);
const [ inFlight, setInFlight ] = useState( false );
const [ listConfig, setListConfig ] = useState( {} );

Expand Down Expand Up @@ -133,110 +172,134 @@ export default function ReaderRegistrationEdit( {
) }
</InspectorControls>
<div { ...blockProps }>
<div className={ `newspack-registration ${ className }` }>
<form onSubmit={ ev => ev.preventDefault() }>
<div className="newspack-registration__form-content">
{ newsletterSubscription && lists.length ? (
<div className="newspack-registration__lists">
{ lists?.length > 1 && (
<RichText
onChange={ value => setAttributes( { newsletterTitle: value } ) }
placeholder={ __( 'Newsletters title…', 'newspack' ) }
value={ newsletterTitle }
tagName="h3"
/>
) }
<ul>
{ lists.map( listId => (
<li key={ listId }>
<span className="newspack-registration__lists__checkbox">
<input type="checkbox" checked readOnly />
</span>
<span className="newspack-registration__lists__details">
<span className="newspack-registration__lists__label">
<span className="newspack-registration__lists__title">
{ lists.length === 1 ? (
<RichText
onChange={ value => setAttributes( { newsletterLabel: value } ) }
placeholder={ __( 'Subscribe to our newsletter', 'newspack' ) }
value={ newsletterLabel }
tagName="span"
/>
) : (
listConfig[ listId ]?.title
<div className="newspack-registration__state-bar">
<span>{ __( 'Edited State', 'newspack' ) }</span>
<div>
<Button
data-is-active={ isInitial }
editedState="secondary"
onClick={ () => setEditedState( 'initial' ) }
>
{ __( 'Initial', 'newspack' ) }
</Button>
<Button
data-is-active={ ! isInitial }
editedState="secondary"
onClick={ () => setEditedState( 'success' ) }
>
{ __( 'Success', 'newspack' ) }
</Button>
</div>
</div>
{ editedState === 'initial' && (
<div className={ `newspack-registration ${ className }` }>
<form onSubmit={ ev => ev.preventDefault() }>
<div className="newspack-registration__form-content">
{ newsletterSubscription && lists.length ? (
<div className="newspack-registration__lists">
{ lists?.length > 1 && (
<RichText
onChange={ value => setAttributes( { newsletterTitle: value } ) }
placeholder={ __( 'Newsletters title…', 'newspack' ) }
value={ newsletterTitle }
tagName="h3"
/>
) }
<ul>
{ lists.map( listId => (
<li key={ listId }>
<span className="newspack-registration__lists__checkbox">
<input type="checkbox" checked readOnly />
</span>
<span className="newspack-registration__lists__details">
<span className="newspack-registration__lists__label">
<span className="newspack-registration__lists__title">
{ lists.length === 1 ? (
<RichText
onChange={ value =>
setAttributes( { newsletterLabel: value } )
}
placeholder={ __( 'Subscribe to our newsletter', 'newspack' ) }
value={ newsletterLabel }
tagName="span"
/>
) : (
listConfig[ listId ]?.title
) }
</span>
{ displayListDescription && (
<span className="newspack-registration__lists__description">
{ listConfig[ listId ]?.description }
</span>
) }
</span>
{ displayListDescription && (
<span className="newspack-registration__lists__description">
{ listConfig[ listId ]?.description }
</span>
) }
</span>
</span>
</li>
) ) }
</ul>
</div>
) : null }
<div className="newspack-registration__main">
<div>
<div className="newspack-registration__inputs">
<input type="email" placeholder={ placeholder } />
<button type="submit">
<RichText
onChange={ value => setAttributes( { label: value } ) }
placeholder={ __( 'Sign up', 'newspack' ) }
value={ label }
tagName="span"
/>
</button>
</li>
) ) }
</ul>
</div>
{ newspack_blocks.has_google_oauth && (
<div className="newspack-registration__logins">
<div className="newspack-registration__logins__separator">
<div />
<div>{ __( 'OR', 'newspack' ) }</div>
<div />
</div>
<button className="newspack-registration__logins__google">
<span
dangerouslySetInnerHTML={ { __html: newspack_blocks.google_logo_svg } }
) : null }
<div className="newspack-registration__main">
<div>
<div className="newspack-registration__inputs">
<input type="email" placeholder={ placeholder } />
<button type="submit">
<RichText
onChange={ value => setAttributes( { label: value } ) }
placeholder={ __( 'Sign up', 'newspack' ) }
value={ label }
tagName="span"
/>
<span>{ __( 'Sign in with Google', 'newspack' ) }</span>
</button>
</div>
) }
<div className="newspack-registration__response" />
</div>

<div className="newspack-registration__help-text">
<RichText
onChange={ value => setAttributes( { privacyLabel: value } ) }
placeholder={ __( 'Terms & Conditions statement…', 'newspack' ) }
value={ privacyLabel }
tagName="p"
/>
<p>
{ newspack_blocks.has_google_oauth && (
<div className="newspack-registration__logins">
<div className="newspack-registration__logins__separator">
<div />
<div>{ __( 'OR', 'newspack' ) }</div>
<div />
</div>
<button className="newspack-registration__logins__google">
<span
dangerouslySetInnerHTML={ { __html: newspack_blocks.google_logo_svg } }
/>
<span>{ __( 'Sign in with Google', 'newspack' ) }</span>
</button>
</div>
) }
<div className="newspack-registration__response" />
</div>
<div className="newspack-registration__help-text">
<RichText
onChange={ value => setAttributes( { haveAccountLabel: value } ) }
placeholder={ __( 'Already have an account?', 'newspack' ) }
value={ haveAccountLabel }
tagName="span"
/>{ ' ' }
<a href="/my-account" onClick={ ev => ev.preventDefault() }>
onChange={ value => setAttributes( { privacyLabel: value } ) }
placeholder={ __( 'Terms & Conditions statement…', 'newspack' ) }
value={ privacyLabel }
tagName="p"
/>
<p>
<RichText
onChange={ value => setAttributes( { signInLabel: value } ) }
placeholder={ __( 'Sign In', 'newspack' ) }
value={ signInLabel }
onChange={ value => setAttributes( { haveAccountLabel: value } ) }
placeholder={ __( 'Already have an account?', 'newspack' ) }
value={ haveAccountLabel }
tagName="span"
/>
</a>
</p>
/>{ ' ' }
<a href="/my-account" onClick={ ev => ev.preventDefault() }>
<RichText
onChange={ value => setAttributes( { signInLabel: value } ) }
placeholder={ __( 'Sign In', 'newspack' ) }
value={ signInLabel }
tagName="span"
/>
</a>
</p>
</div>
</div>
</div>
</div>
</form>
</div>
</form>
</div>
) }
{ editedState === 'success' && <div { ...innerBlocksProps } /> }
</div>
</>
);
Expand Down
35 changes: 35 additions & 0 deletions assets/blocks/reader-registration/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use './style';
@use '~@wordpress/base-styles/colors' as wp-colors;

.newspack-registration {
form {
Expand Down Expand Up @@ -31,6 +32,40 @@
vertical-align: bottom;
}
}
&__state-bar {
align-items: center;
border: 1px solid wp-colors.$gray-900;
border-radius: 2px;
display: flex;
font-family: -apple-system, blinkmacsystemfont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
flex-wrap: wrap;
font-size: 13px;
gap: 8px;
justify-content: space-between;
line-height: 1.4;
margin-bottom: 12px;
padding: 7px 8px;
button {
box-shadow: none !important;
color: wp-colors.$gray-900 !important;
height: 32px !important;
&[data-is-active='true'] {
background-color: wp-colors.$gray-900;
color: white !important;
}
&:hover:not( [data-is-active='true'] ) {
color: var( --wp-admin-theme-color ) !important;
}
}
span {
font-weight: 500;
}
div {
display: flex;
gap: 4px;
}
}
&__logins {
button {
padding: 0.76rem 1rem;
Expand Down
6 changes: 4 additions & 2 deletions assets/blocks/reader-registration/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';
import { Path, SVG } from '@wordpress/components';

/**
Expand All @@ -11,8 +12,8 @@ import metadata from './block.json';

export const icon = (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M18.5 8V5.5H16V4h2.5V1.5H20V4h2.5v1.5H20V8h-1.5ZM16.75 17v-2A2.75 2.75 0 0 0 14 12.25h-4A2.75 2.75 0 0 0 7.25 15v2h1.5v-2c0-.69.56-1.25 1.25-1.25h4c.69 0 1.25.56 1.25 1.25v2h1.5Z" />
<path
<Path d="M18.5 8V5.5H16V4h2.5V1.5H20V4h2.5v1.5H20V8h-1.5ZM16.75 17v-2A2.75 2.75 0 0 0 14 12.25h-4A2.75 2.75 0 0 0 7.25 15v2h1.5v-2c0-.69.56-1.25 1.25-1.25h4c.69 0 1.25.56 1.25 1.25v2h1.5Z" />
<Path
fillRule="evenodd"
clipRule="evenodd"
d="M14.5 8.5a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0Zm-1.5 0a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"
Expand All @@ -30,4 +31,5 @@ export const settings = {
foreground: '#36f',
},
edit,
save: () => <div { ...useInnerBlocksProps.save( useBlockProps.save() ) } />,
};
Loading