From d73cdcac6fe02b8da6735597445a7d953c583bb1 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Tue, 17 Nov 2020 07:32:13 +0300 Subject: [PATCH] Fix creating new workspace Signed-off-by: Alex Andreev --- src/common/__tests__/workspace-store.test.ts | 11 +++++++---- src/common/workspace-store.ts | 14 +++++++------- src/renderer/components/+workspaces/workspaces.tsx | 10 +++++++--- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/common/__tests__/workspace-store.test.ts b/src/common/__tests__/workspace-store.test.ts index 97edfa77cf..ef492bfcfa 100644 --- a/src/common/__tests__/workspace-store.test.ts +++ b/src/common/__tests__/workspace-store.test.ts @@ -38,15 +38,18 @@ describe("workspace store tests", () => { expect(() => ws.removeWorkspaceById(WorkspaceStore.defaultId)).toThrowError("Cannot remove"); }) - it("can update default workspace name", () => { + it("can update workspace description", () => { const ws = WorkspaceStore.getInstance(); - ws.addWorkspace(new Workspace({ - id: WorkspaceStore.defaultId, + const workspace = ws.addWorkspace(new Workspace({ + id: "foobar", name: "foobar", })); - expect(ws.currentWorkspace.name).toBe("foobar"); + workspace.description = "Foobar description"; + ws.updateWorkspace(workspace); + + expect(ws.getById("foobar").description).toBe("Foobar description"); }) it("can add workspaces", () => { diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index 97611a01d3..70f8d08fd2 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -153,20 +153,20 @@ export class WorkspaceStore extends BaseStore { @action addWorkspace(workspace: Workspace) { const { id, name } = workspace; - const existingWorkspace = this.getById(id); if (!name.trim() || this.getByName(name.trim())) { return; } - if (existingWorkspace) { - Object.assign(existingWorkspace, workspace); - appEventBus.emit({name: "workspace", action: "update"}) - } else { - appEventBus.emit({name: "workspace", action: "add"}) - } this.workspaces.set(id, workspace); + appEventBus.emit({name: "workspace", action: "add"}) return workspace; } + @action + updateWorkspace(workspace: Workspace) { + this.workspaces.set(workspace.id, workspace); + appEventBus.emit({name: "workspace", action: "update"}); + } + @action removeWorkspace(workspace: Workspace) { this.removeWorkspaceById(workspace.id) diff --git a/src/renderer/components/+workspaces/workspaces.tsx b/src/renderer/components/+workspaces/workspaces.tsx index 4439a9ea08..8bddf48c24 100644 --- a/src/renderer/components/+workspaces/workspaces.tsx +++ b/src/renderer/components/+workspaces/workspaces.tsx @@ -45,9 +45,13 @@ export class Workspaces extends React.Component { } saveWorkspace = (id: WorkspaceId) => { - const draft = toJS(this.editingWorkspaces.get(id)); - const workspace = workspaceStore.addWorkspace(draft); - if (workspace) { + const workspace = new Workspace(this.editingWorkspaces.get(id)); + if (workspaceStore.getById(id)) { + workspaceStore.updateWorkspace(workspace); + this.clearEditing(id); + return; + } + if (workspaceStore.addWorkspace(workspace)) { this.clearEditing(id); } }