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