refactor(autosave): extract in component, better wording for attestation

This commit is contained in:
Colin Darie 2024-06-07 10:22:01 +02:00
parent ce998f7864
commit 70aee9c9de
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
11 changed files with 43 additions and 17 deletions

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class AutosaveNoticeComponent < ApplicationComponent
attr_reader :label_scope
def initialize(success:, label_scope:)
@success = success
@label_scope = label_scope
end
def success? = @success
def label
success? ? t(".#{label_scope}.saved") : t(".#{label_scope}.error")
end
end

View file

@ -0,0 +1,8 @@
---
en:
form:
saved: 'Form saved'
error: 'Form in error'
attestation:
saved: 'Attestation saved'
error: 'Attestation in error'

View file

@ -0,0 +1,8 @@
---
fr:
form:
saved: 'Formulaire enregistré'
error: 'Formulaire en erreur'
attestation:
saved: 'Attestation enregistrée'
error: 'Attestation en erreur'

View file

@ -0,0 +1,2 @@
#autosave-notice.fr-badge.fr-badge--sm{ class: class_names("fr-badge--success" => success?, "fr-badge--error" => !success?) }
= label

View file

@ -1,2 +0,0 @@
- success = local_assigns.fetch(:success, true)
#autosave-notice.fr-badge.fr-badge--sm{ class: class_names("fr-badge--success" => success, "fr-badge--error" => !success) }= success ? t(".form_saved") : t(".form_error")

View file

@ -2,7 +2,7 @@
= turbo_stream.update "sticky-header", render(partial: "sticky_header")
= turbo_stream.show 'autosave-notice'
= turbo_stream.replace 'autosave-notice', render(partial: 'administrateurs/autosave_notice', locals: { success: !@attestation_template.changed? })
= turbo_stream.replace('autosave-notice', render(AutosaveNoticeComponent.new(success: !@attestation_template.changed?, label_scope: :attestation)))
= turbo_stream.hide 'autosave-notice', delay: 15000
- if @attestation_template.logo_blob&.previously_new_record?

View file

@ -18,7 +18,7 @@
- unless flash.alert
= turbo_stream.show 'autosave-notice'
= turbo_stream.replace 'autosave-notice', render(partial: 'administrateurs/autosave_notice')
= turbo_stream.replace 'autosave-notice', render(AutosaveNoticeComponent.new(success: true, label_scope: :form))
= turbo_stream.hide 'autosave-notice', delay: 30000
- if @destroyed.present?

View file

@ -71,6 +71,3 @@ en:
path_not_available:
owner: This URL is identical to another of your published procedures. If you publish this procedure, the old one will be unpublished and will no longer be accessible to the public.
not_owner: This URL is identical to another procedure, you must modify it.
autosave_notice:
form_saved: "Form saved"
form_error: "Form in error"

View file

@ -71,6 +71,3 @@ fr:
path_not_available:
owner: Cette url est identique à celle dune autre de vos démarches publiées. Si vous publiez cette démarche, lancienne sera dépubliée et ne sera plus accessible au public.
not_owner: Cette url est identique à celle dune autre démarche, vous devez la modifier afin de pouvoir publier votre démarche.
autosave_notice:
form_saved: "Formulaire enregistré"
form_error: "Formulaire en erreur"

View file

@ -192,7 +192,7 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
expect(attestation_template.activated).to eq(true)
expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body])
expect(response.body).to include("Formulaire enregistré")
expect(response.body).to include("Attestation enregistrée")
end
context "with files" do
@ -234,7 +234,7 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
expect(attestation_template.activated).to eq(true)
expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body])
expect(response.body).to include("Formulaire enregistré")
expect(response.body).to include("Attestation enregistrée")
expect(response.body).to include("Publier")
end
@ -258,7 +258,7 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do
it "renders error" do
subject
expect(response.body).to include("Formulaire en erreur")
expect(response.body).to include("Attestation en erreur")
expect(response.body).to include('Supprimer la balise')
end
end

View file

@ -106,9 +106,9 @@ describe 'As an administrateur, I want to manage the procedures attestation',
attestation = procedure.reload.attestation_templates.v2.draft.first
attestation.present?
}
expect(page).to have_content("Attestation enregistrée")
expect(attestation.label_logo).to eq("System Test")
expect(attestation.activated?).to be_truthy
expect(page).to have_content("Formulaire enregistré")
click_on "date de décision"
@ -172,14 +172,14 @@ describe 'As an administrateur, I want to manage the procedures attestation',
click_on "date de décision"
expect(page).to have_content("Formulaire en erreur")
expect(page).to have_content("Attestation en erreur")
expect(page).to have_content("Le champ « Contenu de lattestation » contient la balise \"age\"")
page.execute_script("document.getElementById('attestation_template_tiptap_body').type = 'text'")
fill_in "attestation_template[tiptap_body]", with: AttestationTemplate::TIPTAP_BODY_DEFAULT.to_json
expect(page).to have_content("Formulaire enregistré")
expect(page).not_to have_content("Formulaire en erreur")
expect(page).to have_content("Attestation enregistrée")
expect(page).not_to have_content("Attestation en erreur")
expect(page).not_to have_content("Le champ « Contenu de lattestation » contient la balise \"age\"")
end
end