2022-03-18 14:08:49 +01:00
|
|
|
import find from 'lodash/find';
|
2022-03-18 00:13:35 +01:00
|
|
|
import filter from 'lodash/filter';
|
|
|
|
import last from 'lodash/last';
|
|
|
|
import { createSlice } from '@reduxjs/toolkit'
|
|
|
|
|
|
|
|
// todo: errors should be tracked in each slice and displayed as toasts
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
tabs: [],
|
2022-03-18 14:08:49 +01:00
|
|
|
activeTabUid: null,
|
|
|
|
hasChanges: false
|
2022-03-18 00:13:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const tabsSlice = createSlice({
|
|
|
|
name: 'tabs',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
addTab: (state, action) => {
|
|
|
|
state.tabs.push({
|
|
|
|
uid: action.payload.uid,
|
|
|
|
collectionUid: action.payload.collectionUid
|
|
|
|
});
|
|
|
|
state.activeTabUid = action.payload.uid;
|
|
|
|
},
|
|
|
|
focusTab: (state, action) => {
|
|
|
|
state.activeTabUid = action.payload.uid;
|
|
|
|
},
|
|
|
|
closeTab: (state, action) => {
|
|
|
|
state.tabs = filter(state.tabs, (t) => t.uid !== action.payload);
|
|
|
|
|
|
|
|
if(state.tabs && state.tabs.length) {
|
|
|
|
// todo: closing tab needs to focus on the right adjacent tab
|
|
|
|
state.activeTabUid = last(state.tabs).uid;
|
|
|
|
} else {
|
|
|
|
state.activeTabUid = null;
|
|
|
|
}
|
2022-03-18 14:08:49 +01:00
|
|
|
},
|
|
|
|
requestChanged: (state, action) => {
|
|
|
|
const tab = find(state.tabs, (t) => t.uid == action.payload.itemUid);
|
|
|
|
if(tab) {
|
|
|
|
tab.hasChanges = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
requestSaved: (state, action) => {
|
|
|
|
const tab = find(state.tabs, (t) => t.uid == action.payload.itemUid);
|
|
|
|
if(tab) {
|
|
|
|
tab.hasChanges = false;
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 00:13:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const {
|
|
|
|
addTab,
|
|
|
|
focusTab,
|
2022-03-18 14:08:49 +01:00
|
|
|
closeTab,
|
|
|
|
requestChanged,
|
|
|
|
requestSaved
|
2022-03-18 00:13:35 +01:00
|
|
|
} = tabsSlice.actions;
|
|
|
|
|
|
|
|
export default tabsSlice.reducer;
|