From d0b1292060350e7daf7e820c1422e9c5701c1530 Mon Sep 17 00:00:00 2001 From: Lisa Durand Date: Tue, 1 Oct 2024 15:17:09 +0200 Subject: [PATCH] instructeur can add and delete labels on dossier show --- app/assets/stylesheets/dsfr.scss | 10 ++++++ app/assets/stylesheets/instructeur.scss | 4 +++ .../instructeurs/dossiers_controller.rb | 13 ++++++++ .../dossiers/_header_top.html.haml | 21 +++++++++++- config/routes.rb | 1 + .../instructeurs/dossiers_controller_spec.rb | 33 +++++++++++++++++++ spec/system/instructeurs/instruction_spec.rb | 29 ++++++++++++++++ .../dossiers/show.html.haml_spec.rb | 28 ++++++++++++++++ 8 files changed, 138 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/dsfr.scss b/app/assets/stylesheets/dsfr.scss index a2ca49e60..824ef4263 100644 --- a/app/assets/stylesheets/dsfr.scss +++ b/app/assets/stylesheets/dsfr.scss @@ -262,3 +262,13 @@ button.fr-tag-bug { .fr-badge--lowercase { text-transform: lowercase; } + +// we use badge with small checkbox - to align them we need to reduce the margin +.fr-checkbox-group--sm.fr-checkbox-custom-with-labels input[type="checkbox"] + label::before { + margin-top: 0.15rem; +} + +// we use badge with checkbox so we need to add a background white to the uncheched checkbox +.fr-checkbox-group input[type="checkbox"] + label::before { + background-color: #FFFFFF; +} diff --git a/app/assets/stylesheets/instructeur.scss b/app/assets/stylesheets/instructeur.scss index 9227d5063..8d95fdd70 100644 --- a/app/assets/stylesheets/instructeur.scss +++ b/app/assets/stylesheets/instructeur.scss @@ -48,6 +48,10 @@ width: 450px; } +.dropdown-label.dropdown-content { + min-width: 390px; +} + .print-menu { display: none; position: absolute; diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 0b7eb3a74..07193eac2 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -63,6 +63,19 @@ module Instructeurs end end + def dossier_labels + labels = params[:procedure_label_id]&.map(&:to_i) || [] + + @dossier = dossier + labels.each { |params_label| DossierLabel.find_or_create_by(dossier_id: @dossier.id, procedure_label_id: params_label) } + + all_labels = DossierLabel.where(dossier_id: @dossier.id).pluck(:procedure_label_id) + + (all_labels - labels).each { DossierLabel.find_by(dossier_id: @dossier.id, procedure_label_id: _1).destroy } + + render :change_state + end + def messagerie @commentaire = Commentaire.new @messagerie_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.messagerie_seen_at diff --git a/app/views/instructeurs/dossiers/_header_top.html.haml b/app/views/instructeurs/dossiers/_header_top.html.haml index 765abe36b..e752a54bb 100644 --- a/app/views/instructeurs/dossiers/_header_top.html.haml +++ b/app/views/instructeurs/dossiers/_header_top.html.haml @@ -16,7 +16,6 @@ = render Instructeurs::SVASVRDecisionBadgeComponent.new(projection_or_dossier: dossier, procedure: dossier.procedure, with_label: true) - .header-actions.fr-ml-auto = render partial: 'instructeurs/dossiers/header_actions', locals: { dossier: } = render partial: 'instructeurs/dossiers/print_and_export_actions', locals: { dossier: } @@ -26,3 +25,23 @@ - if dossier.user_deleted? %p.fr-mb-1w %small L’usager a supprimé son compte. Vous pouvez archiver puis supprimer le dossier. + + .fr-mb-3w + - if dossier.procedure_labels.present? + - dossier.procedure_labels.each do |label| + .fr-badge.fr-badge--sm{ class: "fr-badge--#{label.color}" } + = label.name + + = render Dropdown::MenuComponent.new(wrapper: :span, button_options: { class: ['fr-btn--sm fr-btn--tertiary-no-outline fr-pl-1v']}, menu_options: { class: ['dropdown-label left-aligned'] }) do |menu| + - if dossier.procedure_labels.empty? + - menu.with_button_inner_html do + Ajouter un label + + - menu.with_form do + = form_with(url: dossier_labels_instructeur_dossier_path(dossier_id: dossier.id, procedure_id: dossier.procedure.id), method: :post, class: 'fr-p-3w', data: { controller: 'autosubmit', turbo: 'true' }) do |f| + %fieldset.fr-fieldset.fr-mt-2w.fr-mb-0 + = f.collection_check_boxes :procedure_label_id, dossier.procedure.procedure_labels, :id, :name, include_hidden: false do |b| + .fr-fieldset__element + .fr-checkbox-group.fr-checkbox-group--sm.fr-checkbox-custom-with-labels.fr-mb-1w + = b.check_box(checked: DossierLabel.find_by(dossier_id: dossier.id, procedure_label_id: b.value).present? ) + = b.label(class: "fr-label fr-badge fr-badge--sm fr-badge--#{b.object.color}") { b.text } diff --git a/config/routes.rb b/config/routes.rb index d194eb920..5b6dd9634 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -512,6 +512,7 @@ Rails.application.routes.draw do resources :commentaires, only: [:destroy] post 'repousser-expiration' => 'dossiers#extend_conservation' post 'repousser-expiration-and-restore' => 'dossiers#extend_conservation_and_restore' + post 'dossier_labels' => 'dossiers#dossier_labels' get 'geo_data' get 'apercu_attestation' get 'bilans_bdf' diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 4ad9070b2..a035aa8c2 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -1518,4 +1518,37 @@ describe Instructeurs::DossiersController, type: :controller do expect([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp, Commentaire]).to include(*assigns(:gallery_attachments).map { _1.record.class }) end end + + describe 'dossier_labels' do + context 'it create dossier labels' do + subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, procedure_label_id: [ProcedureLabel.first.id] }, format: :turbo_stream } + it 'works' do + subject + dossier.reload + + expect(dossier.dossier_labels.count).to eq(1) + end + + it { expect(subject.body).to include('header-top') } + end + + context 'it remove dossier labels' do + before do + DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.first.id) + end + + subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, procedure_label_id: [] }, format: :turbo_stream } + + it 'works' do + expect(dossier.dossier_labels.count).to eq(1) + + subject + dossier.reload + + expect(dossier.dossier_labels.count).to eq(0) + end + + it { expect(subject.body).to include('header-top') } + end + end end diff --git a/spec/system/instructeurs/instruction_spec.rb b/spec/system/instructeurs/instruction_spec.rb index 62390e084..7f667c9da 100644 --- a/spec/system/instructeurs/instruction_spec.rb +++ b/spec/system/instructeurs/instruction_spec.rb @@ -272,6 +272,35 @@ describe 'Instructing a dossier:', js: true do after { DownloadHelpers.clear_downloads } end + scenario 'An instructeur can add labels to a dossier' do + log_in(instructeur.email, password) + + visit instructeur_dossier_path(procedure, dossier) + click_on 'Ajouter un label' + + check 'à relancer', allow_label_click: true + expect(page).to have_css('.fr-badge', text: "à relancer", count: 2) + expect(dossier.dossier_labels.count).to eq(1) + + expect(page).not_to have_text('Ajouter un label') + find('span.dropdown button.dropdown-button').click + + expect(page).to have_checked_field('à relancer') + check 'complet', allow_label_click: true + + expect(page).to have_css('.fr-badge', text: "complet", count: 2) + expect(dossier.dossier_labels.count).to eq(2) + + find('span.dropdown button.dropdown-button').click + uncheck 'à relancer', allow_label_click: true + + expect(page).to have_unchecked_field('à relancer') + expect(page).to have_checked_field('complet') + expect(page).to have_css('.fr-badge', text: "à relancer", count: 1) + expect(page).to have_css('.fr-badge', text: "complet", count: 2) + expect(dossier.dossier_labels.count).to eq(1) + end + def log_in(email, password, check_email: true) visit new_user_session_path expect(page).to have_current_path(new_user_session_path) diff --git a/spec/views/instructeur/dossiers/show.html.haml_spec.rb b/spec/views/instructeur/dossiers/show.html.haml_spec.rb index bb7163332..372f2a5d1 100644 --- a/spec/views/instructeur/dossiers/show.html.haml_spec.rb +++ b/spec/views/instructeur/dossiers/show.html.haml_spec.rb @@ -217,4 +217,32 @@ describe 'instructeurs/dossiers/show', type: :view do expect(subject).to have_selector('a.fr-sidemenu__link', text: 'l1') end end + + describe "Dossier labels" do + context "Dossier without labels" do + it 'displays button with text to add label' do + expect(subject).to have_text("Ajouter un label") + expect(subject).to have_selector("button.dropdown-button") + expect(subject).to have_text("à relancer", count: 1) + within('.dropdown') do + expect(subject).to have_text("à relancer", count: 1) + end + end + end + + context "Dossier with labels" do + before do + DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.first.id) + end + + it 'displays labels and button without text to add label' do + expect(subject).not_to have_text("Ajouter un label") + expect(subject).to have_selector("button.dropdown-button") + expect(subject).to have_text("à relancer", count: 2) + within('.dropdown') do + expect(subject).to have_text("à relancer", count: 1) + end + end + end + end end