Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid Redux usage and related errors in non-newsletter email editors #1688

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/send-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { get } from 'lodash';
* Internal dependencies
*/
import { getServiceProvider } from '../../service-providers';
import { refreshEmailHtml, validateNewsletter } from '../../newsletter-editor/utils';
import { validateNewsletter } from '../../newsletter-editor/utils';
import { useNewsletterData } from '../../newsletter-editor/store';
import { refreshEmailHtml } from '../../editor/mjml';
import './style.scss';

function PreviewHTML() {
Expand Down
104 changes: 69 additions & 35 deletions src/editor/mjml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,52 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { refreshEmailHtml, usePrevious } from '../../newsletter-editor/utils';
import { usePrevious } from '../../newsletter-editor/utils';

/**
* Internal dependencies
*/
import { fetchNewsletterData, fetchSyncErrors, updateNewsletterDataError, updateIsRefreshingHtml, useIsRefreshingHtml } from '../../newsletter-editor/store';
import { getServiceProvider } from '../../service-providers';
import {
fetchNewsletterData,
fetchSyncErrors,
updateIsRefreshingHtml,
} from '../../newsletter-editor/store';

/**
* External dependencies
*/
import mjml2html from 'mjml-browser';

/**
* Refresh the email-compliant HTML for a post.
*
* @param {number} postId The current post ID.
* @param {string} postTitle The current post title.
* @param {string} postContent The current post content.
* @return {Promise<string>} The refreshed email HTML.
*/
export const refreshEmailHtml = async ( postId, postTitle, postContent ) => {
const mjml = await apiFetch( {
path: `/newspack-newsletters/v1/post-mjml`,
method: 'POST',
data: {
post_id: postId,
title: postTitle,
content: postContent,
},
} );

// Once received MJML markup, convert it to email-compliant HTML and save as post meta.
const { html } = mjml2html( mjml, { keepComments: false, minify: true } );
return html;
};

function MJML() {
const isRefreshingHTML = useIsRefreshingHtml();
const {
saveSucceeded,
isPublishing,
Expand Down Expand Up @@ -54,7 +87,7 @@ function MJML() {
isAutosaveLocked: isPostAutosavingLocked(),
};
} );

const { createNotice } = useDispatch( 'core/notices' );
const { lockPostAutosaving, lockPostSaving, unlockPostSaving, editPost } = useDispatch(
'core/editor'
);
Expand All @@ -79,42 +112,43 @@ function MJML() {
! isSaving &&
! isAutosaving &&
! isPublishing &&
! isRefreshingHTML &&
! isSent &&
saveSucceeded
) {
updateIsRefreshingHtml( true );
lockPostSaving( 'newspack-newsletters-refresh-html' );
refreshEmailHtml( postId, postTitle, postContent )
.then( refreshedHtml => {
updateMetaValue( newspack_email_editor_data.email_html_meta, refreshedHtml );
return apiFetch( {
data: { meta: { [ newspack_email_editor_data.email_html_meta ]: refreshedHtml } },
method: 'POST',
path: `/wp/v2/${ postType }/${ postId }`,
} );
} ).then( () => {
// Rehydrate ESP newsletter data after completing sync.
if ( isSupportedESP ) {
return fetchNewsletterData( postId );
}
return true;
} ).then ( () => {
// Check for sync errors after refreshing the HTML.
if ( isSupportedESP ) {
return fetchSyncErrors( postId );
}
return true;
} )
.catch( e => {
updateNewsletterDataError( e );
} )
.finally( () => {
unlockPostSaving( 'newspack-newsletters-refresh-html' );
updateIsRefreshingHtml( false );
} );
refreshHtml();
}
}, [ isSaving, isAutosaving ] );

const refreshHtml = async () => {
try {
lockPostSaving( 'newspack-newsletters-refresh-html' );
if ( isSupportedESP ) {
updateIsRefreshingHtml( true );
}
const refreshedHtml = await refreshEmailHtml( postId, postTitle, postContent );
updateMetaValue( newspack_email_editor_data.email_html_meta, refreshedHtml );

// Save the refreshed HTML to post meta.
await apiFetch( {
data: { meta: { [ newspack_email_editor_data.email_html_meta ]: refreshedHtml } },
method: 'POST',
path: `/wp/v2/${ postType }/${ postId }`,
} );

// Rehydrate ESP newsletter data after completing sync.
if ( isSupportedESP ) {
await fetchNewsletterData( postId );
await fetchSyncErrors( postId );
updateIsRefreshingHtml( false );
}
unlockPostSaving( 'newspack-newsletters-refresh-html' );
} catch ( e ) {
createNotice( 'error', e?.message || __( 'Error refreshing email HTML.', 'newspack-newsletters' ), {
id: 'newspack-newsletters-mjml-error',
isDismissible: true,
} );
}
}
}

export default MJML;
2 changes: 1 addition & 1 deletion src/newsletter-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function NewsletterEdit( { apiFetchWithErrorHandling, setInFlightForAsync, inFli
// Handle error messages from retrieve/sync requests with connected ESP.
useEffect( () => {
if ( newsletterDataError ) {
createNotice( 'error', newsletterDataError?.message || __( 'Error communicating with service provider.', 'newspack-newseltters' ), {
createNotice( 'error', newsletterDataError?.message || __( 'Error communicating with service provider.', 'newspack-newsletters' ), {
id: 'newspack-newsletters-newsletter-data-error',
isDismissible: true,
} );
Expand Down
30 changes: 0 additions & 30 deletions src/newsletter-editor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* External dependencies
*/
import mjml2html from 'mjml-browser';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -61,30 +55,6 @@ export const validateNewsletter = ( meta = {} ) => {
*/
export const hasValidEmail = string => /\S+@\S+/.test( string );

/**
* Refresh the email-compliant HTML for a post.
*
* @param {number} postId The current post ID.
* @param {string} postTitle The current post title.
* @param {string} postContent The current post content.
* @return {Promise<string>} The refreshed email HTML.
*/
export const refreshEmailHtml = async ( postId, postTitle, postContent ) => {
const mjml = await apiFetch( {
path: `/newspack-newsletters/v1/post-mjml`,
method: 'POST',
data: {
post_id: postId,
title: postTitle,
content: postContent,
},
} );

// Once received MJML markup, convert it to email-compliant HTML and save as post meta.
const { html } = mjml2html( mjml, { keepComments: false, minify: true } );
return html;
};

/**
* Custom hook to fetch a previous state or prop value.
*
Expand Down