From 9da1ef7811e7e10eda4257b9dd80f052c4e83bc7 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 27 Oct 2022 11:10:18 +0200
Subject: [PATCH] better blocks

---
 .../block/template/organization_chart.rb             |  9 +++++++++
 .../blocks/components/person/_edit.html.erb          | 10 ++++++----
 .../blocks/components/string/_edit.html.erb          | 12 +++++++-----
 .../templates/organization_chart/_edit.html.erb      | 11 ++++++-----
 .../blocks/templates/partners/_edit.html.erb         |  2 +-
 config/locales/communication/en.yml                  |  2 ++
 config/locales/communication/fr.yml                  | 10 ++++++----
 7 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/app/models/communication/block/template/organization_chart.rb b/app/models/communication/block/template/organization_chart.rb
index 334d745ed..0b1239215 100644
--- a/app/models/communication/block/template/organization_chart.rb
+++ b/app/models/communication/block/template/organization_chart.rb
@@ -4,5 +4,14 @@ class Communication::Block::Template::OrganizationChart < Communication::Block::
   has_component :description, :rich_text
   has_component :with_link, :boolean
   has_component :with_photo, :boolean
+  has_component :alphabetical, :boolean
 
+  def elements
+    if alphabetical
+      @elements.sort_by! do |element|
+        "#{element.person&.last_name&.parameterize&.downcase}"
+      end
+    end
+    @elements
+  end
 end
diff --git a/app/views/admin/communication/blocks/components/person/_edit.html.erb b/app/views/admin/communication/blocks/components/person/_edit.html.erb
index 679a74a43..33c7acb4a 100644
--- a/app/views/admin/communication/blocks/components/person/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/person/_edit.html.erb
@@ -1,10 +1,12 @@
 <%
 people = current_university.people.ordered
 %>
-<label  class="form-label"
-        :for="<%= dom_id.html_safe %>">
-  <%= label %>
-</label>
+<% unless label.blank? %>
+  <label  class="form-label"
+          :for="<%= dom_id.html_safe %>">
+    <%= label %>
+  </label>
+<% end %>
 <select :id="<%= dom_id.html_safe %>"
         class="form-select select mb-3"
         v-model="<%= model %>.<%= property %>">
diff --git a/app/views/admin/communication/blocks/components/string/_edit.html.erb b/app/views/admin/communication/blocks/components/string/_edit.html.erb
index 9175fe2c8..9dc947a2b 100644
--- a/app/views/admin/communication/blocks/components/string/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/string/_edit.html.erb
@@ -1,9 +1,11 @@
 <div class="mb-3">
-  <label  class="form-label" 
-          aria-label="<%= label %>"
-          :for="<%= dom_id.html_safe %>">
-    <%= label%>
-  </label>
+  <% unless label.blank? %>
+    <label  class="form-label" 
+            aria-label="<%= label %>"
+            :for="<%= dom_id.html_safe %>">
+      <%= label%>
+    </label>
+  <% end %>
   <input  :id="<%= dom_id.html_safe %>"
           class="form-control"
           v-model="<%= model %>.<%= property %>"
diff --git a/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb b/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
index 6f6e48d65..3caeb14fb 100644
--- a/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
@@ -6,12 +6,11 @@
     <label class="form-label">&nbsp;</label>
     <%= block_component_edit :with_link %>
     <%= block_component_edit :with_photo %>
+    <%= block_component_edit :alphabetical %>
   </div>
 </div>
 
-<%= block_component_add_element t('.add_person') %>
-
-<draggable :list="data.elements" class="list-group" handle=".dragHandle">
+<draggable :list="data.elements" class="list-group mb-3" handle=".dragHandle">
   <div v-for="(element, index) in data.elements" class="list-group-item">
     <div class="d-flex">
       <div>
@@ -22,10 +21,10 @@
       <div class="flex-fill">
         <div class="row mb-n3">
           <div class="col-md-6">
-            <%= block_component_edit :id, template: @element %>
+            <%= block_component_edit :id, template: @element, label: '' %>
           </div>
           <div class="col-md-6">
-            <%= block_component_edit :role, template: @element %>
+            <%= block_component_edit :role, template: @element, label: '' %>
           </div>
         </div>
       </div>
@@ -39,3 +38,5 @@
     </div>
   </div>
 </draggable>
+
+<%= block_component_add_element t('.add_person') %>
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/templates/partners/_edit.html.erb b/app/views/admin/communication/blocks/templates/partners/_edit.html.erb
index 4741592e6..14de0acf0 100644
--- a/app/views/admin/communication/blocks/templates/partners/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/partners/_edit.html.erb
@@ -3,7 +3,6 @@
     <%= block_component_edit :description %>
   </div>
 </div>
-<%= block_component_add_element t('.add_partner') %>
 <draggable :list="data.elements" class="list-group" handle=".partnerHandle">
   <div v-for="(element, index) in data.elements" class="list-group-item">
     <div class="d-flex mb-n3">
@@ -40,3 +39,4 @@
     </div>
   </div>
 </draggable>
+<%= block_component_add_element t('.add_partner') %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index bb3df98a3..1b0f3e7c2 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -343,6 +343,8 @@ en:
               add_person: Add person
               drag_title: Drag and drop to organize persons
               delete_title: Remove person
+              alphabetical:
+                label: Alphabetical order
               description:
                 label: Description
               with_link:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 131983d4d..00fbfca42 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -341,11 +341,13 @@ fr:
                   label: Description
                   placeholder: Entrer la description
           organization_chart:
-            description: Un organigramme présentant des personnes et leur fonction.
+            description: Une liste de personnes, avec photo, fonction et lien vers la page de la personne.
             edit:
               add_person: Ajouter une personne
               drag_title: Glisser-déposer pour organiser les personnes
               delete_title: Enlever la personne
+              alphabetical:
+                label: Ordre alphabétique
               with_link:
                 label: Avec des liens cliquables vers les pages des personnes ?
               with_photo:
@@ -397,7 +399,7 @@ fr:
               display:
                 title: Options d'affichage
           partners:
-            description: Une liste de partenaires, avec leur logo, leur site et leur nom.
+            description: Une liste d'organisations, avec leur logo, leur site et leur nom.
             edit:
               add_partner: Ajouter un partenaire
               remove_partner: Enlever le partenaire
@@ -627,9 +629,9 @@ fr:
           gallery: Galerie
           image: Image
           key_figures: Chiffres-clés
-          organization_chart: Organigramme
+          organization_chart: Personnes
           pages: Pages
-          partners: Partenaires
+          partners: Organisations
           posts: Actualités
           programs: Formations
           testimonials: Témoignages
-- 
GitLab