Merge pull request #5839 from tchak/improuve-annuaire-education

Improuve annuaire education
This commit is contained in:
Paul Chavard 2021-01-15 12:06:01 +01:00 committed by GitHub
commit 31a31f65a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 82 additions and 22 deletions

View file

@ -323,8 +323,8 @@ module Users
def champs_params
params.permit(dossier: {
champs_attributes: [
:id, :value, :primary_value, :secondary_value, :piece_justificative_file, value: [],
champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, value: []]
:id, :value, :external_id, :primary_value, :secondary_value, :piece_justificative_file, value: [],
champs_attributes: [:id, :_destroy, :value, :external_id, :primary_value, :secondary_value, :piece_justificative_file, value: []]
]
})
end

View file

@ -28,22 +28,38 @@ function ComboSearch({
transformResults = defaultTransformResults
}) {
const label = scope;
const hiddenField = useMemo(
const hiddenValueField = useMemo(
() => document.querySelector(`input[data-uuid="${hiddenFieldId}"]`),
[hiddenFieldId]
);
const initialValue = hiddenField && hiddenField.value;
const hiddenIdField = useMemo(
() =>
document.querySelector(
`input[data-uuid="${hiddenFieldId}"] + input[data-reference]`
),
[hiddenFieldId]
);
const initialValue = hiddenValueField && hiddenValueField.value;
const [searchTerm, setSearchTerm] = useState('');
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState('');
const [value, setValue] = useState(initialValue);
const resultsMap = useRef({});
const setExternalValue = useCallback((value) => {
if (hiddenField) {
hiddenField.setAttribute('value', value);
fire(hiddenField, 'autosave:trigger');
if (hiddenValueField) {
hiddenValueField.setAttribute('value', value);
fire(hiddenValueField, 'autosave:trigger');
}
});
const setExternalId = useCallback((key) => {
if (hiddenIdField) {
hiddenIdField.setAttribute('value', key);
}
});
const setExternalValueAndId = useCallback((value) => {
const [key, result] = resultsMap.current[value];
setExternalId(key);
setExternalValue(value);
if (onChange) {
const result = resultsMap.current[value];
onChange(value, result);
}
});
@ -62,6 +78,7 @@ function ComboSearch({
if (value.length >= minimumInputLength) {
setSearchTerm(value.trim());
if (allowInputValues) {
setExternalId('');
setExternalValue(value);
}
}
@ -70,7 +87,7 @@ function ComboSearch({
);
const handleOnSelect = useCallback((value) => {
setExternalValue(value);
setExternalValueAndId(value);
setValue(value);
});
@ -95,7 +112,7 @@ function ComboSearch({
<ComboboxList>
{results.map((result) => {
const [key, str] = transformResult(result);
resultsMap.current[str] = result;
resultsMap.current[str] = [key, result];
return (
<ComboboxOption
key={key}

View file

@ -1,14 +1,14 @@
class AnnuaireEducationUpdateJob < ApplicationJob
def perform(champ)
search_term = champ.value
external_id = champ.external_id
if search_term.present?
data = ApiEducation::AnnuaireEducationAdapter.new(search_term).to_params
if external_id.present?
data = ApiEducation::AnnuaireEducationAdapter.new(external_id).to_params
if data.present?
champ.data = data
else
champ.value = nil
champ.external_id = nil
end
champ.save!
end

View file

@ -7,8 +7,8 @@ class ApiEducation::AnnuaireEducationAdapter
end
end
def initialize(search_term)
@search_term = search_term
def initialize(id)
@id = id
end
def to_params
@ -27,7 +27,7 @@ class ApiEducation::AnnuaireEducationAdapter
private
def data_source
@data_source ||= JSON.parse(ApiEducation::API.search_annuaire_education(@search_term), symbolize_names: true)
@data_source ||= JSON.parse(ApiEducation::API.get_annuaire_education(@id), symbolize_names: true)
end
def schemer

View file

@ -2,8 +2,8 @@ class ApiEducation::API
class ResourceNotFound < StandardError
end
def self.search_annuaire_education(search_term)
call([API_EDUCATION_URL, 'search'].join('/'), 'fr-en-annuaire-education', { q: search_term })
def self.get_annuaire_education(id)
call([API_EDUCATION_URL, 'search'].join('/'), 'fr-en-annuaire-education', { 'refine.identifiant_de_l_etablissement': id })
end
private

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#
@ -22,13 +23,13 @@ class Champs::AnnuaireEducationChamp < Champs::TextChamp
private
def cleanup_if_empty
if value_changed?
if external_id_changed?
self.data = nil
end
end
def fetch_data
if value.present? && data.nil?
if external_id.present? && data.nil?
AnnuaireEducationUpdateJob.perform_later(self)
end
end

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -12,6 +12,7 @@
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
#

View file

@ -1,3 +1,4 @@
- hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } }
= react_component("ComboAdresseSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)

View file

@ -1,3 +1,4 @@
- hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } }
= react_component("ComboAnnuaireEducationSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id )

View file

@ -1,3 +1,4 @@
- hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } }
= react_component("ComboCommunesSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)

View file

@ -1,3 +1,4 @@
- hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } }
= react_component("ComboDepartementsSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)

View file

@ -1,3 +1,4 @@
- hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } }
= react_component("ComboRegionsSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)

View file

@ -0,0 +1,5 @@
class AddExternalIdToChamps < ActiveRecord::Migration[6.0]
def change
add_column :champs, :external_id, :string
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_01_13_150013) do
ActiveRecord::Schema.define(version: 2021_01_14_224721) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -144,6 +144,7 @@ ActiveRecord::Schema.define(version: 2021_01_13_150013) do
t.bigint "parent_id"
t.integer "row"
t.jsonb "data"
t.string "external_id"
t.index ["dossier_id"], name: "index_champs_on_dossier_id"
t.index ["parent_id"], name: "index_champs_on_parent_id"
t.index ["private"], name: "index_champs_on_private"