diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index 43afb0d5c6..eec8e8b7e2 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -51,6 +51,10 @@ export class WorkspaceStore extends BaseStore { return this.workspaces.get(id); } + getByName(name: string): Workspace { + return this.workspacesList.find(workspace => workspace.name === name); + } + @action setActive(id = WorkspaceStore.defaultId, { redirectToLanding = true, resetActiveCluster = true } = {}) { if (id === this.currentWorkspaceId) return; @@ -67,14 +71,20 @@ export class WorkspaceStore extends BaseStore { } @action - saveWorkspace(workspace: Workspace) { - const id = workspace.id; + saveWorkspace(workspace: Workspace): { workspace?: Workspace, error?: string} { + const { id, name } = workspace; const existingWorkspace = this.getById(id); - if (existingWorkspace) { - Object.assign(existingWorkspace, workspace); - } else { - this.workspaces.set(id, workspace); + if (!name) { + return { error: "Workspace should has a name." }; } + if (this.getByName(name)) { + return { error: `Workspace '${name}' already exist.` }; + } + if (existingWorkspace) { + return { workspace: Object.assign(existingWorkspace, workspace) }; + } + this.workspaces.set(id, workspace); + return { workspace }; } @action diff --git a/src/common/workspace-store_test.ts b/src/common/workspace-store_test.ts index 55e9672663..b80d22c148 100644 --- a/src/common/workspace-store_test.ts +++ b/src/common/workspace-store_test.ts @@ -92,6 +92,28 @@ describe("workspace store tests", () => { expect(ws.workspaces.size).toBe(2); }) + + it("cannot create namespace with existent name", () => { + const ws = WorkspaceStore.getInstance(); + + ws.saveWorkspace({ + id: "someid", + name: "default", + }); + + expect(ws.workspacesList.length).toBe(1); // default workspace only + }) + + it("cannot create namespace with empty name", () => { + const ws = WorkspaceStore.getInstance(); + + ws.saveWorkspace({ + id: "random", + name: "", + }); + + expect(ws.workspacesList.length).toBe(1); // default workspace only + }) }) describe("for a non-empty config", () => { diff --git a/src/renderer/components/+workspaces/workspaces.tsx b/src/renderer/components/+workspaces/workspaces.tsx index 96007a95fa..3bf37b59f6 100644 --- a/src/renderer/components/+workspaces/workspaces.tsx +++ b/src/renderer/components/+workspaces/workspaces.tsx @@ -12,6 +12,7 @@ import { Icon } from "../icon"; import { Input } from "../input"; import { cssNames, prevDefault } from "../../utils"; import { Button } from "../button"; +import { Notifications } from "../notifications"; @observer export class Workspaces extends React.Component { @@ -41,10 +42,12 @@ export class Workspaces extends React.Component { saveWorkspace = (id: WorkspaceId) => { const draft = toJS(this.editingWorkspaces.get(id)); - if (draft) { + const { error } = workspaceStore.saveWorkspace(draft); + if (!error) { this.clearEditing(id); - workspaceStore.saveWorkspace(draft); + return; } + Notifications.error(error); } addWorkspace = () => {