Save active tab in email editor store

[MAILPOET-5638]
This commit is contained in:
Jan Lysý
2024-02-22 17:08:32 +01:00
committed by Jan Lysý
parent a7b67aa23b
commit b32922dfc8
8 changed files with 65 additions and 39 deletions

View File

@ -1,32 +1,35 @@
import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor'; import { store as blockEditorStore } from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { import {
mainSidebarEmailKey, mainSidebarBlockTab,
mainSidebarBlockKey, mainSidebarEmailTab,
storeName, storeName,
} from '../../store'; } from '../../store';
type Props = { export function Header() {
sidebarKey: string; const { toggleSettingsSidebarActiveTab } = useDispatch(storeName);
};
export function Header({ sidebarKey }: Props) {
const { openSidebar } = useDispatch(storeName);
const selectedBlockId = useSelect( const selectedBlockId = useSelect(
(select) => select(blockEditorStore).getSelectedBlockClientId(), (select) => select(blockEditorStore).getSelectedBlockClientId(),
[], [],
); );
const activeTab = useSelect(
(select) => select(storeName).getSettingsSidebarActiveTab(),
[],
);
// Switch tab based on selected block. // Switch tab based on selected block.
useEffect(() => { useEffect(() => {
void openSidebar( if (selectedBlockId) {
selectedBlockId ? mainSidebarBlockKey : mainSidebarEmailKey, void toggleSettingsSidebarActiveTab(mainSidebarBlockTab);
); } else {
}, [selectedBlockId, openSidebar]); void toggleSettingsSidebarActiveTab(mainSidebarEmailTab);
}
}, [selectedBlockId, toggleSettingsSidebarActiveTab]);
return ( return (
<div className="components-panel__header interface-complementary-area-header edit-post-sidebar__panel-tabs"> <div className="components-panel__header interface-complementary-area-header edit-post-sidebar__panel-tabs">
@ -34,11 +37,11 @@ export function Header({ sidebarKey }: Props) {
<li> <li>
<button <button
onClick={() => { onClick={() => {
void openSidebar(mainSidebarEmailKey); void toggleSettingsSidebarActiveTab(mainSidebarEmailTab);
}} }}
className={classnames( className={classnames(
'components-button edit-post-sidebar__panel-tab', 'components-button edit-post-sidebar__panel-tab',
{ 'is-active': sidebarKey === mainSidebarEmailKey }, { 'is-active': activeTab === mainSidebarEmailTab },
)} )}
data-automation-id="email_settings_tab" data-automation-id="email_settings_tab"
type="button" type="button"
@ -49,11 +52,11 @@ export function Header({ sidebarKey }: Props) {
<li> <li>
<button <button
onClick={() => { onClick={() => {
void openSidebar(mainSidebarBlockKey); void toggleSettingsSidebarActiveTab(mainSidebarBlockTab);
}} }}
className={classnames( className={classnames(
'components-button edit-post-sidebar__panel-tab', 'components-button edit-post-sidebar__panel-tab',
{ 'is-active': sidebarKey === mainSidebarBlockKey }, { 'is-active': activeTab === mainSidebarBlockTab },
)} )}
data-automation-id="mailpoet_block_settings_tab" data-automation-id="mailpoet_block_settings_tab"
type="button" type="button"

View File

@ -2,16 +2,14 @@ import { Panel } from '@wordpress/components';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data'; import { useSelect } from '@wordpress/data';
import { BlockInspector } from '@wordpress/block-editor'; import { BlockInspector } from '@wordpress/block-editor';
import { import { ComplementaryArea } from '@wordpress/interface';
ComplementaryArea,
store as interfaceStore,
} from '@wordpress/interface';
import { ComponentProps } from 'react'; import { ComponentProps } from 'react';
import { drawerRight } from '@wordpress/icons'; import { drawerRight } from '@wordpress/icons';
import { import {
storeName, storeName,
mainSidebarEmailKey, mainSidebarEmailTab,
mainSidebarBlockKey, mainSidebarBlockTab,
mainSidebarId,
} from '../../store'; } from '../../store';
import { Header } from './header'; import { Header } from './header';
import { EmailSettings } from './email-settings'; import { EmailSettings } from './email-settings';
@ -19,25 +17,24 @@ import { EmailSettings } from './email-settings';
type Props = ComponentProps<typeof ComplementaryArea>; type Props = ComponentProps<typeof ComplementaryArea>;
export function Sidebar(props: Props): JSX.Element { export function Sidebar(props: Props): JSX.Element {
const { sidebarKey } = useSelect((select) => ({ const activeTab = useSelect(
sidebarKey: (select) => select(storeName).getSettingsSidebarActiveTab(),
select(interfaceStore).getActiveComplementaryArea(storeName) ?? [],
mainSidebarEmailKey, );
}));
return ( return (
<ComplementaryArea <ComplementaryArea
identifier={sidebarKey} identifier={mainSidebarId}
className="edit-post-sidebar" className="edit-post-sidebar"
header={<Header sidebarKey={sidebarKey} />} header={<Header />}
icon={drawerRight} icon={drawerRight}
scope={storeName} scope={storeName}
smallScreenTitle={__('No title', 'mailpoet')} smallScreenTitle={__('No title', 'mailpoet')}
isActiveByDefault isActiveByDefault
{...props} {...props}
> >
{sidebarKey === mainSidebarEmailKey && <EmailSettings />} {activeTab === mainSidebarEmailTab && <EmailSettings />}
{sidebarKey === mainSidebarBlockKey && ( {activeTab === mainSidebarBlockTab && (
<Panel> <Panel>
<BlockInspector /> <BlockInspector />
</Panel> </Panel>

View File

@ -5,7 +5,7 @@ import { store as preferencesStore } from '@wordpress/preferences';
import { store as noticesStore } from '@wordpress/notices'; import { store as noticesStore } from '@wordpress/notices';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch } from '@wordpress/data-controls';
import { storeName, mainSidebarEmailKey } from './constants'; import { storeName, mainSidebarEmailTab } from './constants';
import { SendingPreviewStatus, State, Feature } from './types'; import { SendingPreviewStatus, State, Feature } from './types';
export const toggleFeature = export const toggleFeature =
@ -47,7 +47,7 @@ export function updateSendPreviewEmail(toEmail: string) {
} }
export const openSidebar = export const openSidebar =
(key = mainSidebarEmailKey) => (key = mainSidebarEmailTab) =>
({ registry }): unknown => ({ registry }): unknown =>
registry.dispatch(interfaceStore).enableComplementaryArea(storeName, key); registry.dispatch(interfaceStore).enableComplementaryArea(storeName, key);
@ -56,6 +56,13 @@ export const closeSidebar =
({ registry }): unknown => ({ registry }): unknown =>
registry.dispatch(interfaceStore).disableComplementaryArea(storeName); registry.dispatch(interfaceStore).disableComplementaryArea(storeName);
export function toggleSettingsSidebarActiveTab(activeTab: string) {
return {
type: 'TOGGLE_SETTINGS_SIDEBAR_ACTIVE_TAB',
state: { activeTab } as Partial<State['settingsSidebar']>,
} as const;
}
export function* saveEditedEmail() { export function* saveEditedEmail() {
const postId = select(storeName).getEmailPostId(); const postId = select(storeName).getEmailPostId();
// This returns a promise // This returns a promise

View File

@ -1,5 +1,5 @@
export const storeName = 'email-editor/editor'; export const storeName = 'email-editor/editor';
export const mainSidebarId = 'email-editor/editor/main'; export const mainSidebarId = 'email-editor/editor/main';
export const mainSidebarEmailKey = 'email-editor/editor/main/email'; export const mainSidebarEmailTab = 'email';
export const mainSidebarBlockKey = 'email-editor/editor/main/block'; export const mainSidebarBlockTab = 'block';

View File

@ -1,3 +1,4 @@
import { mainSidebarEmailTab } from './constants';
import { State } from './types'; import { State } from './types';
import { import {
getEditorLayout, getEditorLayout,
@ -15,6 +16,9 @@ export function getInitialState(): State {
listviewSidebar: { listviewSidebar: {
isOpened: false, isOpened: false,
}, },
settingsSidebar: {
activeTab: mainSidebarEmailTab,
},
postId, postId,
editorSettings: getEditorSettings(), editorSettings: getEditorSettings(),
layoutStyles: getEmailLayoutStyles(), layoutStyles: getEmailLayoutStyles(),

View File

@ -31,6 +31,14 @@ export function reducer(state: State, action): State {
...state, ...state,
preview: { ...state.preview, ...action.state }, preview: { ...state.preview, ...action.state },
}; };
case 'TOGGLE_SETTINGS_SIDEBAR_ACTIVE_TAB':
return {
...state,
settingsSidebar: {
...state.settingsSidebar,
activeTab: action.state.activeTab,
},
};
default: default:
return state; return state;
} }

View File

@ -111,6 +111,10 @@ export function isListviewSidebarOpened(state: State): boolean {
return state.listviewSidebar.isOpened; return state.listviewSidebar.isOpened;
} }
export function getSettingsSidebarActiveTab(state: State): string {
return state.settingsSidebar.activeTab;
}
export function getInitialEditorSettings( export function getInitialEditorSettings(
state: State, state: State,
): State['editorSettings'] { ): State['editorSettings'] {

View File

@ -48,6 +48,9 @@ export type State = {
listviewSidebar: { listviewSidebar: {
isOpened: boolean; isOpened: boolean;
}; };
settingsSidebar: {
activeTab: string;
};
postId: number; postId: number;
editorSettings: EmailEditorSettings; editorSettings: EmailEditorSettings;
layoutStyles: EmailLayoutStyles; layoutStyles: EmailLayoutStyles;