mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
workspace-store refactoring
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
a27ce9cf8b
commit
d4bbaf4f34
@ -20,7 +20,7 @@ export class BaseStore<T = any> extends Singleton {
|
||||
public whenLoaded = when(() => this.isLoaded);
|
||||
|
||||
@observable isLoaded = false;
|
||||
@observable protected data: T;
|
||||
@observable protected data = {} as T;
|
||||
|
||||
protected constructor(protected params: BaseStoreParams) {
|
||||
super();
|
||||
@ -109,7 +109,7 @@ export class BaseStore<T = any> extends Singleton {
|
||||
|
||||
// todo: use "serializr" ?
|
||||
protected fromStore(data: Partial<T> = {}) {
|
||||
this.data = data as T;
|
||||
Object.assign(this.data, data);
|
||||
}
|
||||
|
||||
toJSON(): T {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import Config from "conf"
|
||||
import Singleton from "./utils/singleton";
|
||||
import { computed, toJS } from "mobx";
|
||||
import { BaseStore } from "./base-store";
|
||||
import { clusterStore } from "./cluster-store"
|
||||
import { getAppVersion } from "./utils/app-version";
|
||||
|
||||
export type WorkspaceId = string;
|
||||
|
||||
@ -15,57 +14,53 @@ export interface Workspace {
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export class WorkspaceStore extends Singleton {
|
||||
static readonly defaultId = "default"
|
||||
export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
|
||||
static readonly defaultId: WorkspaceId = "default"
|
||||
|
||||
private storeConfig = new Config<WorkspaceStoreModel>({
|
||||
configName: "lens-workspace-store",
|
||||
projectVersion: getAppVersion(),
|
||||
});
|
||||
protected data: WorkspaceStoreModel = {
|
||||
workspaces: [{
|
||||
id: WorkspaceStore.defaultId,
|
||||
name: "default"
|
||||
}]
|
||||
}
|
||||
|
||||
@computed get workspaces() {
|
||||
return toJS(this.data.workspaces);
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
super();
|
||||
this.init();
|
||||
super({
|
||||
configName: "lens-workspace-store",
|
||||
});
|
||||
}
|
||||
|
||||
protected init() {
|
||||
if (!this.getWorkspace(WorkspaceStore.defaultId)) {
|
||||
this.saveWorkspace({
|
||||
id: WorkspaceStore.defaultId,
|
||||
name: "default"
|
||||
})
|
||||
}
|
||||
public getById(id: WorkspaceId): Workspace {
|
||||
return this.workspaces.find(workspace => workspace.id === id);
|
||||
}
|
||||
|
||||
public getWorkspace(id: WorkspaceId): Workspace {
|
||||
return this.getAllWorkspaces().find(workspace => workspace.id === id)
|
||||
public getIndexById(id: WorkspaceId): number {
|
||||
return this.workspaces.findIndex(workspace => workspace.id === id);
|
||||
}
|
||||
|
||||
public getAllWorkspaces(): Workspace[] {
|
||||
return this.storeConfig.get("workspaces", [])
|
||||
}
|
||||
|
||||
public saveWorkspace(workspace: Workspace) {
|
||||
const workspaces = this.getAllWorkspaces()
|
||||
const index = workspaces.findIndex((w) => w.id === workspace.id)
|
||||
if (index !== -1) {
|
||||
workspaces[index] = workspace
|
||||
public saveWorkspace(newWorkspace: Workspace) {
|
||||
const workspace = this.getById(newWorkspace.id);
|
||||
if (workspace) {
|
||||
Object.assign(workspace, newWorkspace);
|
||||
} else {
|
||||
workspaces.push(workspace)
|
||||
this.data.workspaces.push(newWorkspace);
|
||||
}
|
||||
this.storeConfig.set("workspaces", workspaces)
|
||||
}
|
||||
|
||||
public removeWorkspace(workspace: Workspace) {
|
||||
public removeWorkspace(workspaceOrId: Workspace | WorkspaceId) {
|
||||
const workspace = this.getById(typeof workspaceOrId == "string" ? workspaceOrId : workspaceOrId.id);
|
||||
if (!workspace) return;
|
||||
if (workspace.id === WorkspaceStore.defaultId) {
|
||||
throw new Error("Cannot remove default workspace")
|
||||
throw new Error("Cannot remove default workspace");
|
||||
}
|
||||
const workspaces = this.getAllWorkspaces()
|
||||
const index = workspaces.findIndex((w) => w.id === workspace.id)
|
||||
if (index !== -1) {
|
||||
const index = this.getIndexById(workspace.id);
|
||||
if (index > -1) {
|
||||
this.data.workspaces.splice(index, 1)
|
||||
clusterStore.removeClustersByWorkspace(workspace.id)
|
||||
workspaces.splice(index, 1)
|
||||
this.storeConfig.set("workspaces", workspaces)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,6 +55,7 @@
|
||||
|
||||
<script>
|
||||
import ClosePageButton from "@/_vue/components/common/ClosePageButton";
|
||||
import { workspaceStore } from "../../../common/workspace-store"
|
||||
|
||||
export default {
|
||||
name: 'EditWorkspacePage',
|
||||
@ -71,7 +72,7 @@ export default {
|
||||
},
|
||||
computed: {
|
||||
workspace: function() {
|
||||
return this.$store.getters.workspaceById(this.$route.params.id)
|
||||
return workspaceStore.getById(this.$route.params.id)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { MutationTree, ActionTree, GetterTree } from "vuex"
|
||||
import { workspaceStore, Workspace } from "../../../../common/workspace-store"
|
||||
import { ActionTree, GetterTree, MutationTree } from "vuex"
|
||||
import { Workspace, workspaceStore } from "../../../../common/workspace-store"
|
||||
|
||||
export interface WorkspaceState {
|
||||
workspaces: Array<Workspace>;
|
||||
@ -7,12 +7,11 @@ export interface WorkspaceState {
|
||||
}
|
||||
|
||||
const state: WorkspaceState = {
|
||||
workspaces: workspaceStore.getAllWorkspaces(),
|
||||
currentWorkspace: workspaceStore.getAllWorkspaces().find((w) => w.id === "default")
|
||||
workspaces: workspaceStore.workspaces,
|
||||
currentWorkspace: workspaceStore.workspaces.find((w) => w.id === "default")
|
||||
}
|
||||
|
||||
const actions: ActionTree<WorkspaceState, any> = {
|
||||
}
|
||||
const actions: ActionTree<WorkspaceState, any> = {}
|
||||
|
||||
const getters: GetterTree<WorkspaceState, any> = {
|
||||
workspaces: state => state.workspaces,
|
||||
@ -27,16 +26,16 @@ const mutations: MutationTree<WorkspaceState> = {
|
||||
state.currentWorkspace = workspace
|
||||
},
|
||||
addWorkspace(state, workspace: Workspace) {
|
||||
workspaceStore.saveWorkspace(workspace)
|
||||
state.workspaces = workspaceStore.getAllWorkspaces()
|
||||
workspaceStore.saveWorkspace({ ...workspace })
|
||||
state.workspaces = workspaceStore.workspaces
|
||||
},
|
||||
updateWorkspace(state, workspace: Workspace) {
|
||||
workspaceStore.saveWorkspace(workspace)
|
||||
state.workspaces = workspaceStore.getAllWorkspaces()
|
||||
workspaceStore.saveWorkspace({ ...workspace })
|
||||
state.workspaces = workspaceStore.workspaces
|
||||
},
|
||||
removeWorkspace(state, workspace: Workspace) {
|
||||
workspaceStore.removeWorkspace(workspace)
|
||||
state.workspaces = workspaceStore.getAllWorkspaces()
|
||||
workspaceStore.removeWorkspace(workspace.id)
|
||||
state.workspaces = workspaceStore.workspaces
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user