feat(conditional): show loader during autosave when other champs are dependent of it

This commit is contained in:
Colin Darie 2022-12-16 12:19:49 +01:00
parent 81f00774af
commit 7c13449b4f
9 changed files with 34 additions and 3 deletions

View file

@ -2,4 +2,8 @@ class EditableChamp::EditableChampBaseComponent < ApplicationComponent
def initialize(form:, champ:, seen_at: nil)
@form, @champ, @seen_at = form, champ, seen_at
end
def data_dependent_conditions
{ "dependent-conditions": "true" } if @champ.dependent_conditions?
end
end

View file

@ -2,4 +2,5 @@
id: @champ.input_id,
aria: { describedby: @champ.describedby_id },
placeholder: 5,
required: @champ.required?
required: @champ.required?,
data: data_dependent_conditions

View file

@ -2,4 +2,5 @@
id: @champ.input_id,
aria: { describedby: @champ.describedby_id },
placeholder: @champ.libelle,
required: @champ.required?
required: @champ.required?,
data: data_dependent_conditions

View file

@ -1,4 +1,5 @@
= @form.text_field :value,
id: @champ.input_id,
required: @champ.required?,
aria: { describedby: @champ.describedby_id }
aria: { describedby: @champ.describedby_id },
data: data_dependent_conditions

View file

@ -82,6 +82,8 @@ class RootController < ApplicationController
champ.association(:dossier).target = @dossier
end
end
@dossier.association(:revision).target = @dossier.procedure.build_draft_revision
end
def suivi

View file

@ -101,6 +101,12 @@ export class AutosaveController extends ApplicationController {
isTextInputElement(target)
) {
this.debounce(this.enqueueAutosaveRequest, AUTOSAVE_DEBOUNCE_DELAY);
if (target.dataset.dependentConditions) {
const spinner = document.createElement('div');
spinner.classList.add('spinner', 'right');
target.after(spinner);
}
}
}

View file

@ -213,6 +213,10 @@ class Champ < ApplicationRecord
type_de_champ.read_attribute_before_type_cast('condition').present?
end
def dependent_conditions?
dossier.revision.dependent_conditions(type_de_champ).any?
end
def visible?
# Huge gain perf for cascade conditions
return @visible if instance_variable_defined? :@visible

View file

@ -185,6 +185,16 @@ class ProcedureRevision < ApplicationRecord
end
end
def dependent_conditions(tdc)
stable_id = tdc.stable_id
(tdc.public? ? types_de_champ_public : types_de_champ_private).filter do |other_tdc|
next if !other_tdc.condition?
other_tdc.condition.sources.include?(stable_id)
end
end
# Estimated duration to fill the form, in seconds.
#
# If the revision is locked (i.e. published), the result is cached (because type de champs can no longer be mutated).

View file

@ -6,3 +6,5 @@
= fields_for champ.input_name, champ do |form|
= turbo_stream.morph champ.input_group_id do
= render EditableChamp::EditableChampComponent.new champ:, form:
= turbo_stream.remove_all(".editable-champ .spinner")