1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Preventing to create empty and existent workspaces

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2020-09-09 14:32:16 +03:00
parent 295333d055
commit 67e9e9b62a
3 changed files with 43 additions and 8 deletions

View File

@ -51,6 +51,10 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
return this.workspaces.get(id); return this.workspaces.get(id);
} }
getByName(name: string): Workspace {
return this.workspacesList.find(workspace => workspace.name === name);
}
@action @action
setActive(id = WorkspaceStore.defaultId, { redirectToLanding = true, resetActiveCluster = true } = {}) { setActive(id = WorkspaceStore.defaultId, { redirectToLanding = true, resetActiveCluster = true } = {}) {
if (id === this.currentWorkspaceId) return; if (id === this.currentWorkspaceId) return;
@ -67,14 +71,20 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
} }
@action @action
saveWorkspace(workspace: Workspace) { saveWorkspace(workspace: Workspace): { workspace?: Workspace, error?: string} {
const id = workspace.id; const { id, name } = workspace;
const existingWorkspace = this.getById(id); const existingWorkspace = this.getById(id);
if (existingWorkspace) { if (!name) {
Object.assign(existingWorkspace, workspace); return { error: "Workspace should has a name." };
} else {
this.workspaces.set(id, workspace);
} }
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 @action

View File

@ -92,6 +92,28 @@ describe("workspace store tests", () => {
expect(ws.workspaces.size).toBe(2); expect(ws.workspaces.size).toBe(2);
}) })
it("cannot create namespace with existent name", () => {
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
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<WorkspaceStore>();
ws.saveWorkspace({
id: "random",
name: "",
});
expect(ws.workspacesList.length).toBe(1); // default workspace only
})
}) })
describe("for a non-empty config", () => { describe("for a non-empty config", () => {

View File

@ -12,6 +12,7 @@ import { Icon } from "../icon";
import { Input } from "../input"; import { Input } from "../input";
import { cssNames, prevDefault } from "../../utils"; import { cssNames, prevDefault } from "../../utils";
import { Button } from "../button"; import { Button } from "../button";
import { Notifications } from "../notifications";
@observer @observer
export class Workspaces extends React.Component { export class Workspaces extends React.Component {
@ -41,10 +42,12 @@ export class Workspaces extends React.Component {
saveWorkspace = (id: WorkspaceId) => { saveWorkspace = (id: WorkspaceId) => {
const draft = toJS(this.editingWorkspaces.get(id)); const draft = toJS(this.editingWorkspaces.get(id));
if (draft) { const { error } = workspaceStore.saveWorkspace(draft);
if (!error) {
this.clearEditing(id); this.clearEditing(id);
workspaceStore.saveWorkspace(draft); return;
} }
Notifications.error(error);
} }
addWorkspace = () => { addWorkspace = () => {