From 90371bbb826c533dba2b8a382b06facab4df2228 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Wed, 11 Aug 2021 19:02:12 +0200 Subject: [PATCH 1/3] save instructeur commentaire local storage --- .../shared/dossiers/_messagerie.html.haml | 2 +- .../shared/dossiers/messages/_form.html.haml | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/views/shared/dossiers/_messagerie.html.haml b/app/views/shared/dossiers/_messagerie.html.haml index a37612b50..039f01043 100644 --- a/app/views/shared/dossiers/_messagerie.html.haml +++ b/app/views/shared/dossiers/_messagerie.html.haml @@ -5,6 +5,6 @@ = render partial: "shared/dossiers/messages/message", locals: { commentaire: commentaire, connected_user: connected_user, messagerie_seen_at: messagerie_seen_at, show_reply_button: show_reply_button(commentaire, connected_user) } - if dossier.messagerie_available? - = render partial: "shared/dossiers/messages/form", locals: { commentaire: new_commentaire, form_url: form_url } + = render partial: "shared/dossiers/messages/form", locals: { commentaire: new_commentaire, form_url: form_url, dossier: dossier } - else = render partial: "shared/dossiers/messages/messagerie_disabled", locals: { service: dossier.procedure.service, dossier: dossier } diff --git a/app/views/shared/dossiers/messages/_form.html.haml b/app/views/shared/dossiers/messages/_form.html.haml index c9a4837de..c1b555b7a 100644 --- a/app/views/shared/dossiers/messages/_form.html.haml +++ b/app/views/shared/dossiers/messages/_form.html.haml @@ -1,8 +1,9 @@ -= form_for(commentaire, url: form_url, html: { class: 'form' }) do |f| += form_for(commentaire, url: form_url, html: { class: 'form', id: "commentaire-form-#{@dossier.id}" }) do |f| + - dossier = commentaire.dossier - placeholder = t('views.shared.dossiers.messages.form.write_message_to_administration_placeholder') - if instructeur_signed_in? || administrateur_signed_in? - placeholder = t('views.shared.dossiers.messages.form.write_message_placeholder') - = f.text_area :body, rows: 5, placeholder: placeholder, required: true, class: 'message-textarea' + = f.text_area :body, rows: 5, placeholder: placeholder, required: true, class: 'message-textarea', id: "messagerie-#{@dossier.id}" .flex.justify-between.wrap %div = f.label :piece_jointe, for: :piece_jointe do @@ -12,3 +13,17 @@ %div = f.submit t('views.shared.dossiers.messages.form.send_message'), class: 'button primary send', data: { disable: true } + +:javascript + const commentaireInput = document.getElementById('messagerie-#{@dossier.id}') + const commentaireForm = document.getElementById('commentaire-form-#{@dossier.id}') + commentaireInput.value = localStorage.getItem(`message-#{@dossier.id}`) + + commentaireInput.addEventListener('change', (event) => { + localStorage.setItem(`message-#{@dossier.id}`, event.target.value); + }); + + commentaireForm.addEventListener('submit', () => { + localStorage.removeItem(`message-#{@dossier.id}`); + }) + From cf0b3b4f747bd5a42af29c9c5653cf3aaecc8dc6 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Thu, 12 Aug 2021 11:39:33 +0200 Subject: [PATCH 2/3] tests --- spec/features/users/dossier_shared_examples.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/users/dossier_shared_examples.rb b/spec/features/users/dossier_shared_examples.rb index d9c36a73b..84fe6a1ae 100644 --- a/spec/features/users/dossier_shared_examples.rb +++ b/spec/features/users/dossier_shared_examples.rb @@ -30,7 +30,7 @@ RSpec.shared_examples 'the user can send messages to the instructeur' do expect(page).to have_current_path(messagerie_dossier_path(dossier)) expect(page).to have_content(commentaire.body) - fill_in 'commentaire_body', with: message_body + fill_in "messagerie-#{dossier.id}", with: message_body click_on 'Envoyer le message' expect(page).to have_current_path(messagerie_dossier_path(dossier)) From 2a84f1452180f99871099d0cc1f38a9459e4bde3 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 17 Aug 2021 10:11:01 +0100 Subject: [PATCH 3/3] Make generic --- app/javascript/new_design/messagerie.js | 41 +++++++++++++++++++ app/views/experts/avis/instruction.html.haml | 4 +- app/views/experts/shared/avis/_form.html.haml | 4 +- .../instructeurs/shared/avis/_form.html.haml | 4 +- .../shared/dossiers/messages/_form.html.haml | 18 +------- .../features/users/dossier_shared_examples.rb | 2 +- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/app/javascript/new_design/messagerie.js b/app/javascript/new_design/messagerie.js index d849996f8..0a3e01b39 100644 --- a/app/javascript/new_design/messagerie.js +++ b/app/javascript/new_design/messagerie.js @@ -14,4 +14,45 @@ function scrollMessagerie() { } } +function saveMessageContent() { + const commentaireForms = Array.from( + document.querySelectorAll('form[data-persisted-content-id]') + ); + + if (commentaireForms.length) { + const commentaireInputs = Array.from( + document.querySelectorAll('.persisted-input') + ); + + const persistedContentIds = commentaireForms.map( + (form) => form.dataset.persistedContentId + ); + + const keys = persistedContentIds.map((key) => `persisted-value-${key}`); + + const object = commentaireInputs.map((input, index) => { + return { + input: input, + form: commentaireForms[index], + key: keys[index] + }; + }); + + for (const el of object) { + if (localStorage.getItem(el.key)) { + el.input.value = localStorage.getItem(el.key); + } + + el.input.addEventListener('change', (event) => { + localStorage.setItem(el.key, event.target.value); + }); + + el.form.addEventListener('submit', () => { + localStorage.removeItem(el.key); + }); + } + } +} + addEventListener('ds:page:update', scrollMessagerie); +addEventListener('ds:page:update', saveMessageContent); diff --git a/app/views/experts/avis/instruction.html.haml b/app/views/experts/avis/instruction.html.haml index 902a320b7..3c0673547 100644 --- a/app/views/experts/avis/instruction.html.haml +++ b/app/views/experts/avis/instruction.html.haml @@ -15,8 +15,8 @@ = render partial: 'shared/attachment/show', locals: { attachment: @avis.introduction_file.attachment } %br/ - = form_for @avis, url: expert_avis_path(@avis.procedure, @avis), html: { class: 'form' } do |f| - = f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true + = form_for @avis, url: expert_avis_path(@avis.procedure, @avis), html: { class: 'form', data: { persisted_content_id: @avis.id } } do |f| + = f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true, class: 'persisted-input' = text_upload_and_render f, @avis.piece_justificative_file .flex.justify-between.align-baseline diff --git a/app/views/experts/shared/avis/_form.html.haml b/app/views/experts/shared/avis/_form.html.haml index 7dc44b8f4..f4fc339f4 100644 --- a/app/views/experts/shared/avis/_form.html.haml +++ b/app/views/experts/shared/avis/_form.html.haml @@ -3,7 +3,7 @@ %h1.tab-title Inviter des personnes à donner leur avis %p.avis-notice Les invités pourront consulter le dossier, donner un avis et contribuer au fil de messagerie. Ils ne pourront pas modifier le dossier. - = form_for avis, url: url, html: { class: 'form' } do |f| + = form_for avis, url: url, html: { class: 'form', data: { persisted_content_id: "expert-ask-avis-for-dossier-#{@avis.dossier.id}" } } do |f| - hidden_field_id = SecureRandom.uuid = hidden_field_tag 'avis[emails]', nil, data: { uuid: hidden_field_id } = react_component("ComboMultipleDropdownList", @@ -12,7 +12,7 @@ hiddenFieldId: hidden_field_id, label: 'avis_emails', acceptNewValues: true) - = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true + = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true, class: 'persisted-input' %p.tab-title Ajouter une pièce jointe .form-group = text_upload_and_render f, avis.introduction_file diff --git a/app/views/instructeurs/shared/avis/_form.html.haml b/app/views/instructeurs/shared/avis/_form.html.haml index 9803b4901..9764a7cba 100644 --- a/app/views/instructeurs/shared/avis/_form.html.haml +++ b/app/views/instructeurs/shared/avis/_form.html.haml @@ -6,7 +6,7 @@ - else %p.avis-notice Entrez les adresses email des experts à qui vous souhaitez demander un avis - = form_for avis, url: url, html: { class: 'form' } do |f| + = form_for avis, url: url, html: { class: 'form', data: { persisted_content_id: "instructeur-ask-avis-for-dossier-#{@dossier.id}" } } do |f| - hidden_field_id = SecureRandom.uuid = hidden_field_tag 'avis[emails]', nil, data: { uuid: hidden_field_id } = react_component("ComboMultipleDropdownList", @@ -17,7 +17,7 @@ label: 'avis_emails', id: 'avis_emails', acceptNewValues: !@dossier.procedure.experts_require_administrateur_invitation) - = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true + = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true, class: "persisted-input" %p.tab-title Ajouter une pièce jointe .form-group = text_upload_and_render f, avis.introduction_file diff --git a/app/views/shared/dossiers/messages/_form.html.haml b/app/views/shared/dossiers/messages/_form.html.haml index c1b555b7a..72b9a6fcb 100644 --- a/app/views/shared/dossiers/messages/_form.html.haml +++ b/app/views/shared/dossiers/messages/_form.html.haml @@ -1,9 +1,9 @@ -= form_for(commentaire, url: form_url, html: { class: 'form', id: "commentaire-form-#{@dossier.id}" }) do |f| += form_for(commentaire, url: form_url, html: { class: 'form', data: { persisted_content_id: @dossier.id } }) do |f| - dossier = commentaire.dossier - placeholder = t('views.shared.dossiers.messages.form.write_message_to_administration_placeholder') - if instructeur_signed_in? || administrateur_signed_in? - placeholder = t('views.shared.dossiers.messages.form.write_message_placeholder') - = f.text_area :body, rows: 5, placeholder: placeholder, required: true, class: 'message-textarea', id: "messagerie-#{@dossier.id}" + = f.text_area :body, rows: 5, placeholder: placeholder, required: true, class: 'message-textarea persisted-input' .flex.justify-between.wrap %div = f.label :piece_jointe, for: :piece_jointe do @@ -13,17 +13,3 @@ %div = f.submit t('views.shared.dossiers.messages.form.send_message'), class: 'button primary send', data: { disable: true } - -:javascript - const commentaireInput = document.getElementById('messagerie-#{@dossier.id}') - const commentaireForm = document.getElementById('commentaire-form-#{@dossier.id}') - commentaireInput.value = localStorage.getItem(`message-#{@dossier.id}`) - - commentaireInput.addEventListener('change', (event) => { - localStorage.setItem(`message-#{@dossier.id}`, event.target.value); - }); - - commentaireForm.addEventListener('submit', () => { - localStorage.removeItem(`message-#{@dossier.id}`); - }) - diff --git a/spec/features/users/dossier_shared_examples.rb b/spec/features/users/dossier_shared_examples.rb index 84fe6a1ae..d9c36a73b 100644 --- a/spec/features/users/dossier_shared_examples.rb +++ b/spec/features/users/dossier_shared_examples.rb @@ -30,7 +30,7 @@ RSpec.shared_examples 'the user can send messages to the instructeur' do expect(page).to have_current_path(messagerie_dossier_path(dossier)) expect(page).to have_content(commentaire.body) - fill_in "messagerie-#{dossier.id}", with: message_body + fill_in 'commentaire_body', with: message_body click_on 'Envoyer le message' expect(page).to have_current_path(messagerie_dossier_path(dossier))