Refactor panel toggle reducer to be more readable
[MAILPOET-2455]
This commit is contained in:
committed by
Jack Kitterhing
parent
a1ac7cb4fd
commit
cd0cce07a6
@@ -61,11 +61,17 @@ export function switchSidebarTab(id) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toggleSidebarPanel(id, isOpened = undefined) {
|
/**
|
||||||
|
* Toggle a panel within the sidebar. Use toggleTo to enforce certain state
|
||||||
|
* @param {string} id
|
||||||
|
* @param {string|undefined} toggleTo - possible values 'opened', 'closed'
|
||||||
|
* @return {{toggleTo: string|undefined, id: string, type: string}}
|
||||||
|
*/
|
||||||
|
export function toggleSidebarPanel(id, toggleTo = undefined) {
|
||||||
return {
|
return {
|
||||||
type: 'TOGGLE_SIDEBAR_PANEL',
|
type: 'TOGGLE_SIDEBAR_PANEL',
|
||||||
id,
|
id,
|
||||||
isOpened,
|
toggleTo,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,13 +1,34 @@
|
|||||||
import { remove } from 'lodash';
|
import { remove } from 'lodash';
|
||||||
|
|
||||||
export default (state, action) => {
|
const getRequiredAction = (openedPanels, panelId, toggleTo) => {
|
||||||
const openedPanels = [...state.sidebar.openedPanels];
|
const isPanelOpened = openedPanels.includes(panelId);
|
||||||
const isOpenedCurrent = openedPanels.includes(action.id);
|
let requestedToggleState = toggleTo;
|
||||||
const isOpenedFinal = action.isOpened !== undefined ? action.isOpened : !isOpenedCurrent;
|
if (requestedToggleState === undefined) {
|
||||||
if (isOpenedFinal && !isOpenedCurrent) {
|
requestedToggleState = isPanelOpened ? 'closed' : 'opened';
|
||||||
openedPanels.push(action.id);
|
|
||||||
}
|
}
|
||||||
if (!isOpenedFinal && isOpenedCurrent) {
|
if (isPanelOpened && requestedToggleState === 'closed') {
|
||||||
|
return 'close';
|
||||||
|
}
|
||||||
|
if (!isPanelOpened && requestedToggleState === 'opened') {
|
||||||
|
return 'open';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} state
|
||||||
|
* @param {{toggleTo: string|undefined, id: string, type: string}} action
|
||||||
|
* @return {object} Modified state object
|
||||||
|
*/
|
||||||
|
export default (state, action) => {
|
||||||
|
if (action.toggleTo !== undefined && !['opened', 'closed'].includes(action.toggleTo)) {
|
||||||
|
throw new Error(`Unexpected toggleTo value "${action.toggleTo}"`);
|
||||||
|
}
|
||||||
|
const openedPanels = [...state.sidebar.openedPanels];
|
||||||
|
const requiredAction = getRequiredAction(openedPanels, action.id, action.toggleTo);
|
||||||
|
if (requiredAction === 'open') {
|
||||||
|
openedPanels.push(action.id);
|
||||||
|
} else if (requiredAction === 'close') {
|
||||||
remove(openedPanels, (item) => item === action.id);
|
remove(openedPanels, (item) => item === action.id);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
Reference in New Issue
Block a user