From 25c8ff51400401ce8ed4ffb085db5a4f9614409f Mon Sep 17 00:00:00 2001
From: Benoit Alessandroni <benoit@happy-dev.fr>
Date: Fri, 13 Sep 2019 17:23:18 +0200
Subject: [PATCH] Fixing the format filter

---
 src/includes/entrepreneur/resources/list.pug |  6 ++--
 src/scripts/coopstarter.js                   | 30 ++++++++++++--------
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/includes/entrepreneur/resources/list.pug b/src/includes/entrepreneur/resources/list.pug
index e7cb36de..a91cde02 100644
--- a/src/includes/entrepreneur/resources/list.pug
+++ b/src/includes/entrepreneur/resources/list.pug
@@ -94,10 +94,8 @@ container.block_list.flex.flex_espace
         search-range-format=`${endpoints.formats}`
 
         search-widget-search_for_a_resource="hidden-widget"
-
- 
-        search-multiple-format='sib-form-dropdown'
-        search-widget-format='sib-form-auto-completion'
+        search-widget-more_criterias_hidden="hidden-widget"
+        search-widget-format='sib-form-dropdown'
  
         widget-name='cs-display-property'
         widget-country='cs-display-property'
diff --git a/src/scripts/coopstarter.js b/src/scripts/coopstarter.js
index 551622dd..aef0b5ff 100644
--- a/src/scripts/coopstarter.js
+++ b/src/scripts/coopstarter.js
@@ -123,24 +123,30 @@ jQuery(document).ready(function($) {
         );
 
 
-
+        //TODO: Every time the format field in the search form changes
+        //Iterate through all 5 filters form associated with the sib-displays
+        //Iterate through their inputs to get the format one
+        //Change the value and the selected option of this field
+        //Reload the component to trigger the filter 
         format_field_search.onchange = function() {
-          console.log("format_field_search", format_field_search);
+          let option_selected = format_field_search.querySelector(
+            "option:checked"
+          );
+
           format_hidden_field.forEach(function(select_hidden) {
             options_hidden = select_hidden.getElementsByTagName("option");
-
             for (let option_hidden of options_hidden) {
-
-              option_selected = format_field_search.querySelector(
-                "option:checked"
-              );
-
+              //Removing the selected attribute from previous selection
+              option_hidden.removeAttribute("selected");
               if (option_hidden.value == option_selected.value) {
-                console.log("option_selected", option_selected )
-                option_hidden.setAttribute("checked", "checked");
-                console.log("option_hidden", option_hidden )
+                //Actually selecting the option if it is the good one
+                option_hidden.setAttribute("selected", "selected");
+                select_hidden.setAttribute('value', option_selected.value)
               }
-              
+
+              //Trigerring a reload of the associated form
+              let parent_form = select_hidden.closest('sib-form');
+              parent_form.component.inputChange();
             }
           });
         };
-- 
GitLab