POST working with prefill query example for repeating fields

This commit is contained in:
Damien Le Thiec 2023-01-31 14:54:23 +01:00
parent 81df033282
commit 20ba96ba3c
10 changed files with 76 additions and 10 deletions

View file

@ -2,6 +2,7 @@ class API::Public::V1::DossiersController < API::Public::V1::BaseController
before_action :retrieve_procedure
def create
byebug
dossier = Dossier.new(
revision: @procedure.active_revision,
groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier,

View file

@ -7,7 +7,7 @@ module DossierPrefillableConcern
return unless champs_public_attributes.any?
attr = { prefilled: true }
attr[:champs_public_all_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) }
attr[:champs_public_all_attributes] = champs_public_attributes.compact.map { |h| h.merge(prefilled: true) }
assign_attributes(attr)
save(validate: false)

View file

@ -50,7 +50,13 @@ class PrefillDescription < SimpleDelegator
end
def prefilled_champs_for_query
prefilled_champs.map { |type_de_champ| "\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\"" } .join(', ')
prefilled_champs.map do |type_de_champ|
if type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:repetition)
"\"champ_#{type_de_champ.to_typed_id}\": #{type_de_champ.example_value}"
else
"\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\""
end
end.join(', ')
end
def active_fillable_public_types_de_champ

View file

@ -11,6 +11,9 @@ class PrefillParams
private
def build_prefill_values
byebug
# nop : "[{\"txt\":\"Texte court\", \"nb\":\"3.14\", \"Test dropdown\":\"Premier choix\", \"régio\":\"53\"}, {\"txt\":\"Texte court\", \"nb\":\"3.14\", \"Test dropdown\":\"Premier choix\", \"régio\":\"53\"}]"
# {"champ_Q2hhbXAtNDI="=>["{\"txt\":\"abc\", \"nb\":\"1,12\"}", "{\"txt\":\"def\", \"nb\":\"2,12\"}"]}
value_by_stable_id = @params
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
.filter { |stable_id, value| stable_id.present? && value.present? }
@ -78,11 +81,12 @@ class PrefillParams
value.map.with_index do |repetition, index|
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
JSON.parse(repetition).map do |key, value|
id = row.find { |champ| champ.libelle == key }.id
id = row.find { |champ| champ.libelle == key }&.id
next unless id
{ id: id, value: value }
end
rescue JSON::ParserError
end.flatten
end.compact
end
end
end

View file

@ -128,6 +128,7 @@ class TypeDeChamp < ApplicationRecord
has_one :procedure, through: :revision
delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelle_for_export, to: :dynamic_type
delegate :active_revision, to: :procedure, prefix: true
class WithIndifferentAccess
def self.load(options)
@ -486,6 +487,12 @@ class TypeDeChamp < ApplicationRecord
end
end
def active_revision_type_de_champ
procedure_active_revision.revision_types_de_champ_public.find do |rtc|
rtc.type_de_champ_id == id
end
end
private
DEFAULT_EMPTY = ['']

View file

@ -0,0 +1,37 @@
class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
include ActionView::Helpers::UrlHelper
def possible_values
prefillable_subchamps.map do |prefill_type_de_champ|
if prefill_type_de_champ.too_many_possible_values?
link = link_to "Voir toutes les valeurs possibles", Rails.application.routes.url_helpers.prefill_type_de_champ_path("piece-jointe", self)
"#{prefill_type_de_champ.libelle}: #{link}"
else
"#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values_sentence}"
end
end
end
def possible_values_sentence
"#{I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")}<br>#{possible_values.join("<br>")}".html_safe
end
def example_value
[row_values_format, row_values_format].map { |row| row.to_s.gsub("=>", ":") }
end
private
def row_values_format
@row_example_value ||=
prefillable_subchamps.map do |prefill_type_de_champ|
[prefill_type_de_champ.libelle, prefill_type_de_champ.example_value.to_s]
end.to_h
end
def prefillable_subchamps
return [] unless active_revision_type_de_champ
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
end
end

View file

@ -9,6 +9,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ)
when TypeDeChamp.type_champs.fetch(:regions)
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ)
when TypeDeChamp.type_champs.fetch(:repetition)
TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ)
else
new(type_de_champ)
end
@ -19,7 +21,9 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
end
def possible_values
[]
return [] unless prefillable?
[I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")]
end
def example_value
@ -31,4 +35,12 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
def too_many_possible_values?
possible_values.count > POSSIBLE_VALUES_THRESHOLD
end
def possible_values_sentence
if too_many_possible_values?
I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html").html_safe
else
possible_values.to_sentence
end
end
end

View file

@ -39,13 +39,10 @@
%th
= t("views.prefill_descriptions.edit.possible_values.title")
%td
- if I18n.exists?("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}_html")
= t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}_html")
%br
= type_de_champ.possible_values_sentence
%br
- if type_de_champ.too_many_possible_values?
= link_to "Voir toutes les valeurs possibles", prefill_type_de_champ_path(prefill_description.path, type_de_champ)
- else
= type_de_champ.possible_values.to_sentence
%tr{ class: prefillable ? "" : "fr-text-mention--grey" }
%th
= t("views.prefill_descriptions.edit.examples.title")

View file

@ -128,6 +128,7 @@ en:
date_html: ISO8601 date
datetime_html: ISO8601 datetime
drop_down_list_other_html: Any value
repetition_html: A array of hashes with possible values for each field of the repetition.
examples:
title: Example
text: Short text

View file

@ -119,6 +119,7 @@ fr:
datetime_html: Datetime au format ISO8601
date_html: Date au format ISO8601
drop_down_list_other_html: Toute valeur
repetition_html: Un array de hash avec les valeurs possibles pour chaque champ de la répétition.
examples:
title: Exemple
text: Texte court