refactor(champ): update champ annuaire education

This commit is contained in:
Paul Chavard 2024-05-06 18:09:44 +02:00
parent 79a65a4847
commit f1f1af4e61
No known key found for this signature in database
6 changed files with 57 additions and 23 deletions

View file

@ -1,12 +1,15 @@
class EditableChamp::AnnuaireEducationComponent < EditableChamp::ComboSearchComponent class EditableChamp::AnnuaireEducationComponent < EditableChamp::EditableChampBaseComponent
def dsfr_input_classname def dsfr_input_classname
'fr-input' 'fr-select'
end end
def react_input_opts def react_props
opts = input_opts(id: @champ.input_id, required: @champ.required?, aria: { describedby: @champ.describedby_id }) react_input_opts(id: @champ.input_id,
opts[:className] = "#{opts.delete(:class)} fr-mt-1w" class: "fr-mt-1w",
name: @form.field_name(:external_id),
opts selected_key: @champ.external_id,
items: @champ.selected_items,
loader: data_sources_data_source_education_path,
minimum_input_length: 3)
end end
end end

View file

@ -1,7 +1,3 @@
- render_parent %react-fragment
= render ReactComponent.new "ComboBox/RemoteComboBox", **react_props do
= @form.hidden_field :value = render ReactComponent.new "ComboBox/ComboBoxValueSlot", field: :label, name: @form.field_name(:value)
= @form.hidden_field :external_id
= react_component("ComboAnnuaireEducationSearch",
**react_input_opts,
**react_combo_props)

View file

@ -0,0 +1,38 @@
class DataSources::EducationController < ApplicationController
def search
if params[:q].present? && params[:q].length >= 3
response = fetch_results
if response.success?
results = JSON.parse(response.body, symbolize_names: true)
return render json: format_results(results)
end
end
render json: []
rescue JSON::ParserError => e
Sentry.set_extras(body: response.body, code: response.code)
Sentry.capture_exception(e)
render json: []
end
private
def fetch_results
Typhoeus.get("#{API_EDUCATION_URL}/search", params: { q: params[:q], rows: 5, dataset: 'fr-en-annuaire-education' }, timeout: 3)
end
def format_results(results)
results[:records].map do |record|
fields = record.fetch(:fields)
value = fields.fetch(:identifiant_de_l_etablissement)
{
label: "#{fields.fetch(:nom_etablissement)}, #{fields.fetch(:nom_commune)} (#{value})",
value:,
data: record
}
end
end
end

View file

@ -7,14 +7,11 @@ class Champs::AnnuaireEducationChamp < Champs::TextChamp
APIEducation::AnnuaireEducationAdapter.new(external_id).to_params APIEducation::AnnuaireEducationAdapter.new(external_id).to_params
end end
def update_with_external_data!(data:) def selected_items
if data&.is_a?(Hash) && data['nom_etablissement'].present? && data['nom_commune'].present? && data['identifiant_de_l_etablissement'].present? if external_id.present?
update!( [{ value: external_id, label: value }]
data: data,
value: "#{data['nom_etablissement']}, #{data['nom_commune']} (#{data['identifiant_de_l_etablissement']})"
)
else else
update!(data: data) []
end end
end end
end end

View file

@ -641,8 +641,7 @@ class TypeDeChamp < ApplicationRecord
# We should refresh all champs after update except for champs using react or custom refresh # We should refresh all champs after update except for champs using react or custom refresh
# logic (RNA, SIRET, etc.) # logic (RNA, SIRET, etc.)
case type_champ case type_champ
when type_champs.fetch(:annuaire_education), when type_champs.fetch(:carte),
type_champs.fetch(:carte),
type_champs.fetch(:piece_justificative), type_champs.fetch(:piece_justificative),
type_champs.fetch(:titre_identite), type_champs.fetch(:titre_identite),
type_champs.fetch(:rna), type_champs.fetch(:rna),

View file

@ -251,6 +251,7 @@ Rails.application.routes.draw do
namespace :data_sources do namespace :data_sources do
get :adresse, to: 'adresse#search', as: :data_source_adresse get :adresse, to: 'adresse#search', as: :data_source_adresse
get :commune, to: 'commune#search', as: :data_source_commune get :commune, to: 'commune#search', as: :data_source_commune
get :education, to: 'education#search', as: :data_source_education
get :search_domaine_fonct, to: 'chorus#search_domaine_fonct', as: :search_domaine_fonct get :search_domaine_fonct, to: 'chorus#search_domaine_fonct', as: :search_domaine_fonct
get :search_centre_couts, to: 'chorus#search_centre_couts', as: :search_centre_couts get :search_centre_couts, to: 'chorus#search_centre_couts', as: :search_centre_couts