From 182ce921d883556cf505d6d2eefa604180752c28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Wed, 12 Mar 2025 12:36:01 +0100
Subject: [PATCH] MAINT: refactor creating destination drop down

---
 .../forms/loan-forms/borrow_checkout.html     | 156 ++++++++----------
 .../caosdb-webui/src/ext/js/box_loan.js       |  58 ++-----
 2 files changed, 88 insertions(+), 126 deletions(-)

diff --git a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
index ff419c7..f183fb0 100644
--- a/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
+++ b/loan-custom/caosdb-server/caosdb-webui/src/ext/html/forms/loan-forms/borrow_checkout.html
@@ -1,95 +1,79 @@
 <!DOCTYPE html>
 <html>
-
-<body>
-  <div id="caosdb-form">
-    <div class="row" id="checkout-form-{boxid}" style="margin:0px">
-      <div id="caosdb-f-borrow-all-form-modal" class="modal fade" style="max-width:900px" role="dialog">
-        <div class="modal-dialog">
-          <div class="modal-content">
-            <div class="modal-header">
-              <h4 class="modal-title">Borrow all bookmarked items</h4>
+  <body>
+    <div id="caosdb-form">
+      <div class="row" id="checkout-form-{boxid}" style="margin:0px">
+        <div class="col-lg-12">
+        <h2>Borrow Box</h2>
+        <form class="caosdb-awi-box-checkout-form" autocomplete="on">
+          <div>
+            <h5>Please enter your full name and your email address. If you are doing this for the first time, a new user record will be created.</h5>
+          </div>
+          <div class="row caosdb-form-row">
+            <div class="col-sm-4">
+              <div class="form-group">
+                <label>First Name:
+                  <input name="first-name" type="name" class="form-control" id="first-name" placeholder="Enter name" required="required" value="{first_name}" />
+                </label>
+              </div>
             </div>
-            <div class="modal-body  ">
-              <form class="caosdb-awi-box-checkout-form" autocomplete="on">
-                <div>
-                  Please enter your full name and your email address. If you are doing this for the first time, a
-                  new user record will be created.
-                </div>
-                <div class="row caosdb-form-row">
-                  <div class="col-sm-4">
-                    <div class="form-group">
-                      <label>First Name:
-                        <input name="first-name" type="name" class="form-control" id="first-name"
-                          placeholder="Enter name" required="required" value="{first_name}" />
-                      </label>
-                    </div>
-                  </div>
-                  <div class="col-sm-4">
-                    <div class="form-group">
-                      <label>Last Name:
-                        <input name="last-name" type="name" class="form-control" id="last-name" placeholder="Enter name"
-                          required="required" value="{last_name}" />
-                      </label>
-                    </div>
-                  </div>
-                  <div class="col-sm-4">
-                    <div class="form-group">
-                      <label>Email:
-                        <input name="email" type="email" class="form-control" id="email" placeholder="Enter email"
-                          required="required" value="{email}" />
-                      </label>
-                    </div>
-                  </div>
-                </div>
-                <div class="row caosdb-form-row">
-                  <div class="col-sm-4">
-                    <div class="form-group">
-                      <label>Expected Return Date:
-                        <input name="expectedreturn" min="{mindate}" type="date" class="form-control"
-                          id="expected-return" required="required" value="{expected_return_date}" />
-                      </label>
-                    </div>
-                  </div>
-                  <div class="col-sm-4">
-                    <div class="form-group">
-                      <label>Destination:
-                        <input id="loan-destination" required="required" />
-                      </label>
-                    </div>
-                  </div>
-                </div>
-                <div class="row caosdb-form-row">
-                  <div class="col-sm-12">
-                    <div class="form-group">
-                      <label for="loan-comment" style="margin-bottom:0px">Purpose:
-                        <textarea id="loan-comment" class="form-control" rows="4" style="width:100%"
-                          placeholder="Please indicate the purpose of the loan.">{purpose}</textarea>
-                      </label>
-                    </div>
-                  </div>
-                </div>
-                <div class="row caosdb-form-row">
-                  <div class="col-sm-12">
-                    <div class="form-check">
-                      <label class="form-check-label">Are you going to completely exhaust the contents of this box?
-                        <input type="checkbox" class="form-check-input" id="exhaust-contents" value="no" />
-                      </label>
-                    </div>
-                  </div>
-                </div>
-              </form>
+            <div class="col-sm-4">
+              <div class="form-group">
+                <label>Last Name:
+                  <input name="last-name" type="name" class="form-control" id="last-name" placeholder="Enter name" required="required" value="{last_name}" />
+                </label>
+              </div>
             </div>
-            <div class="modal-footer">
-              <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
-              <button type="submit" class="btn btn-secondary" form="borrow-all-form">Submit</button>
+            <div class="col-sm-4">
+              <div class="form-group">
+                <label>Email:
+                  <input name="email" type="email" class="form-control" id="email" placeholder="Enter email" required="required" value="{email}" />
+                </label>
+              </div>
             </div>
           </div>
-        </div>
-
+          <div class="row caosdb-form-row">
+            <div class="col-sm-4">
+              <div class="form-group">
+                <label>Expected Return Date:
+                    <input name="expectedreturn" min="{mindate}" type="date" class="form-control" id="expected-return" required="required" value="{expected_return_date}" />
+                </label>
+              </div>
+            </div>
+            <div class="col-sm-4">
+              <div class="form-group">
+                <label>Destination:
+                  <input id="loan-destination" required="required"/>
+                </label>
+              </div>
+            </div>
+          </div>
+          <div class="row caosdb-form-row">
+            <div class="col-sm-12">
+              <div class="form-group">
+                  <label for="loan-comment" style="margin-bottom:0px">Purpose:
+                  <textarea id="loan-comment" class="form-control" rows="4" style="width:100%"
+                            placeholder="Please indicate the purpose of the loan.">{purpose}</textarea>
+                  </label>
+              </div>
+            </div>
+          </div>
+          <div class="row caosdb-form-row">
+            <div class="col-sm-12">
+              <div class="form-check">
+                <label class="form-check-label">Are you going to completely exhaust the contents of this box?
+                  <input type="checkbox" class="form-check-input" id="exhaust-contents" value="no" />
+                </label>
+              </div>
+            </div>
+          </div>
+          <div class="text-right">
+            <button type="submit" class="btn btn-secondary">Submit</button>
+          </div>
+        </form>
+      </div>
       </div>
     </div>
-  </div>
-</body>
+  </body>
+</html>
 
-</html>
\ No newline at end of file
diff --git a/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js b/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
index f91a7f2..fdeb511 100644
--- a/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
+++ b/loan-custom/caosdb-server/caosdb-webui/src/ext/js/box_loan.js
@@ -19,9 +19,6 @@
  *
  */
 
-
-
-
 /**
  * Return the formatted date of today.
  */
@@ -51,9 +48,6 @@ var getHTMLForm = async function(pageName, variables) {
 var box_loan = function(logger, box_loan_config) {
 
     const datamodel = box_loan_config.datamodel;
-
-
-
     const _dismiss_button = '<button class="btn btn-secondary box-loan-btn">OK</button>'
     const _server_did_not_respond = "The server did not respond. Please reload the page.";
     const _return_box_button = `<a title="Return ${datamodel.box}." class="btn btn-link box-loan-btn">Return ${datamodel.box}</a>`;
@@ -381,19 +375,10 @@ var box_loan = function(logger, box_loan_config) {
         const but = $(_borrow_box_button);
         get_actions_panel(box).append(but);
 
-        const config = {
-            type: "reference_drop_down",
-            name: "destination",
-            label: "",
-            required: true,
-            query: `FIND Record ${datamodel.location}`,
-            make_desc: getEntityName,
-        }
-        const dd = form_elements.make_form_field(config);
         const form = await _add_form(box, generate_form_borrow_checkout, borrow_function);
-        $(dd).find(".col-sm-9").removeClass("col-sm-9");
-        $(dd).find(".col-sm-3").removeClass("col-sm-3");
-        $(form).find("#loan-destination").replaceWith(dd);
+   
+        replace_loan_destination(form);
+
         $(form).find("label").css("display", "block");
 
         _init_validator(form);
@@ -660,12 +645,7 @@ var box_loan = function(logger, box_loan_config) {
         show_result(actions_panel, result, box);
     }
 
-    var add_menu_entry = async function () {
- 
-        if ($("#caosdb-f-bookmarks-borrow-all").length > 0) {
-            return;
-        }
-
+    var replace_loan_destination = async function(node) {
         const config = {
             type: "reference_drop_down",
             name: "destination",
@@ -677,9 +657,18 @@ var box_loan = function(logger, box_loan_config) {
         const dd = form_elements.make_form_field(config);
         $(dd).find(".col-sm-9").removeClass("col-sm-9");
         $(dd).find(".col-sm-3").removeClass("col-sm-3");
+        $(node).find("#loan-destination").replaceWith(dd);
+    }
+
+    var add_menu_entry = async function () {
+ 
+        if ($("#caosdb-f-bookmarks-borrow-all").length > 0) {
+            return;
+        }
+
         var modal = $(await generate_form_borrow_checkout(undefined, "loan-forms/borrow_all_bookmarked")).find(".modal");
 
-        $(modal).find("#loan-destination").replaceWith(dd);
+        replace_loan_destination(modal);
         $(modal).find("label").css("display", "block");
 
         $('body').append(modal);
@@ -744,7 +733,6 @@ var box_loan = function(logger, box_loan_config) {
             exp_return = "";
         }
         formhtml = formhtml || 'loan-forms/borrow_checkout';
-        console.log("formhtml", formhtml);
         return getHTMLForm(formhtml, {
             boxid: boxid,
             first_name: firstname,
@@ -768,14 +756,11 @@ var box_loan = function(logger, box_loan_config) {
      * currentlocation (pre-filled)
      * comment
      */
-    var generate_return_form = async function(box) {
-        const loan = (await get_active_loans(box.id))[0];
+    var generate_return_form = async function(boxid) {
+        const loan = (await get_active_loans(boxid))[0];
         if (typeof loan === "undefined") {
             // no loan found
-
-            console.log(loan);
-            console.log(box);
-            logger.error("No loan found for box", box);
+            logger.error("No loan found for box", boxid);
             return ;
         }
         const borrower = (await retrieve(getProperty(loan, "Borrower", case_sensitive = false)))[0];
@@ -794,7 +779,7 @@ var box_loan = function(logger, box_loan_config) {
         var cur_loc = getProperty(loan, "destination", case_sensitive = false);
 
         return getHTMLForm("loan-forms/return_box", {
-            boxid: getEntityID(box),
+            boxid: boxid,
             first_name: first_name,
             last_name: last_name,
             email: email,
@@ -804,8 +789,6 @@ var box_loan = function(logger, box_loan_config) {
         });
     }
 
-
-
     var init = function(boxes) {
         const init_boxes = boxes || $(".caosdb-entity-panel")
             .not("[data-version-successor]")
@@ -816,11 +799,6 @@ var box_loan = function(logger, box_loan_config) {
         add_menu_entry();
         add_buttons(init_boxes);
     }
-    const is_authorized = function() {
-        // return true //for testing
-        return isAuthenticated() //&& (userHasRole("stockmanager") || userHasRole("administration"))
-    }
-
 
     return {
         init: init,
-- 
GitLab