refactor(dsfr/button): remove useless & verbose Dsfr::ButtonComponent, in favor of good link_to / f.submit

This commit is contained in:
Colin Darie 2022-09-07 14:52:51 +02:00
parent 5f760585ad
commit 5f08df7561
8 changed files with 23 additions and 83 deletions

View file

@ -1,59 +0,0 @@
class Dsfr::ButtonComponent < ApplicationComponent
private
attr_reader :label, :url, :form, :html_options
# Mimic link_to signature link_to, except when form keyword is given for a submit, or when rendering a collection of buttons.
#
# `fr-btn` class is always added.
#
# Examples of usage:
#
# A link as a button, with any html option:
# Dsfr::ButtonComponent.new("My link", "http://example.com", class: "fr-btn--secondary", title: "Link title")
#
# With external: true option which appends target="_blank", rel="noopener noreferrer" to html options:
# Dsfr::ButtonComponent.new("My link", "http://example.com", class: "fr-btn--lg fr-btn--mb-1w", external: true)
#
# A submit button for a given form, (accept also any html option):
# Dsfr::ButtonComponent.new("My submit", form: $form-builder-instance)
#
# A collection of button rendered:
# Dsfr::ButtonComponent.with_collection([
# { label: "My link 1", url: "http://example.com" },
# { label: "My link 2", url: "http://example2.com", class: "fr-btn--secondary" }
# ])
#
def initialize(label = nil, url = nil, form: nil, external: nil, button: nil, **html_options)
if button.present?
assign_attributes(**button)
else
assign_attributes(label:, form:, url:, html_options:, external:)
end
end
def assign_attributes(label: nil, url: nil, form: nil, html_options: {}, external: false)
@label = label
@url = url
@form = form
@external = external
@html_options = build_all_html_options(html_options)
end
def build_all_html_options(input_html_options)
class_names = Array(input_html_options.delete(:class)).push("fr-btn")
external_attributes.merge(class: class_names.join(" "), **input_html_options)
end
def external_attributes
return {} unless external?
{ target: "_blank", rel: "noopener noreferrer" }
end
def external?
@external
end
end

View file

@ -1,5 +0,0 @@
- if form
= form.submit label, html_options
- elsif url
= link_to label, url, html_options

View file

@ -195,4 +195,8 @@ module ApplicationHelper
Rails.env.production? || ENV['VITE_LEGACY'] == 'enabled'
end
end
def external_link_attributes
{ target: "_blank", rel: "noopener noreferrer" }
end
end

View file

@ -72,6 +72,6 @@
= hidden_field_tag :procedure_id, procedure_id
.send-wrapper.justify-content--space-between.send-wrapper--with-border-top.my-0.py-3
= render Dsfr::ButtonComponent.new("Annuler et revenir à la page de suivi", admin_procedure_path(id: @procedure.id), class: "fr-btn--secondary")
= render Dsfr::ButtonComponent.new("Enregistrer", form: f)
= link_to "Annuler et revenir à la page de suivi", admin_procedure_path(id: @procedure.id), class: "fr-btn fr-btn--secondary"
= f.submit "Enregistrer", class: "fr-btn"

View file

@ -13,7 +13,7 @@
%th{ scope: "col" }
Nom
%th.change{ scope: "col" }
= render Dsfr::ButtonComponent.new("Nouveau service", new_admin_service_path(procedure_id: @procedure.id), class: "fr-btn--secondary")
= link_to "Nouveau service", new_admin_service_path(procedure_id: @procedure.id), class: "fr-btn fr-btn--secondary"
%tbody
- @services.each do |service|

View file

@ -20,8 +20,8 @@
%h1.role-panel-title Est-ce fait pour mon administration ?
%p.role-panel-explanation Découvrez notre outil et posez nous vos questions lors de notre démonstration en ligne ou lisez notre documentation
= render Dsfr::ButtonComponent.new("Participer à notre démonstration en ligne", "https://app.livestorm.co/demarches-simplifiees/presentation-general-de-demarches-simplifieesfr", class: "fr-btn--lg frm-mr-1w fr-mb-2w", external: true)
= render Dsfr::ButtonComponent.new("Documentation", DOC_URL, class: "fr-btn--secondary fr-btn--lg", external: true)
= link_to "Participer à notre démonstration en ligne", INSCRIPTION_WEBINAIRE_URL, class: "fr-btn fr-btn--lg fr-mr-1w fr-mb-2w", **external_link_attributes
= link_to "Documentation", DOC_URL, class: "fr-btn fr-btn--secondary fr-btn--lg", **external_link_attributes
.role-panel-30.role-more-info-image.fr-mt-2w
%img.role-image{ :src => image_url("landing/roles/usagers.svg"), alt: "" }
@ -37,7 +37,7 @@
%p.role-panel-explanation Créez un premier formulaire de test en quelques minutes
= render Dsfr::ButtonComponent.new("Créer votre compte administrateur", DEMANDE_INSCRIPTION_ADMIN_PAGE_URL, class: "fr-btn--lg")
= link_to "Créer votre compte administrateur", DEMANDE_INSCRIPTION_ADMIN_PAGE_URL, class: "fr-btn fr-btn--lg"
%br
%br
@ -104,16 +104,15 @@
.container
.fr-grid-row.fr-grid-row--gutters.fr-py-5w
.fr-col-md-6.fr-col-12
= render Dsfr::CardComponent.new(title: "Vous êtes prêt pour dématérialiser ?",
desc: "Réduisez vos temps dinstruction de 50 %",
footer_buttons: [{ url: DEMANDE_INSCRIPTION_ADMIN_PAGE_URL, label: "Créer votre compte administrateur", external: true }],
)
= render Dsfr::CardComponent.new(title: "Vous êtes prêt pour dématérialiser ?", desc: "Réduisez vos temps dinstruction de 50 %") do |c|
- c.with_footer_button do
= link_to("Créer votre compte administrateur", DEMANDE_INSCRIPTION_ADMIN_PAGE_URL, class: "fr-btn", **external_link_attributes)
.fr-col-md-6.fr-col-12
= render Dsfr::CardComponent.new(title: "Vous voulez en savoir plus ?",
desc: "Participez à notre prochain Webinaire",
footer_buttons: [{ url: "https://app.livestorm.co/demarches-simplifiees/presentation-general-de-demarches-simplifieesfr", label: "Inscription à notre prochain webinaire", external: true }],
)
= render Dsfr::CardComponent.new(title: "Vous voulez en savoir plus ?", desc: "Participez à notre prochain Webinaire") do |c|
- c.with_footer_button do
= link_to("Inscription à notre prochain webinanire", INSCRIPTION_WEBINAIRE_URL, class: "fr-btn", **external_link_attributes)
.landing-panel.cta-panel
.container
.cta-panel-wrapper
@ -121,4 +120,4 @@
%h1.cta-panel-title Une question, un problème ?
%p.cta-panel-explanation Consultez notre FAQ
%div
= render Dsfr::ButtonComponent.new("Voir la FAQ", FAQ_URL, external: true, class: "fr-btn--lg")
= link_to "Voir la FAQ", FAQ_URL, class: "fr-btn fr-btn--lg", **external_link_attributes

View file

@ -26,8 +26,8 @@
%h2.role-panel-title Vous souhaitez effectuer une demande auprès dune administration ?
%p.role-panel-explanation Réalisez vos demandes en toute simplicité et retrouvez vos dossiers en ligne
= render Dsfr::ButtonComponent.new("Comment trouver ma démarche ?", COMMENT_TROUVER_MA_DEMARCHE_URL, class: "fr-btn--lg fr-mr-1w fr-mb-2w", external: true)
= render Dsfr::ButtonComponent.new("Se connecter", new_user_session_path, class: "fr-btn--secondary fr-btn--lg", external: true)
= link_to "Comment trouver ma démarche ?", COMMENT_TROUVER_MA_DEMARCHE_URL, class: "fr-btn fr-btn--lg fr-mr-1w fr-mb-2w", **external_link_attributes
= link_to "Se connecter", new_user_session_path, class: "fr-btn fr-btn--secondary fr-btn--lg", **external_link_attributes
- cache "numbers-panel", :expires_in => 3.hours do
.landing-panel
@ -63,7 +63,7 @@
%h2.cta-panel-title Une question, un problème ?
%p.cta-panel-explanation La réponse est dans laide en ligne
%div
= render Dsfr::ButtonComponent.new "Accéder à laide en ligne", FAQ_URL, class: "fr-btn--lg", external: true
= link_to "Accéder à laide en ligne", FAQ_URL, class: "fr-btn fr-btn--lg", **external_link_attributes
-# We temporarily disable the link to the contact page on the homepage
-# %p.cta-panel-explanation Notre équipe est disponible pour vous renseigner et vous aider
-# %div
@ -80,4 +80,4 @@
%h2.cta-panel-title.grey Administration : vous voulez dématerialiser ?
%p.cta-panel-explanation.grey Proposez à vos usagers de remplir leurs dossiers en ligne
%div
= render Dsfr::ButtonComponent.new "Découvrez notre outil", administration_path, class: "fr-btn--lg"
= link_to "Découvrez notre outil", administration_path, class: "fr-btn fr-btn--lg"

View file

@ -18,6 +18,7 @@ DS_PROXY_URL = ENV.fetch("DS_PROXY_URL", "")
# External services URLs
WEBINAIRE_URL = "https://app.livestorm.co/demarches-simplifiees"
INSCRIPTION_WEBINAIRE_URL = "https://app.livestorm.co/demarches-simplifiees/inscription-webinaire-les-avances-de-la-demarche-simplifiee"
CALENDLY_URL = "https://calendly.com/demarches-simplifiees/accompagnement-administrateur-demarches-simplifiees-fr"
DOC_URL = ENV.fetch("DOC_URL", "https://doc.demarches-simplifiees.fr")