From e2b8b51db8d4a5e37a77df6dd936092c10b4f9d2 Mon Sep 17 00:00:00 2001
From: Gregoo <versatile.mailbox@gmail.com>
Date: Wed, 29 Jan 2025 15:58:41 +0100
Subject: [PATCH] Move dynamic input to BrickChanger

---
 bricktracker/part.py      | 4 +++-
 bricktracker/views/set.py | 6 ++----
 static/scripts/changer.js | 8 ++++++--
 templates/macro/form.html | 2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/bricktracker/part.py b/bricktracker/part.py
index c847db5..d81bb85 100644
--- a/bricktracker/part.py
+++ b/bricktracker/part.py
@@ -143,7 +143,9 @@ class BrickPart(RebrickablePart):
         return self
 
     # Update the missing part
-    def update_missing(self, missing: Any, /) -> None:
+    def update_missing(self, json: Any | None, /) -> None:
+        missing = json.get('value', '')  # type: ignore
+
         # We need a positive integer
         try:
             missing = int(missing)
diff --git a/bricktracker/views/set.py b/bricktracker/views/set.py
index 44eabed..d82896c 100644
--- a/bricktracker/views/set.py
+++ b/bricktracker/views/set.py
@@ -137,9 +137,7 @@ def missing_part(
         minifigure=brickminifigure,
     )
 
-    missing = request.json.get('missing', '')  # type: ignore
-
-    brickpart.update_missing(missing)
+    brickpart.update_missing(request.json)
 
     # Info
     logger.info('Set {set} ({id}): updated part ({part} color: {color}, spare: {spare}, minifigure: {figure}) missing count to {missing}'.format(  # noqa: E501
@@ -152,7 +150,7 @@ def missing_part(
         missing=brickpart.fields.missing,
     ))
 
-    return jsonify({'missing': missing})
+    return jsonify({'missing': brickpart.fields.missing})
 
 
 # Refresh a set
diff --git a/static/scripts/changer.js b/static/scripts/changer.js
index 224e24b..e3a32f8 100644
--- a/static/scripts/changer.js
+++ b/static/scripts/changer.js
@@ -15,8 +15,10 @@ class BrickChanger {
         // Register an event depending on the type
         if (this.html_type == "checkbox") {
             var listener = "change";
+        } else if(this.html_type == "text") {
+            var listener = "change";
         } else {
-            var listener = "click";
+            throw Error("Unsupported input type for BrickChanger");
         }
 
         this.html_element.addEventListener(listener, ((changer) => (e) => {
@@ -70,8 +72,10 @@ class BrickChanger {
             // Grab the value depending on the type
             if (this.html_type == "checkbox") {
                 var value = this.html_element.checked;
-            } else {
+            } else if(this.html_type == "text") {
                 var value = this.html_element.value;
+            } else {
+                throw Error("Unsupported input type for BrickChanger");
             }
 
             const response = await fetch(this.url, {
diff --git a/templates/macro/form.html b/templates/macro/form.html
index f88b673..4844822 100644
--- a/templates/macro/form.html
+++ b/templates/macro/form.html
@@ -24,7 +24,7 @@
     <div class="input-group">
       <input class="form-control form-control-sm flex-shrink-1" type="text" id="{{ prefix }}-{{ id }}" value="{% if value %}{{ value }}{% endif %}"
   {% if g.login.is_authenticated() %}
-      onchange="change_part_missing_amount(this, '{{ id }}', '{{ html_id }}', '{{ url }}')"
+        data-changer-id="{{ id }}" data-changer-prefix="{{ prefix }}" data-changer-url="{{ url }}"
   {% else %}
       disabled
   {% endif %}