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);
}
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<WorkspaceStoreModel> {
}
@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

View File

@ -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<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", () => {

View File

@ -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 = () => {