mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Refactoring of save workspace process
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
af05b6761d
commit
58bd82294c
@ -71,20 +71,17 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
saveWorkspace(workspace: Workspace): { workspace?: Workspace, error?: string} {
|
saveWorkspace(workspace: Workspace) {
|
||||||
const { id, name } = workspace;
|
const { id, name } = workspace;
|
||||||
const existingWorkspace = this.getById(id);
|
const existingWorkspace = this.getById(id);
|
||||||
if (!name) {
|
if (!name.trim() || this.getByName(name.trim())) {
|
||||||
return { error: "Workspace should has a name." };
|
return;
|
||||||
}
|
|
||||||
if (this.getByName(name)) {
|
|
||||||
return { error: `Workspace '${name}' already exist.` };
|
|
||||||
}
|
}
|
||||||
if (existingWorkspace) {
|
if (existingWorkspace) {
|
||||||
return { workspace: Object.assign(existingWorkspace, workspace) };
|
Object.assign(existingWorkspace, workspace);
|
||||||
}
|
}
|
||||||
this.workspaces.set(id, workspace);
|
this.workspaces.set(id, workspace);
|
||||||
return { workspace };
|
return workspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
|||||||
@ -93,7 +93,7 @@ describe("workspace store tests", () => {
|
|||||||
expect(ws.workspaces.size).toBe(2);
|
expect(ws.workspaces.size).toBe(2);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("cannot create namespace with existent name", () => {
|
it("cannot create workspace with existent name", () => {
|
||||||
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
ws.saveWorkspace({
|
ws.saveWorkspace({
|
||||||
@ -104,7 +104,7 @@ describe("workspace store tests", () => {
|
|||||||
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||||
})
|
})
|
||||||
|
|
||||||
it("cannot create namespace with empty name", () => {
|
it("cannot create workspace with empty name", () => {
|
||||||
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
ws.saveWorkspace({
|
ws.saveWorkspace({
|
||||||
@ -114,6 +114,28 @@ describe("workspace store tests", () => {
|
|||||||
|
|
||||||
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("cannot create workspace with ' ' name", () => {
|
||||||
|
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
ws.saveWorkspace({
|
||||||
|
id: "random",
|
||||||
|
name: " ",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||||
|
})
|
||||||
|
|
||||||
|
it("trim workspace name", () => {
|
||||||
|
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
ws.saveWorkspace({
|
||||||
|
id: "random",
|
||||||
|
name: "default ",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("for a non-empty config", () => {
|
describe("for a non-empty config", () => {
|
||||||
|
|||||||
@ -12,7 +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";
|
import { isRequired, Validator } from "../input/input.validators";
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class Workspaces extends React.Component {
|
export class Workspaces extends React.Component {
|
||||||
@ -42,12 +42,10 @@ 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));
|
||||||
const { error } = workspaceStore.saveWorkspace(draft);
|
const workspace = workspaceStore.saveWorkspace(draft);
|
||||||
if (!error) {
|
if (workspace) {
|
||||||
this.clearEditing(id);
|
this.clearEditing(id);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
Notifications.error(error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addWorkspace = () => {
|
addWorkspace = () => {
|
||||||
@ -95,6 +93,9 @@ export class Workspaces extends React.Component {
|
|||||||
|
|
||||||
onInputKeypress = (evt: React.KeyboardEvent<any>, workspaceId: WorkspaceId) => {
|
onInputKeypress = (evt: React.KeyboardEvent<any>, workspaceId: WorkspaceId) => {
|
||||||
if (evt.key == 'Enter') {
|
if (evt.key == 'Enter') {
|
||||||
|
// Trigget input validation
|
||||||
|
evt.currentTarget.blur();
|
||||||
|
evt.currentTarget.focus();
|
||||||
this.saveWorkspace(workspaceId);
|
this.saveWorkspace(workspaceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,11 +112,15 @@ export class Workspaces extends React.Component {
|
|||||||
const isDefault = workspaceStore.isDefault(workspaceId);
|
const isDefault = workspaceStore.isDefault(workspaceId);
|
||||||
const isEditing = this.editingWorkspaces.has(workspaceId);
|
const isEditing = this.editingWorkspaces.has(workspaceId);
|
||||||
const editingWorkspace = this.editingWorkspaces.get(workspaceId);
|
const editingWorkspace = this.editingWorkspaces.get(workspaceId);
|
||||||
const className = cssNames("workspace flex gaps align-center", {
|
const className = cssNames("workspace flex gaps", {
|
||||||
active: isActive,
|
active: isActive,
|
||||||
editing: isEditing,
|
editing: isEditing,
|
||||||
default: isDefault,
|
default: isDefault,
|
||||||
});
|
});
|
||||||
|
const existenceValidator: Validator = {
|
||||||
|
message: () => `Workspace '${name}' already exists`,
|
||||||
|
validate: value => !workspaceStore.getByName(value.trim())
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div key={workspaceId} className={className}>
|
<div key={workspaceId} className={className}>
|
||||||
{!isEditing && (
|
{!isEditing && (
|
||||||
@ -149,6 +154,7 @@ export class Workspaces extends React.Component {
|
|||||||
value={editingWorkspace.name}
|
value={editingWorkspace.name}
|
||||||
onChange={v => editingWorkspace.name = v}
|
onChange={v => editingWorkspace.name = v}
|
||||||
onKeyPress={(e) => this.onInputKeypress(e, workspaceId)}
|
onKeyPress={(e) => this.onInputKeypress(e, workspaceId)}
|
||||||
|
validators={[isRequired, existenceValidator]}
|
||||||
autoFocus
|
autoFocus
|
||||||
/>
|
/>
|
||||||
<Input
|
<Input
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user