1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-04-20 06:48:14 +03:00
parent 437731b67f
commit 786af2a96b
4 changed files with 24 additions and 8 deletions

View File

@ -17,4 +17,11 @@ describe("HotbarStore", () => {
expect(hotbarStore.hotbars.length).toEqual(1); expect(hotbarStore.hotbars.length).toEqual(1);
}); });
}); });
describe("add", () => {
it("adds a hotbar", () => {
hotbarStore.add({ name: "hottest" });
expect(hotbarStore.hotbars.length).toEqual(2);
});
});
}); });

View File

@ -1,7 +1,7 @@
import { action, comparer, observable, toJS } from "mobx"; import { action, comparer, observable, toJS } from "mobx";
import { BaseStore } from "./base-store"; import { BaseStore } from "./base-store";
import migrations from "../migrations/hotbar-store"; import migrations from "../migrations/hotbar-store";
import uuid from "uuid"; import * as uuid from "uuid";
export interface HotbarItem { export interface HotbarItem {
entity: { entity: {
@ -18,6 +18,12 @@ export interface Hotbar {
items: HotbarItem[]; items: HotbarItem[];
} }
export interface HotbarCreateOptions {
id?: string;
name: string;
items?: HotbarItem[];
}
export interface HotbarStoreModel { export interface HotbarStoreModel {
hotbars: Hotbar[]; hotbars: Hotbar[];
activeHotbarId: string; activeHotbarId: string;
@ -86,18 +92,21 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
return this.hotbars.find((hotbar) => hotbar.id === id); return this.hotbars.find((hotbar) => hotbar.id === id);
} }
add(hotbar: Partial<Hotbar>) { add(data: HotbarCreateOptions) {
hotbar.id = uuid.v4(); const {
id = uuid.v4(),
items = [],
name,
} = data;
if (!hotbar.items) { const hotbar = { id, name, items };
hotbar.items = [];
}
this.hotbars.push(hotbar as Hotbar); this.hotbars.push(hotbar as Hotbar);
return hotbar as Hotbar; return hotbar as Hotbar;
} }
@action
remove(hotbar: Hotbar) { remove(hotbar: Hotbar) {
this.hotbars = this.hotbars.filter((h) => h !== hotbar); this.hotbars = this.hotbars.filter((h) => h !== hotbar);

View File

@ -1,7 +1,7 @@
// Cleans up a store that had the state related data stored // Cleans up a store that had the state related data stored
import { Hotbar } from "../../common/hotbar-store"; import { Hotbar } from "../../common/hotbar-store";
import { migration } from "../migration-wrapper"; import { migration } from "../migration-wrapper";
import uuid from "uuid"; import * as uuid from "uuid";
export default migration({ export default migration({
version: "5.0.0-alpha.2", version: "5.0.0-alpha.2",

View File

@ -6,7 +6,7 @@ import { Input, InputValidator } from "../input";
const uniqueHotbarName: InputValidator = { const uniqueHotbarName: InputValidator = {
condition: ({ required }) => required, condition: ({ required }) => required,
message: () => `Hotbar with this name already exists`, message: () => "Hotbar with this name already exists",
validate: value => !hotbarStore.getByName(value), validate: value => !hotbarStore.getByName(value),
}; };