refactor(demarches): use turbo_stream template

This commit is contained in:
Paul Chavard 2023-01-06 17:53:20 +01:00
parent 07001e8866
commit 5128b9a086
7 changed files with 32 additions and 20 deletions

View file

@ -333,10 +333,10 @@ module Administrateurs
def detail
@procedure = Procedure.find(params[:id])
render turbo_stream: [
turbo_stream.remove("procedure_detail_#{@procedure.id}"),
turbo_stream.replace("procedure_#{@procedure.id}", partial: "detail", locals: { procedure: @procedure, show_detail: params[:show_detail] })
]
@show_detail = params[:show_detail]
respond_to do |format|
format.turbo_stream
end
end
def all

View file

@ -1,5 +1,5 @@
import { ApplicationController } from './application_controller';
import { toggle } from '@utils';
import { show, hide } from '@utils';
const AUTOSUBMIT_DEBOUNCE_DELAY = 5000;
export class AutosubmitController extends ApplicationController {
@ -7,6 +7,7 @@ export class AutosubmitController extends ApplicationController {
declare readonly formTarget: HTMLFormElement;
declare readonly spinnerTarget: HTMLElement;
declare readonly hasSpinnerTarget: boolean;
submit() {
this.formTarget.requestSubmit();
@ -18,7 +19,14 @@ export class AutosubmitController extends ApplicationController {
connect() {
this.onGlobal('turbo:submit-start', () => {
toggle(this.spinnerTarget);
if (this.hasSpinnerTarget) {
show(this.spinnerTarget);
}
});
this.onGlobal('turbo:submit-end', () => {
if (this.hasSpinnerTarget) {
hide(this.spinnerTarget);
}
});
}
}

View file

@ -6,4 +6,10 @@ class ProcedureDetail < OpenStruct
[I18n.t(attribute, scope: 'activerecord.attributes.procedure_export'), attribute]
end
end
def administrateurs
Administrateurs.new(count: admin_count)
end
Administrateurs = Struct.new(:count)
end

View file

@ -1,7 +1,11 @@
%tr.procedure{ id: "procedure_#{procedure.id}" }
%td
= button_to detail_admin_procedure_path(procedure["id"]), params: show_detail ? {} : { show_detail: true }, method: :get, title: 'Cacher les détails', class: [ show_detail ? 'fr-icon-subtract-line':'fr-icon-add-line', "fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline"] do
Cacher les détails
- title = show_detail ? 'Cacher les détails de la démarche' : 'Afficher les détails de la démarche'
- icon = show_detail ? 'fr-icon-subtract-line' : 'fr-icon-add-line'
- params = show_detail ? {} : { show_detail: true }
= button_to detail_admin_procedure_path(procedure["id"]), method: :post, params:, title:, class: [icon, "fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline" ] do
= title
%td= procedure.libelle
%td= procedure.id
%td= procedure.administrateurs.count

View file

@ -1,6 +1,6 @@
- content_for :results do
.main-filter-header.fr-my-3w
= form_with(url: all_admin_procedures_path, method: :get, html: {'data-autosubmit-target': 'form', role: 'search', class: 'search' }) do |f|
= form_with(url: all_admin_procedures_path, method: :get, html: { 'data-autosubmit-target': 'form', role: 'search', class: 'search' }) do |f|
- @filter.zone_ids&.each do |zone_id|
= hidden_field_tag 'zone_ids[]', zone_id
- @filter.statuses&.each do |status|
@ -42,13 +42,5 @@
%th{ scope: 'col' } Date
%tbody{ 'data-turbo': 'true' }
- @procedures.each do |procedure|
%tr.procedure{ id: "procedure_#{procedure['id']}" }
%td
= button_to detail_admin_procedure_path(procedure["id"]), params: { show_detail: true}, method: :get, title: 'Afficher details', class: "fr-icon-add-line fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline" do
Afficher details procedure
%td= procedure.libelle
%td= procedure.id
%td= procedure.admin_count
%td= t procedure.aasm_state, scope: 'activerecord.values.procedure.aasm_state'
%td= l(procedure.published_at, format: :message_date_without_time)
= render partial: 'detail', locals: { procedure: procedure, show_detail: false }
.fr-mt-2w= paginate @procedures, views_prefix: 'administrateurs'

View file

@ -1 +1,3 @@
= turbo_stream.after("procedure_#{@procedure.id}", partial: "detail", locals: { procedure: @procedure })
- if !@show_detail
= turbo_stream.remove "procedure_detail_#{@procedure.id}"
= turbo_stream.replace "procedure_#{@procedure.id}", partial: "detail", locals: { procedure: @procedure, show_detail: @show_detail }

View file

@ -459,7 +459,7 @@ Rails.application.routes.draw do
end
member do
get 'detail'
post 'detail'
get 'apercu'
get 'champs'
get 'zones'