From 786af2a96b93c05c0ef048f7e925b782d80f0e9e Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 20 Apr 2021 06:48:14 +0300 Subject: [PATCH] fixes Signed-off-by: Jari Kolehmainen --- src/common/__tests__/hotbar-store.test.ts | 7 +++++++ src/common/hotbar-store.ts | 21 +++++++++++++------ src/migrations/hotbar-store/5.0.0-alpha.2.ts | 2 +- .../components/hotbar/hotbar-add-command.tsx | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/common/__tests__/hotbar-store.test.ts b/src/common/__tests__/hotbar-store.test.ts index 40f38416c2..47339b6672 100644 --- a/src/common/__tests__/hotbar-store.test.ts +++ b/src/common/__tests__/hotbar-store.test.ts @@ -17,4 +17,11 @@ describe("HotbarStore", () => { expect(hotbarStore.hotbars.length).toEqual(1); }); }); + + describe("add", () => { + it("adds a hotbar", () => { + hotbarStore.add({ name: "hottest" }); + expect(hotbarStore.hotbars.length).toEqual(2); + }); + }); }); diff --git a/src/common/hotbar-store.ts b/src/common/hotbar-store.ts index 55f96a4550..20eb9d9ef5 100644 --- a/src/common/hotbar-store.ts +++ b/src/common/hotbar-store.ts @@ -1,7 +1,7 @@ import { action, comparer, observable, toJS } from "mobx"; import { BaseStore } from "./base-store"; import migrations from "../migrations/hotbar-store"; -import uuid from "uuid"; +import * as uuid from "uuid"; export interface HotbarItem { entity: { @@ -18,6 +18,12 @@ export interface Hotbar { items: HotbarItem[]; } +export interface HotbarCreateOptions { + id?: string; + name: string; + items?: HotbarItem[]; +} + export interface HotbarStoreModel { hotbars: Hotbar[]; activeHotbarId: string; @@ -86,18 +92,21 @@ export class HotbarStore extends BaseStore { return this.hotbars.find((hotbar) => hotbar.id === id); } - add(hotbar: Partial) { - hotbar.id = uuid.v4(); + add(data: HotbarCreateOptions) { + const { + id = uuid.v4(), + items = [], + name, + } = data; - if (!hotbar.items) { - hotbar.items = []; - } + const hotbar = { id, name, items }; this.hotbars.push(hotbar as Hotbar); return hotbar as Hotbar; } + @action remove(hotbar: Hotbar) { this.hotbars = this.hotbars.filter((h) => h !== hotbar); diff --git a/src/migrations/hotbar-store/5.0.0-alpha.2.ts b/src/migrations/hotbar-store/5.0.0-alpha.2.ts index 306879202e..060fa9f542 100644 --- a/src/migrations/hotbar-store/5.0.0-alpha.2.ts +++ b/src/migrations/hotbar-store/5.0.0-alpha.2.ts @@ -1,7 +1,7 @@ // Cleans up a store that had the state related data stored import { Hotbar } from "../../common/hotbar-store"; import { migration } from "../migration-wrapper"; -import uuid from "uuid"; +import * as uuid from "uuid"; export default migration({ version: "5.0.0-alpha.2", diff --git a/src/renderer/components/hotbar/hotbar-add-command.tsx b/src/renderer/components/hotbar/hotbar-add-command.tsx index 6922473ae8..b4cd57c626 100644 --- a/src/renderer/components/hotbar/hotbar-add-command.tsx +++ b/src/renderer/components/hotbar/hotbar-add-command.tsx @@ -6,7 +6,7 @@ import { Input, InputValidator } from "../input"; const uniqueHotbarName: InputValidator = { condition: ({ required }) => required, - message: () => `Hotbar with this name already exists`, + message: () => "Hotbar with this name already exists", validate: value => !hotbarStore.getByName(value), };