From 1b9eb1c4fca0e486958b99e7c76c1ee7790709ec Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 15 Feb 2022 15:36:40 +0100
Subject: [PATCH] working prototype

---
 Gemfile                                       |  1 +
 Gemfile.lock                                  |  2 +
 app/assets/javascripts/admin.js               |  1 +
 .../website/blocks_controller.rb              | 10 ++++-
 app/models/communication/website/block.rb     |  5 +++
 .../website/blocks/_list.html.erb             |  7 +++-
 .../website/blocks/_static.html.erb           |  2 +-
 .../website/blocks/edit.html.erb              | 18 +++++---
 .../website/blocks/show.html.erb              |  7 ++++
 .../organization_chart/_edit.html.erb         | 42 ++++++++++++++++++-
 .../organization_chart/_show.html.erb         |  6 +++
 .../organization_chart/_static.html.erb       | 12 ++++--
 .../blocks/templates/partners/_edit.html.erb  |  1 -
 config/routes/admin/communication.rb          |  2 +-
 14 files changed, 101 insertions(+), 15 deletions(-)
 create mode 100644 app/views/admin/communication/website/blocks/show.html.erb

diff --git a/Gemfile b/Gemfile
index d1b9cae28..ca2585d71 100644
--- a/Gemfile
+++ b/Gemfile
@@ -41,6 +41,7 @@ gem 'has_scope', '~> 0.8.0'
 
 # Front
 gem 'jquery-rails'
+gem 'angularjs-rails'
 gem 'sassc-rails'
 gem 'jbuilder'
 gem 'kamifusen'#, path: '../kamifusen'
diff --git a/Gemfile.lock b/Gemfile.lock
index 8575dc8d6..6351eebe9 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -80,6 +80,7 @@ GEM
       zeitwerk (~> 2.3)
     addressable (2.8.0)
       public_suffix (>= 2.0.2, < 5.0)
+    angularjs-rails (1.8.0)
     annotate (3.2.0)
       activerecord (>= 3.2, < 8.0)
       rake (>= 10.4, < 14.0)
@@ -426,6 +427,7 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
+  angularjs-rails
   annotate
   aws-sdk-s3
   bootsnap (>= 1.4.4)
diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js
index 8325a2d7f..b350adb49 100644
--- a/app/assets/javascripts/admin.js
+++ b/app/assets/javascripts/admin.js
@@ -5,6 +5,7 @@
 //= require notyf/notyf.min
 //= require simple_form_password_with_hints
 //= require simple_form_bs5_file_input
+//= require angular
 //= require cropperjs/dist/cropper
 //= require jquery-cropper/dist/jquery-cropper
 //= require appstack/app
diff --git a/app/controllers/admin/communication/website/blocks_controller.rb b/app/controllers/admin/communication/website/blocks_controller.rb
index 4b0abb3b4..888a6611e 100644
--- a/app/controllers/admin/communication/website/blocks_controller.rb
+++ b/app/controllers/admin/communication/website/blocks_controller.rb
@@ -10,6 +10,10 @@ class Admin::Communication::Website::BlocksController < Admin::Communication::We
     end
   end
 
+  def show
+    breadcrumb
+  end
+
   def new
     @block.about_type = params[:about_type]
     @block.about_id = params[:about_id]
@@ -50,7 +54,11 @@ class Admin::Communication::Website::BlocksController < Admin::Communication::We
     super
     add_breadcrumb @block.about.model_name.human(count: 2), [:admin, @block.about.class]
     add_breadcrumb @block.about, [:admin, @block.about]
-    add_breadcrumb t('communication.website.block.choose_template')
+    if @block.new_record?
+      add_breadcrumb t('communication.website.block.choose_template')
+    else
+      add_breadcrumb @block
+    end
   end
 
 
diff --git a/app/models/communication/website/block.rb b/app/models/communication/website/block.rb
index ca0170661..db79b935c 100644
--- a/app/models/communication/website/block.rb
+++ b/app/models/communication/website/block.rb
@@ -36,6 +36,11 @@ class Communication::Website::Block < ApplicationRecord
     partners: 200
   }
 
+  def data=(value)
+    value = JSON.parse value if value.is_a? String
+    super(value)
+  end
+
   def to_s
     "Bloc #{position}"
   end
diff --git a/app/views/admin/communication/website/blocks/_list.html.erb b/app/views/admin/communication/website/blocks/_list.html.erb
index 462075f63..e215b6715 100644
--- a/app/views/admin/communication/website/blocks/_list.html.erb
+++ b/app/views/admin/communication/website/blocks/_list.html.erb
@@ -15,14 +15,19 @@
           <th width="20" class="ps-0">&nbsp;</th>
           <th><%= Communication::Website::Block.human_attribute_name('name') %></th>
           <th><%= Communication::Website::Block.human_attribute_name('template') %></th>
+          <th></th>
         </tr>
       </thead>
       <tbody data-sortable data-sort-url="<%= reorder_admin_communication_website_blocks_path %>">
         <% about.blocks.ordered.each do |block| %>
           <tr data-id="<%= block.id %>">
             <td><i class="fa fa-bars handle"></i></td>
-            <td><%= link_to block, edit_admin_communication_website_block_path(block) %></td>
+            <td><%= link_to block, admin_communication_website_block_path(block) %></td>
             <td><%= block.template_i18n  %></td>
+            <td class="text-end">
+              <%= edit_link block %>
+              <%= destroy_link block %>
+            </td>
           </tr>
         <% end %>
       </tbody>
diff --git a/app/views/admin/communication/website/blocks/_static.html.erb b/app/views/admin/communication/website/blocks/_static.html.erb
index 48cbe0436..2b5dbed63 100644
--- a/app/views/admin/communication/website/blocks/_static.html.erb
+++ b/app/views/admin/communication/website/blocks/_static.html.erb
@@ -3,5 +3,5 @@ blocks:
 <% about.blocks.each do |block| %>
   - template: <%= block.template %>
     data:
-<%= render "admin/communication/website/blocks/templates/#{block.template}/static", block: block %><% end %>
+<%= render "admin/communication/website/blocks/templates/#{block.template}/static", block: block if block.data %><% end %>
 <% end %>
diff --git a/app/views/admin/communication/website/blocks/edit.html.erb b/app/views/admin/communication/website/blocks/edit.html.erb
index 7502b581b..0760375b3 100644
--- a/app/views/admin/communication/website/blocks/edit.html.erb
+++ b/app/views/admin/communication/website/blocks/edit.html.erb
@@ -1,8 +1,14 @@
 <% content_for :title, @block %>
-
-<%= simple_form_for [:admin, @block] do |f| %>
-  <%= render "admin/communication/website/blocks/templates/#{@block.template}/edit", f: f %>
-  <% content_for :action_bar_right do %>
-    <%= submit f %>
+<%
+@block.data = {} if @block.data.nil? || @block.data.blank?
+@block.data['elements'] ||= []
+%>
+<div ng-app ng-init="data = <%= @block.data.is_a?(String) ? @block.data : @block.data.to_json %>">
+  <%= simple_form_for [:admin, @block] do |f| %>
+    <%= render "admin/communication/website/blocks/templates/#{@block.template}/edit", f: f %>
+    <textarea name="communication_website_block[data]" rows="20" cols="200" class="d-none">{{ data | json}}</textarea>
+    <% content_for :action_bar_right do %>
+      <%= submit f %>
+    <% end %>
   <% end %>
-<% end %>
+</div>
diff --git a/app/views/admin/communication/website/blocks/show.html.erb b/app/views/admin/communication/website/blocks/show.html.erb
new file mode 100644
index 000000000..abb88f0df
--- /dev/null
+++ b/app/views/admin/communication/website/blocks/show.html.erb
@@ -0,0 +1,7 @@
+<% content_for :title, @block %>
+
+<%= render "admin/communication/website/blocks/templates/#{@block.template}/show" %>
+
+<% content_for :action_bar_right do %>
+  <%= edit_link @block %>
+<% end %>
diff --git a/app/views/admin/communication/website/blocks/templates/organization_chart/_edit.html.erb b/app/views/admin/communication/website/blocks/templates/organization_chart/_edit.html.erb
index f805d4a4e..0c6c5f581 100644
--- a/app/views/admin/communication/website/blocks/templates/organization_chart/_edit.html.erb
+++ b/app/views/admin/communication/website/blocks/templates/organization_chart/_edit.html.erb
@@ -1 +1,41 @@
-<%= f.input :data %>
+<a class="<%= button_classes('mb-2') %>" ng-click="data.elements.push({title: 'Role name', persons: []})">Add role</a>
+<div class="card" ng-repeat="element in data.elements">
+  <div class="card-body">
+    <div class="row">
+      <div class="col-md-5">
+        <div class="row">
+          <div class="col-md-11">
+            <input class="form-control" type="text" ng-model="element.title">
+          </div>
+          <div class="col-md-1 text-end">
+            <a class="btn btn-sm btn-danger mt-1" ng-click="data.elements.splice(data.elements.indexOf(element))"><i class="fas fa-trash"></i></a>
+          </div>
+        </div>
+      </div>
+      <div class="offset-md-1 col-md-6">
+        <div class="row">
+          <div class="col-md-8">
+            <p>People</p>
+          </div>
+          <div class="col-md-4 text-end">
+            <a class="<%= button_classes %>" ng-click="element.persons.push({slug: ''})">Add person</a>
+          </div>
+        </div>
+        <div ng-repeat="person in element.persons" class="mb-2">
+          <div class="row">
+            <div class="col-md-11">
+              <select class="form-select select" ng-model="person.slug">
+                <% @website.university.people.each_with_index do |person, index| %>
+                <option value="<%= person.slug %>"><%= person %></option>
+                <% end %>
+              </select>
+            </div>
+            <div class="col-md-1 text-end">
+              <a class="btn btn-sm btn-danger mt-1" ng-click="element.persons.splice(element.persons.indexOf(person))"><i class="fas fa-trash"></i></a>
+            </div>
+          </div>
+        </div>
+      </div>
+    </div>
+  </div>
+</div>
diff --git a/app/views/admin/communication/website/blocks/templates/organization_chart/_show.html.erb b/app/views/admin/communication/website/blocks/templates/organization_chart/_show.html.erb
index e69de29bb..e9da4c0ee 100644
--- a/app/views/admin/communication/website/blocks/templates/organization_chart/_show.html.erb
+++ b/app/views/admin/communication/website/blocks/templates/organization_chart/_show.html.erb
@@ -0,0 +1,6 @@
+<% @block.data['elements'].each do |element| %>
+  <h2><%= element['title'] %></h2>
+  <% element['persons'].each do |person| %>
+    <p><%= person['slug'] %></p>
+  <% end %>
+<% end %>
diff --git a/app/views/admin/communication/website/blocks/templates/organization_chart/_static.html.erb b/app/views/admin/communication/website/blocks/templates/organization_chart/_static.html.erb
index 1d3d08e84..8442a6bb1 100644
--- a/app/views/admin/communication/website/blocks/templates/organization_chart/_static.html.erb
+++ b/app/views/admin/communication/website/blocks/templates/organization_chart/_static.html.erb
@@ -1,4 +1,10 @@
-      - title: Leadership
+<% if !block.data.nil? && !block.data.is_a?(String) && block.data.has_key?('elements') %>
+<% block.data['elements'].each do |element| %>
+      - title: >
+          <%= element['title'] %>
         persons:
-          - amah-edoh
-          - liliane-umubyeyi
+<% element['persons'].each do |person| %>
+          - "<%= person['slug'] %>"
+<% end %>
+<% end %>
+<% end %>
diff --git a/app/views/admin/communication/website/blocks/templates/partners/_edit.html.erb b/app/views/admin/communication/website/blocks/templates/partners/_edit.html.erb
index f805d4a4e..e69de29bb 100644
--- a/app/views/admin/communication/website/blocks/templates/partners/_edit.html.erb
+++ b/app/views/admin/communication/website/blocks/templates/partners/_edit.html.erb
@@ -1 +0,0 @@
-<%= f.input :data %>
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index 3cddac9b8..76cd8c1fe 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -25,7 +25,7 @@ namespace :communication do
     resources :authors, controller: 'website/authors', only: [:index, :show]
     resources :posts, controller: 'website/posts'
     resources :curations, path: 'posts/curations', as: :post_curations, controller: 'website/posts/curations', only: [:new, :create]
-    resources :blocks, controller: 'website/blocks', except: [:index, :show] do
+    resources :blocks, controller: 'website/blocks', except: :index do
       collection do
         post :reorder
       end
-- 
GitLab