From 714efeb4ab315292e4f04adef4f9582e7fe979b0 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Thu, 7 Jan 2021 11:55:54 +0100
Subject: [PATCH] prevent error when trying to manage active effects on an
 owned item

---
 src/module/item/item-sheet.ts           | 44 ++++++++++++++++---------
 src/templates/item/partials/effects.hbs |  8 +++--
 2 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/src/module/item/item-sheet.ts b/src/module/item/item-sheet.ts
index 8670dc3e..b9bc1272 100644
--- a/src/module/item/item-sheet.ts
+++ b/src/module/item/item-sheet.ts
@@ -52,6 +52,9 @@ export class DS4ItemSheet extends ItemSheet<DS4ItemDataType, DS4Item> {
         return position;
     }
 
+    private readonly ownedItemActiveEffectWarning =
+        "Managing Active Effects within an Owned Item is not currently supported and will be added in a subsequent update.";
+
     /* -------------------------------------------- */
 
     /** @override */
@@ -60,29 +63,38 @@ export class DS4ItemSheet extends ItemSheet<DS4ItemDataType, DS4Item> {
 
         if (!this.options.editable) return;
 
-        html.find(".effect-create").on("click", this._onEffectCreate.bind(this));
-
-        html.find(".effect-edit").on("click", (ev) => {
-            const li = $(ev.currentTarget).parents(".effect");
-            console.log(li.data("effectId"));
-            const effect = this.item.effects.get(li.data("effectId"));
-            effect.sheet.render(true);
-        });
-
-        html.find(".effect-delete").on("click", async (ev) => {
-            const li = $(ev.currentTarget).parents(".effect");
-            await this.item.deleteEmbeddedEntity("ActiveEffect", li.data("effectId"));
-        });
+        html.find(".effect-control").on("click", this._onManageActiveEffect.bind(this));
     }
 
     /**
-     * Handle creating a new ActiveEffect for the item using initial data defined in the HTML dataset
+     * Handle management of ActiveEffects.
      * @param {Event} event   The originating click event
-     * @private
      */
-    private async _onEffectCreate(event: JQuery.ClickEvent): Promise<unknown> {
+    private async _onManageActiveEffect(event: JQuery.ClickEvent): Promise<unknown> {
         event.preventDefault();
 
+        if (this.item.isOwned) {
+            return ui.notifications.warn(this.ownedItemActiveEffectWarning);
+        }
+        const a = event.currentTarget;
+        const li = $(a).parents(".effect");
+
+        switch (a.dataset["action"]) {
+            case "create":
+                return this._createActiveEffect();
+            case "edit":
+                const effect = this.item.effects.get(li.data("effectId"));
+                return effect.sheet.render(true);
+            case "delete": {
+                return this.item.deleteEmbeddedEntity("ActiveEffect", li.data("effectId"));
+            }
+        }
+    }
+
+    /**
+     * Create a new ActiveEffect for the item using default data.
+     */
+    private async _createActiveEffect(): Promise<unknown> {
         const label = `New Effect`;
 
         const createData = {
diff --git a/src/templates/item/partials/effects.hbs b/src/templates/item/partials/effects.hbs
index 819290d6..b4cf02bc 100644
--- a/src/templates/item/partials/effects.hbs
+++ b/src/templates/item/partials/effects.hbs
@@ -5,7 +5,7 @@
             <div class="effect-image"></div>
             <div class="effect-name">Name</div>
             <div class="effect-controls">
-                <a class="effect-control effect-create" title="Create Effect"><i
+                <a class="effect-control effect-create" data-action="create" title="Create Effect"><i
                         class="fas fa-plus"></i> Add effect</a>
             </div>
         </li>
@@ -13,8 +13,10 @@
         <li class="effect flexrow" data-effect-id="{{effect._id}}">
             <h4 class="effect-name">{{effect.label}}</h4>
             <div class="effect-controls">
-                <a class="effect-control effect-edit" title="Edit Effect"><i class="fas fa-edit"></i></a>
-                <a class="effect-control effect-delete" title="Delete Effect"><i class="fas fa-trash"></i></a>
+                <a class="effect-control effect-edit" data-action="edit" title="Edit Effect"><i
+                        class="fas fa-edit"></i></a>
+                <a class="effect-control effect-delete" data-action="delete" title="Delete Effect"><i
+                        class="fas fa-trash"></i></a>
             </div>
         </li>
         {{/each}}