feat: persist active workspace in local storage

This commit is contained in:
Anoop M D 2022-10-16 17:24:30 +05:30
parent 2efc11ff6b
commit ecc2252e84
2 changed files with 29 additions and 1 deletions

View File

@ -1,7 +1,9 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import find from 'lodash/find'; import find from 'lodash/find';
import map from 'lodash/map';
import filter from 'lodash/filter'; import filter from 'lodash/filter';
import { findCollectionInWorkspace } from 'utils/workspaces'; import { findCollectionInWorkspace } from 'utils/workspaces';
import cache from 'utils/common/cache';
const initialState = { const initialState = {
workspaces: [], workspaces: [],
@ -16,11 +18,19 @@ export const workspacesSlice = createSlice({
state.workspaces = action.payload.workspaces; state.workspaces = action.payload.workspaces;
if(state.workspaces && state.workspaces.length) { if(state.workspaces && state.workspaces.length) {
state.activeWorkspaceUid = state.workspaces[0].uid; const workspaceUids = map(state.workspaces, (w) => w.uid);
const activeWorkspaceUid = cache.getActiveWorkspaceUid();
if(activeWorkspaceUid && workspaceUids.includes(activeWorkspaceUid)) {
state.activeWorkspaceUid = activeWorkspaceUid;
} else {
state.activeWorkspaceUid = state.workspaces[0].uid;
cache.setActiveWorkspaceUid(state.activeWorkspaceUid);
}
} }
}, },
selectWorkspace: (state, action) => { selectWorkspace: (state, action) => {
state.activeWorkspaceUid = action.payload.workspaceUid; state.activeWorkspaceUid = action.payload.workspaceUid;
cache.setActiveWorkspaceUid(state.activeWorkspaceUid);
}, },
renameWorkspace: (state, action) => { renameWorkspace: (state, action) => {
const { name, uid } = action.payload; const { name, uid } = action.payload;

View File

@ -0,0 +1,18 @@
class Cache {
get(key) {
return window.localStorage.getItem(key);
}
set(key, val) {
window.localStorage.setItem(key, val);
}
getActiveWorkspaceUid() {
return this.get('bruno.activeWorkspaceUid');
}
setActiveWorkspaceUid(workspaceUid) {
this.set('bruno.activeWorkspaceUid', workspaceUid);
}
};
module.exports = new Cache();;