From ae78224baccbec8a5425821d818e7065401d914d Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 22 Apr 2024 16:43:57 +0200 Subject: [PATCH] feat(faq): link siblings FAQs of same category --- app/controllers/faq_controller.rb | 2 ++ app/services/faqs_loader_service.rb | 6 ++++++ app/views/faq/_sidebar.html.haml | 20 ++++++++++++++++++++ app/views/faq/show.html.haml | 2 ++ config/locales/faqs.fr.yml | 1 + 5 files changed, 31 insertions(+) create mode 100644 app/views/faq/_sidebar.html.haml diff --git a/app/controllers/faq_controller.rb b/app/controllers/faq_controller.rb index ba0b69a95..7d19499c3 100644 --- a/app/controllers/faq_controller.rb +++ b/app/controllers/faq_controller.rb @@ -7,6 +7,8 @@ class FAQController < ApplicationController @renderer = Redcarpet::Markdown.new( Redcarpet::BareRenderer.new(class_names_map: { list: 'fr-ol-content--override' }) ) + + @siblings = loader_service.faqs_for_category(@metadata[:category]) end private diff --git a/app/services/faqs_loader_service.rb b/app/services/faqs_loader_service.rb index 2665d8aa8..95db7b63d 100644 --- a/app/services/faqs_loader_service.rb +++ b/app/services/faqs_loader_service.rb @@ -16,6 +16,12 @@ class FAQsLoaderService FrontMatterParser::Parser.parse_file(file_path) end + def faqs_for_category(category) + @faqs_by_path.values + .filter { |faq| faq[:category] == category } + .group_by { |faq| faq[:subcategory] } + end + private def load_faqs diff --git a/app/views/faq/_sidebar.html.haml b/app/views/faq/_sidebar.html.haml new file mode 100644 index 000000000..d6055490a --- /dev/null +++ b/app/views/faq/_sidebar.html.haml @@ -0,0 +1,20 @@ +%nav.fr-sidemenu.fr-sidemenu--sticky{ role: "navigation", 'aria-labelledby': "fr-sidemenu-title" } + .fr-sidemenu__inner + %button.fr-sidemenu__btn{ 'aria-controls': "fr-sidemenu-wrapper", 'aria-expanded': "false" } + = t(:sidebar_button, scope: [:faq]) + .fr-collapse#fr-sidemenu-wrapper + .fr-sidemenu__title#fr-sidemenu-title + = t(:name, scope: [:faq, :categories, current[:category]]) + %ul.fr-sidemenu__list + - faqs.each_with_index do |(subcategory, faqs), index| + %li{ class: class_names("fr-sidemenu__item", "fr-sidemenu__item--active" => subcategory == current[:subcategory]) } + %button.fr-sidemenu__btn{ aria: { 'expanded': subcategory == current[:subcategory] ? "true" : "false", + 'controls': "fr-sidemenu-item-#{index}", + 'current' => subcategory == current[:subcategory] ? "true" : nil } } + = t(:name, scope: [:faq, :subcategories, subcategory]) + .fr-collapse{ id: "fr-sidemenu-item-#{index}" } + %ul.fr-sidemenu__list + - faqs.each do |faq| + %li{ class: class_names("fr-sidemenu__item", "fr-sidemenu__item--active" => faq[:slug] == current[:slug]) } + = link_to faq[:title], faq_path(category: faq[:category], slug: faq[:slug]), + class: 'fr-sidemenu__link', target: "_self", "aria-current" => current[:slug] == faq[:slug] ? "page" : nil diff --git a/app/views/faq/show.html.haml b/app/views/faq/show.html.haml index 46dc34cd9..b72b8c8f0 100644 --- a/app/views/faq/show.html.haml +++ b/app/views/faq/show.html.haml @@ -2,5 +2,7 @@ .fr-container.fr-my-4w .fr-grid-row + .fr-col-12.fr-col-md-4 + = render partial: "sidebar", locals: { faqs: @siblings, current: @metadata } .fr-col-12.fr-col-md-8.fr-py-12v = @renderer.render(@content).html_safe diff --git a/config/locales/faqs.fr.yml b/config/locales/faqs.fr.yml index 155a815f2..f57a355c4 100644 --- a/config/locales/faqs.fr.yml +++ b/config/locales/faqs.fr.yml @@ -1,5 +1,6 @@ fr: faq: + sidebar_button: Dans cette Foire Aux Questions categories: usager: name: Usager (dépôt d’un dossier)