Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETQ admin : je peux ordonner les labels #11237

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/controllers/administrateurs/labels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@
redirect_to [:admin, @procedure, :labels]
end

def order_positions
@labels = @procedure.labels
render layout: "empty_layout"

Check warning on line 52 in app/controllers/administrateurs/labels_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/administrateurs/labels_controller.rb#L51-L52

Added lines #L51 - L52 were not covered by tests
end

def update_order_positions
@procedure.update_labels_position(ordered_label_ids_params)
redirect_to admin_procedure_labels_path, notice: "L'ordre des labels a été mis à jour."

Check warning on line 57 in app/controllers/administrateurs/labels_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/administrateurs/labels_controller.rb#L56-L57

Added lines #L56 - L57 were not covered by tests
end

private

def label_params
params.require(:label).permit(:name, :color)
end

def ordered_label_ids_params
params.require(:ordered_label_ids)

Check warning on line 67 in app/controllers/administrateurs/labels_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/administrateurs/labels_controller.rb#L67

Added line #L67 was not covered by tests
end

def retrieve_label
@label = @procedure.labels.find(params[:id])
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApplicationController } from './application_controller';

export class MoveProceduresPositionController extends ApplicationController {
export class MoveCardsPositionController extends ApplicationController {
connect() {
this.updateButtonsStates();
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
belongs_to :transfer, class_name: 'DossierTransfer', foreign_key: 'dossier_transfer_id', optional: true, inverse_of: :dossiers
has_many :transfer_logs, class_name: 'DossierTransferLog', dependent: :destroy
has_many :dossier_labels, dependent: :destroy
has_many :labels, through: :dossier_labels
has_many :labels, -> { order(:position, :id) }, through: :dossier_labels

after_destroy_commit :log_destroy

Expand Down
11 changes: 10 additions & 1 deletion app/models/procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Procedure < ApplicationRecord
has_and_belongs_to_many :procedure_tags

has_many :bulk_messages, dependent: :destroy
has_many :labels, dependent: :destroy
has_many :labels, -> { order(:position, :id) }, dependent: :destroy, inverse_of: :procedure

has_many :instructeurs_procedures, dependent: :destroy

Expand Down Expand Up @@ -803,6 +803,15 @@ def create_generic_labels
end
end

def update_labels_position(ordered_label_ids)
label_ids_positions = ordered_label_ids.each.with_index.to_h
Label.transaction do
label_ids_positions.each do |label_id, position|
Label.where(id: label_id).update(position:)
end
end
end

def used_by_routing_rules?(type_de_champ)
type_de_champ.stable_id.in?(stable_ids_used_by_routing_rules)
end
Expand Down
3 changes: 2 additions & 1 deletion app/services/dossier_projection_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ def self.project(dossiers_ids, columns)

id_value_h =
DossierLabel
.includes(:label)
.joins(:label)
.where(dossier_id: dossiers_ids)
.order('labels.position ASC, labels.id ASC')
.pluck('dossier_id, labels.name, labels.color')
.group_by { |dossier_id, _| dossier_id }

Expand Down
12 changes: 8 additions & 4 deletions app/views/administrateurs/labels/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@

= link_to "Nouveau label",
[:new, :admin, @procedure, :label],
class: "fr-btn fr-btn--primary fr-btn--icon-left fr-icon-add-circle-line mb-3"

- if @procedure.labels.present?
class: "fr-btn fr-btn--primary fr-btn--icon-left fr-icon-add-circle-line fr-mb-4w"

- if @labels.present?
.fr-container.flex.justify-between.fr-mb-2w.fr-p-0
%h2.fr-h6.fr-m-0
Liste des labels
- if @labels.size > 1
= link_to "Personnaliser l'ordre", order_positions_admin_procedure_labels_path, class: 'fr-btn fr-btn--sm fr-btn--tertiary fr-btn--icon-left fr-icon-settings-5-line'
.fr-table.fr-table--lg
.fr-table__wrapper
.fr-table__container
.fr-table__content
%table
%caption Liste des labels
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'arrive après la bataille mais c'est dommage de retirer la caption en faveur d'un titre ! (on devrait en avoir sur toutes nos tables, en tout cas dès qu'on a une exigence en accessibilité, ce qui certes est un peu plus discutable côté admin)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep ... j'ai eu la discussion avec Marlène et Lisa.
En fait, la contrainte d'avoir le bouton "personnaliser" aligné horizontalement avec le titre initialement dans caption n'était pas compatible (j'ai tenté plusieurs astuces en vain) sur l'idée de disposer en justify-between. Il y a un comportement JS propre à caption qui ramène à gauche tout ce qui se trouve dans cette balise, malgré les class qui vont bien.
Et en effet toutes les autres tables qui ont dans l'esprit plusieurs éléments là où l'on a le titre du tableau, n'ont pas/plus de balise caption.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dans le cas ici, et d'autres tableaux "simples", sous couvert de Marlène, on pourrait avoir un équilibre access/UX en acceptant de décaler verticalement. En revanche pour des tableaux plus complexes, comme le suivi instructeur des dossiers, ça paraît plus complexe

%thead
%tr
%th{ scope: "col" }
Expand Down
20 changes: 20 additions & 0 deletions app/views/administrateurs/labels/order_positions.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.fr-container.fr-mt-6w.fr-mb-15w
= link_to " Liste des labels", admin_procedure_labels_path, class: 'fr-link fr-icon-arrow-left-line fr-link--icon--left fr-icon--sm'
%h3.fr-my-3w
Personnaliser l'ordre des #{@labels.size} labels
%p Déplacez les labels dans la liste pour définir leur ordre d'affichage pour les instructeurs :

%fr-container{ data: { controller: 'move-cards-position' } }
= form_tag update_order_positions_admin_procedure_labels_path, method: :patch, id: 'order-admin-procedure-labels-form' do
- @labels.each do |label|
.fr-card.fr-mb-1w.fr-py-1w.fr-px-2w
.flex.align-center
%button.fr-btn.fr-btn--sm.fr-icon-arrow-up-line.fr-btn--secondary.fr-col-1{ data: { action: "move-cards-position#moveUp" } }
%button.fr-btn.fr-btn--sm.fr-icon-arrow-down-line.fr-btn--secondary.fr-col-1.fr-ml-2w.fr-mr-4w{ data: { action: "move-cards-position#moveDown" } }
= tag_label(label.name, label.color)
= hidden_field_tag "ordered_label_ids[]", label.id

.fixed-footer.fr-py-1w
.fr-btns-group.fr-btns-group--center.fr-btns-group--inline.fr-btns-group--inline-lg
= link_to "Annuler", admin_procedure_labels_path, class: 'fr-btn fr-btn--secondary fr-my-1w'
%button.fr-btn.fr-my-1w{ type: "submit", form: 'order-admin-procedure-labels-form' } Valider
6 changes: 3 additions & 3 deletions app/views/instructeurs/procedures/order_positions.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
Personnaliser l'ordre des #{@procedures.size} démarches « en cours »
%p Déplacez les démarches dans la liste pour les classer en fonction de vos préférences :

%fr-container{ data: { controller: 'move-procedures-position' } }
%fr-container{ data: { controller: 'move-cards-position' } }
= form_tag update_order_positions_instructeur_procedures_path, method: :patch, id: 'order-instructeur-procedures-form' do
- @procedures.each do |procedure|
.fr-card.fr-mb-1w.fr-py-1w.fr-px-2w
.flex.align-center
%button.fr-btn.fr-icon-arrow-up-line.fr-btn--secondary.fr-col-1{ data: { action: "move-procedures-position#moveUp" } }
%button.fr-btn.fr-icon-arrow-down-line.fr-btn--secondary.fr-col-1.fr-mx-2w{ data: { action: "move-procedures-position#moveDown" } }
%button.fr-btn.fr-icon-arrow-up-line.fr-btn--secondary.fr-col-1{ data: { action: "move-cards-position#moveUp" } }
%button.fr-btn.fr-icon-arrow-down-line.fr-btn--secondary.fr-col-1.fr-mx-2w{ data: { action: "move-cards-position#moveDown" } }
- if procedure.close?
%span.fr-badge.fr-mr-2w Close
- elsif procedure.depubliee?
Expand Down
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,17 @@
get 'preview', on: :member
end

resources :labels, controller: 'labels'
resources :labels, controller: 'labels' do
collection do
get 'order_positions'
patch 'update_order_positions'
end
end

resource :attestation_template, only: [:show, :edit, :update, :create] do
get 'preview', on: :member
end

resource :chorus, only: [:edit, :update] do
get 'add_champ_engagement_juridique'
end
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20250120083052_add_position_to_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddPositionToLabels < ActiveRecord::Migration[7.0]
def change
add_column :labels, :position, :integer
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@
t.string "color"
t.datetime "created_at", null: false
t.string "name"
t.integer "position"
t.bigint "procedure_id", null: false
t.datetime "updated_at", null: false
t.index ["procedure_id"], name: "index_labels_on_procedure_id"
Expand Down
17 changes: 17 additions & 0 deletions spec/models/procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,23 @@
end
end

describe '#update_labels_position' do
let(:procedure) { create(:procedure) }
let!(:labels) { create_list(:label, 5, procedure_id: procedure.id) }

it 'updates the positions of the specified instructeurs_procedures' do
procedure.update_labels_position(labels.map(&:id))

expect(procedure.labels.reload.pluck(:id, :position)).to match_array([
[labels[0].id, 0],
[labels[1].id, 1],
[labels[2].id, 2],
[labels[3].id, 3],
[labels[4].id, 4]
])
end
end

private

def create_dossier_with_pj_of_size(size, procedure)
Expand Down
Loading