Refactor sidebar tabs from local state to store

[MAILPOET-2455]
This commit is contained in:
Rostislav Wolny
2019-11-26 12:23:01 +01:00
committed by Jack Kitterhing
parent 1287152d0f
commit 1b0497bad3
6 changed files with 31 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
import React, { useState } from 'react'; import React from 'react';
import { IconButton } from '@wordpress/components'; import { IconButton } from '@wordpress/components';
import { useDispatch } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classnames from 'classnames'; import classnames from 'classnames';
@@ -29,9 +29,12 @@ SidebarHeader.propTypes = {
}; };
export default () => { export default () => {
const [activeTab, setActiveTab] = useState('form'); const activeTab = useSelect(
(select) => select('mailpoet-form-editor').getSidebarActiveTab(),
[]
);
const { toggleSidebar } = useDispatch('mailpoet-form-editor'); const { toggleSidebar, switchSidebarTab } = useDispatch('mailpoet-form-editor');
return ( return (
<div className="edit-post-sidebar"> <div className="edit-post-sidebar">
@@ -39,7 +42,7 @@ export default () => {
<ul> <ul>
<li> <li>
<button <button
onClick={() => setActiveTab('form')} onClick={() => switchSidebarTab('form')}
className={classnames('edit-post-sidebar__panel-tab', { 'is-active': activeTab === 'form' })} className={classnames('edit-post-sidebar__panel-tab', { 'is-active': activeTab === 'form' })}
type="button" type="button"
> >
@@ -48,7 +51,7 @@ export default () => {
</li> </li>
<li> <li>
<button <button
onClick={() => setActiveTab('block')} onClick={() => switchSidebarTab('block')}
className={classnames('edit-post-sidebar__panel-tab', { 'is-active': activeTab === 'block' })} className={classnames('edit-post-sidebar__panel-tab', { 'is-active': activeTab === 'block' })}
type="button" type="button"
> >

View File

@@ -63,6 +63,13 @@ export function removeNotice(id) {
}; };
} }
export function switchSidebarTab(id) {
return {
type: 'SWITCH_SIDEBAR_TAB',
id,
};
}
export function* saveForm() { export function* saveForm() {
yield { yield {
type: 'SAVE_FORM', type: 'SAVE_FORM',

View File

@@ -6,6 +6,7 @@ import saveFormStarted from './reducers/saveFormStarted.jsx';
import saveFormDone from './reducers/saveFormDone.jsx'; import saveFormDone from './reducers/saveFormDone.jsx';
import removeNotice from './reducers/removeNotice.jsx'; import removeNotice from './reducers/removeNotice.jsx';
import changeFormSettings from './reducers/changeFormSettings.jsx'; import changeFormSettings from './reducers/changeFormSettings.jsx';
import switchSidebarTab from './reducers/switchSidebarTab.jsx';
export default (defaultState) => (state = defaultState, action) => { export default (defaultState) => (state = defaultState, action) => {
switch (action.type) { switch (action.type) {
@@ -17,6 +18,7 @@ export default (defaultState) => (state = defaultState, action) => {
case 'SAVE_FORM_STARTED': return saveFormStarted(state); case 'SAVE_FORM_STARTED': return saveFormStarted(state);
case 'TOGGLE_SIDEBAR': return toggleSidebar(state, action); case 'TOGGLE_SIDEBAR': return toggleSidebar(state, action);
case 'CHANGE_FORM_SETTINGS': return changeFormSettings(state, action); case 'CHANGE_FORM_SETTINGS': return changeFormSettings(state, action);
case 'SWITCH_SIDEBAR_TAB': return switchSidebarTab(state, action);
default: default:
return state; return state;
} }

View File

@@ -0,0 +1,7 @@
export default (state, action) => ({
...state,
sidebar: {
...state.sidebar,
activeTab: action.id,
},
});

View File

@@ -38,4 +38,7 @@ export default {
getFormErrors(state) { getFormErrors(state) {
return state.formErrors; return state.formErrors;
}, },
getSidebarActiveTab(state) {
return state.sidebar.activeTab;
},
}; };

View File

@@ -18,6 +18,9 @@ const defaultState = {
pages: window.mailpoet_form_pages, pages: window.mailpoet_form_pages,
isFormSaving: false, isFormSaving: false,
notices: [], notices: [],
sidebar: {
activeTab: 'form',
},
}; };
const config = { const config = {