Actions and reducers for managing UI contexts

This commit is contained in:
dannyrb 2019-06-03 12:22:06 -04:00
parent 56de51a21d
commit 9326b5e63d
2 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,18 @@
export const addActiveContext = state => ({
type: 'ADD_ACTIVE_CONTEXT',
state,
});
export const removeActiveContext = state => ({
type: 'REMOVE_ACTIVE_CONTEXT',
state,
});
export const clearActiveContexts = state => ({
type: 'CLEAR_ACTIVE_CONTEXTS',
state,
});
export const setLeftSidebarOpen = state => ({
type: 'SET_LEFT_SIDEBAR_OPEN',
state,
@ -14,6 +29,10 @@ export const setUserPreferencesModalOpen = state => ({
});
const actions = {
addActiveContext,
removeActiveContext,
clearActiveContexts,
//
setLeftSidebarOpen,
setRightSidebarOpen,
setUserPreferencesModalOpen,

View File

@ -4,10 +4,28 @@ const defaultState = {
userPreferencesModalOpen: false,
labelling: {},
contextMenu: {},
activeContexts: [],
};
const ui = (state = defaultState, action) => {
switch (action.type) {
// ~ ACTIVE CONTEXTS
// https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns#inserting-and-removing-items-in-arrays
case 'ADD_ACTIVE_CONTEXT': {
const shallowCopy = Object.assign({}, state);
shallowCopy.activeContexts = [...shallowCopy.activeContexts, action.item];
return shallowCopy;
}
case 'REMOVE_ACTIVE_CONTEXT': {
const shallowCopy = Object.assign({}, state);
shallowCopy.activeContexts = shallowCopy.activeContexts.filter(
item => item !== action.item
);
return shallowCopy;
}
case 'CLEAR_ACTIVE_CONTEXTS':
return Object.assign({}, state, { activeContexts: [] });
// ~ SIDEBAR
case 'SET_LEFT_SIDEBAR_OPEN':
return Object.assign({}, state, { leftSidebarOpen: action.state });
case 'SET_RIGHT_SIDEBAR_OPEN':