From b999e8b51014d2e66d5ddd5fe9d676be3fc8497e Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 8 Oct 2021 14:44:26 +0200
Subject: [PATCH] autocomplete on refered by should be filled only when field
 is available

---
 CHANGELOG                             |  2 ++
 smash/web/templates/subjects/add.html | 14 ++++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2505ec5e..972e5b1c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -17,6 +17,8 @@ smasch (1.1.0~alpha.0-1) unstable; urgency=low
     (#473)
   * bug fix: importing from csv file that contained unmapped column did not
     work (#414)
+  * bug fix: unchecked 'refered by' in study configuration raised errors when
+    adding new subject (#468)
   * bug fix: privacy notice files were not removed when policy was removed
   * bug fix: holidays were wrongly encoded and displayed when they span
     multiple days (#430, #429)
diff --git a/smash/web/templates/subjects/add.html b/smash/web/templates/subjects/add.html
index 07b9fdb9..eeae1a8a 100644
--- a/smash/web/templates/subjects/add.html
+++ b/smash/web/templates/subjects/add.html
@@ -102,13 +102,18 @@
     <script src="{% static 'npm/awesomplete/awesomplete.min.js' %}"></script>
     <script>
         $(document).ready(function () {
-
-            var cityInput = document.querySelector("#id_subject-city");
             var referralInput = document.querySelector("#id_study_subject-referral");
+            if (referralInput !== null) {
+                $.get("{% url 'web.api.referrals' %}", function (data) {
+                    new Awesomplete(referralInput).list = data.referrals;
+                });
+            }
+            
+            var cityInput = document.querySelector("#id_subject-city");
             var lastNameInput = $("input[name='subject-last_name']");
             var firstNameInput = $("input[name='subject-first_name']");
 
-            if (cityInput === null || referralInput === null || lastNameInput.length === 0 || firstNameInput.length === 0) {
+            if (cityInput === null || lastNameInput.length === 0 || firstNameInput.length === 0) {
                 alert("Internal error. Contact administrators.");
             }
 
@@ -116,9 +121,6 @@
             $.get("{% url 'web.api.cities' %}", function (data) {
                 new Awesomplete(cityInput).list = data.cities;
             });
-            $.get("{% url 'web.api.referrals' %}", function (data) {
-                new Awesomplete(referralInput).list = data.referrals;
-            });
 
 
             var checkDuplicates = function (e) {
-- 
GitLab