Cleanup JS code from styles - template association

[MAILPOET-6335]
This commit is contained in:
Rostislav Wolny
2024-12-12 13:44:46 +01:00
committed by Aschepikov
parent 269b55ed08
commit bc10ae7216
4 changed files with 14 additions and 34 deletions

View File

@@ -85,10 +85,7 @@ export function SelectTemplateModal( {
} }
void dispatch( storeName ).setTemplateToPost( void dispatch( storeName ).setTemplateToPost(
templateIsPostContent ? postContent.template : template.slug, templateIsPostContent ? postContent.template : template.slug
templateIsPostContent
? template?.template?.mailpoet_email_theme || {}
: template.template.mailpoet_email_theme ?? {}
); );
onSelectCallback(); onSelectCallback();
}; };

View File

@@ -8,7 +8,6 @@ import {
EmailTemplatePreview, EmailTemplatePreview,
TemplatePreview, TemplatePreview,
EmailEditorPostType, EmailEditorPostType,
EmailTheme,
} from '../store'; } from '../store';
/** /**
@@ -41,29 +40,22 @@ function setPostContentInnerBlocks(
} ); } );
} }
const InternalCssThemeCache = {}; const InternalTemplateCache = {};
type GenerateTemplateCssThemeType = { type GenerateTemplateCssThemeType = {
emailThemeCss: string;
mailpoetEmailTheme?: EmailTheme;
postTemplateContent?: EmailTemplatePreview; postTemplateContent?: EmailTemplatePreview;
}; };
/** /**
* We are reusing the template CSS and mailpoet theme by fetching the template from
* the list of email editor available templates.
* Note: This function may need an update when https://mailpoet.atlassian.net/browse/MAILPOET-6335 is merged
* @param post * @param post
* @param allTemplates * @param allTemplates
*/ */
function generateTemplateCssTheme( function generateTemplateContent(
post: EmailEditorPostType, post: EmailEditorPostType,
allTemplates: TemplatePreview[] = [] allTemplates: TemplatePreview[] = []
): GenerateTemplateCssThemeType { ): GenerateTemplateCssThemeType {
const contentTemplate = post.template; const contentTemplate = post.template;
const defaultReturnObject = { const defaultReturnObject = {
mailpoetEmailTheme: null,
emailThemeCss: '',
postTemplateContent: null, postTemplateContent: null,
}; };
@@ -71,8 +63,8 @@ function generateTemplateCssTheme(
return defaultReturnObject; return defaultReturnObject;
} }
if ( InternalCssThemeCache[ contentTemplate ] ) { if ( InternalTemplateCache[ contentTemplate ] ) {
return InternalCssThemeCache[ contentTemplate ]; return InternalTemplateCache[ contentTemplate ];
} }
const postTemplate = allTemplates.find( const postTemplate = allTemplates.find(
@@ -83,16 +75,13 @@ function generateTemplateCssTheme(
return defaultReturnObject; return defaultReturnObject;
} }
const cssTheme = { const templateContent = {
mailpoetEmailTheme:
postTemplate?.template?.mailpoet_email_theme || null,
emailThemeCss: postTemplate?.template?.email_theme_css || '',
postTemplateContent: postTemplate?.template, postTemplateContent: postTemplate?.template,
}; };
InternalCssThemeCache[ contentTemplate ] = cssTheme; InternalTemplateCache[ contentTemplate ] = templateContent;
return cssTheme; return templateContent;
} }
export function usePreviewTemplates( export function usePreviewTemplates(
@@ -178,8 +167,10 @@ export function usePreviewTemplates(
const allEmailPosts = useMemo( () => { const allEmailPosts = useMemo( () => {
return emailPosts?.map( ( post: EmailEditorPostType ) => { return emailPosts?.map( ( post: EmailEditorPostType ) => {
const { mailpoetEmailTheme, emailThemeCss, postTemplateContent } = const { postTemplateContent } = generateTemplateContent(
generateTemplateCssTheme( post, allTemplates ); post,
allTemplates
);
const parsedPostContent = parse( post.content?.raw ); const parsedPostContent = parse( post.content?.raw );
let parsedPostContentWithTemplate = parsedPostContent; let parsedPostContentWithTemplate = parsedPostContent;
@@ -203,8 +194,6 @@ export function usePreviewTemplates(
rendered: rendered:
post?.mailpoet_data?.subject || post.title.rendered, // use MailPoet subject as title post?.mailpoet_data?.subject || post.title.rendered, // use MailPoet subject as title
}, },
mailpoet_email_theme: mailpoetEmailTheme,
email_theme_css: emailThemeCss,
}, },
category: 'recent', category: 'recent',
type: post.type, type: post.type,

View File

@@ -11,7 +11,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch } from '@wordpress/data-controls';
import wpApiFetch from '@wordpress/api-fetch'; import wpApiFetch from '@wordpress/api-fetch';
import { storeName, mainSidebarDocumentTab } from './constants'; import { storeName, mainSidebarDocumentTab } from './constants';
import { SendingPreviewStatus, State, Feature, EmailTheme } from './types'; import { SendingPreviewStatus, State, Feature } from './types';
import { addQueryArgs } from '@wordpress/url'; import { addQueryArgs } from '@wordpress/url';
import { import {
// @ts-expect-error No types for __unstableSerializeAndClean // @ts-expect-error No types for __unstableSerializeAndClean
@@ -127,16 +127,13 @@ export function* updateEmailMailPoetProperty( name: string, value: string ) {
} }
export const setTemplateToPost = export const setTemplateToPost =
( templateSlug, emailTheme: EmailTheme ) => ( templateSlug ) =>
async ( { registry } ) => { async ( { registry } ) => {
const postId = registry.select( storeName ).getEmailPostId(); const postId = registry.select( storeName ).getEmailPostId();
registry registry
.dispatch( coreDataStore ) .dispatch( coreDataStore )
.editEntityRecord( 'postType', 'mailpoet_email', postId, { .editEntityRecord( 'postType', 'mailpoet_email', postId, {
template: templateSlug, template: templateSlug,
meta: {
mailpoet_email_theme: emailTheme,
},
} ); } );
}; };

View File

@@ -206,9 +206,6 @@ export type EmailTemplate = {
id: string; id: string;
slug: string; slug: string;
content: string; content: string;
email_theme_css: string;
mailpoet_email_theme?: EmailTheme;
theme: string;
title: string; title: string;
type: string; type: string;
}; };