Skip to content

Commit

Permalink
feat(champ.referentiel): 1st step of setup wizard [some options are p…
Browse files Browse the repository at this point in the history
…resent but disable. will be enabled once implemented]
  • Loading branch information
mfo committed Dec 23, 2024
1 parent 9e88bf2 commit d9ddf6a
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class TypeDeChampReferentiel::SetupDatasourceComponent < ApplicationComponent
attr_reader :type_de_champ, :procedure
def initialize(type_de_champ:, procedure:)
@type_de_champ = type_de_champ
@procedure = procedure
end

def id
dom_id(type_de_champ, :setup_datasource)
end

def form_options
{ id:, method: :patch, data: { turbo: 'true' }, html: { novalidate: 'novalidate' } }
end

def adatper?(value)
type_de_champ.referentiel_adapter == value
end

private

def force_autosubmit? = adatper?(nil)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
= form_with model: @type_de_champ, url: update_datasource_admin_procedure_type_de_champ_path(@procedure, @type_de_champ.stable_id), **form_options do |form|
%div
= render Dsfr::RadioButtonListComponent.new(form:, target: :referentiel_adapter,
buttons: [ { label: 'À partir d’une URL', value: 'url', hint: 'Connectez un champ à une API', data: {controller: 'autosubmit'} } ,
{ label: 'À partir d’un fichier CSV', value: 'csv', hint: 'Connectez un champ à un CSV', disabled: true }]) do ||
Comment intéroger votre référentiel ?

- if adatper?('url')
= render Dsfr::InputComponent.new(form:, attribute: :referentiel_url)
- elsif adatper?('csv')
.fr-input-group
= form.label :piece_justificative_template, class: 'fr-label', for: dom_id(@type_de_champ, :piece_justificative_template) do
Fichier CSV
%span.fr-text-hint Utilisez le modèle du fichier CSV fourni ci-dessous pour construire votre référentiel (le nombre de colonne n’est pas limité).
= render Attachment::EditComponent.new(attached_file: @type_de_champ.piece_justificative_template, view_as: :link)

%hr.fr-hr.fr-my-5w

%div
= render Dsfr::RadioButtonListComponent.new(form:, target: :referentiel_presenter,
buttons: [ { label: 'Correspondance exacte', value: 'exact_match', hint: 'Vérification de l’existence de la donnée saisie dans la BDD du référentiel (exemple : plaque d’immatriculation, SIREN...)' } ,
{ label: 'Autosuggestion au fur et à mesure de la saisie de l’usager', value: 'autocomplete', hint: 'Affichage de données issues de la BDD du référentiel correspondant en partie ou en totalité à la donnée saisie par l’usager (exemple : BDD de médicaments, modèles de véhicules...)', disabled: true }]) do
Mode de remplissage du champ par l’usager

= render Dsfr::InputComponent.new(form:, attribute: :referentiel_hint)
= render Dsfr::InputComponent.new(form:, attribute: :referentiel_test_data)

%hr.fr-hr.fr-my-5w

%ul.fr-btns-group.fr-btns-group--inline-sm
%li= link_to "Annuler", champs_admin_procedure_path(@procedure), class: 'fr-btn fr-btn--secondary'
%li= form.submit 'Étape suivante', class: 'fr-btn'
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
= form.select :character_limit, options_for_character_limit, {}, { id: dom_id(type_de_champ, :character_limit), class: 'fr-select' }

- if type_de_champ.referentiel?
= render(TypesDeChampEditor::InfoReferentielComponent.new(type_de_champ:))
= render(TypesDeChampEditor::InfoReferentielComponent.new(procedure:, type_de_champ:))

- if type_de_champ.repetition?
.flex.justify-start.section.fr-ml-1w
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# frozen_string_literal: true

class TypesDeChampEditor::InfoReferentielComponent < ApplicationComponent
attr_reader :type_de_champ
attr_reader :procedure, :type_de_champ

def initialize(type_de_champ:)
def initialize(procedure:, type_de_champ:)
@procedure = procedure
@type_de_champ = type_de_champ
end

def setup_datasource_url
setup_datasource_admin_procedure_type_de_champ_path(procedure, type_de_champ.stable_id)
end

def configured?
false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
%li ou uniquement vérifier la correspondance exacte de saisie dans la BDD du référentiel

%p.flex.column.align-end
= link_to "Configurer le champ", "", class: 'fr-btn fr-btn--secondary fr-btn--icon-left fr-icon-equalizer-fill'
= link_to "Configurer le champ", setup_datasource_url, class: 'fr-btn fr-btn--secondary fr-btn--icon-left fr-icon-equalizer-fill'
%hr.fr-hr
20 changes: 20 additions & 0 deletions app/controllers/administrateurs/types_de_champ_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,28 @@ def destroy
end
end

def setup_datasource
@procedure = draft.procedure
@type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id])

render layout: "empty_layout"
end

def update_datasource
type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id])
type_de_champ.update(types_de_champ_referentiel_params)

component = TypeDeChampReferentiel::SetupDatasourceComponent.new(type_de_champ:, procedure: draft.procedure)
render turbo_stream: turbo_stream.replace(component.id, component)
end

private

def types_de_champ_referentiel_params
params.require(:type_de_champ)
.permit(:referentiel_adapter, :referentiel_presenter, :referentiel_url, :referentiel_hint, :referentiel_test_data)
end

def changing_of_type?(type_de_champ)
type_de_champ_update_params['type_champ'].present? && (type_de_champ_update_params['type_champ'] != type_de_champ.type_champ)
end
Expand Down
1 change: 1 addition & 0 deletions app/javascript/entrypoints/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@import '@gouvfr/dsfr/dist/component/translate/translate.css';
@import '@gouvfr/dsfr/dist/component/pagination/pagination.css';
@import '@gouvfr/dsfr/dist/component/skiplink/skiplink.css';
@import '@gouvfr/dsfr/dist/component/stepper/stepper.css';
@import '@gouvfr/dsfr/dist/component/password/password.css';
@import '@gouvfr/dsfr/dist/component/accordion/accordion.css';
@import '@gouvfr/dsfr/dist/component/tab/tab.css';
Expand Down
7 changes: 6 additions & 1 deletion app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ class TypeDeChamp < ApplicationRecord
:expression_reguliere_error_message,
:collapsible_explanation_enabled,
:collapsible_explanation_text,
:header_section_level
:header_section_level,
:referentiel_adapter,
:referentiel_presenter,
:referentiel_url,
:referentiel_hint,
:referentiel_test_data

has_many :revision_types_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ
has_many :revisions, -> { ordered }, through: :revision_types_de_champ
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.fr-container.fr-mt-6w.fr-mb-15w
= link_to " Champs du formulaire", champs_admin_procedure_path(@procedure), class: 'fr-link fr-icon-arrow-left-line fr-link--icon--left fr-icon--sm'
%h3.fr-my-3w
Configuration du champ « #{@type_de_champ.libelle} »

.fr-stepper
%h2.fr-stepper__title
Requête
%span.fr-stepper__state Étape 1 sur 3

.fr-stepper__steps{ data: { "fr-current-step" => "1", "fr-steps" => "3" } }
%p.fr-stepper__details
%span.fr-text--bold Étape suivante :
Réponse et mapping
= render TypeDeChampReferentiel::SetupDatasourceComponent.new(type_de_champ: @type_de_champ, procedure: @procedure)
6 changes: 6 additions & 0 deletions config/locales/models/type_de_champ/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ en:
cojo: "Accreditation Paris 2024"
expression_reguliere: 'Regular expression'
referentiel: 'External data to configure (advanced)'
referentiel_url: "API url"
referentiel_hint: "Hints to share with the user regarding the expected fill in format"
referentiel_test_data: "Valid example (displayed to the user and will be used to test the API"
hints:
referentiel_url: "Example : https://api_1.ext/{id}"
referentiel_hint: "Example : ID containing 12 characters including digis and letters (example : PG46YY6YWCX8)"
errors:
type_de_champ:
attributes:
Expand Down
6 changes: 6 additions & 0 deletions config/locales/models/type_de_champ/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ fr:
cojo: "Accréditation Paris 2024"
expression_reguliere: 'Expression régulière'
referentiel: 'Référentiel à configurer (avancé)'
referentiel_url: "URL de l'API"
referentiel_hint: "Indications à fournir à l’usager concernant le format de saisie attendu"
referentiel_test_data: "Exemple de saisie valide (affiché à l'usager et utilisé pour tester la requête)"
hints:
referentiel_url: "Exemple : https://api_1.ext/{id}"
referentiel_hint: "Exemple : Identifiant de 12 caractères comportant des chiffres et des lettres (exemple : PG46YY6YWCX8)"
errors:
type_de_champ:
attributes:
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,9 @@
patch :move_down
put :piece_justificative_template
put :notice_explicative

get :setup_datasource
patch :update_datasource
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/controllers/administrateurs/types_de_champ_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,33 @@ def morpheds
end
end
end

describe '#setup_datasource' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :referentiel, stable_id: 123 }]) }
it 'works' do
get :setup_datasource, params: { procedure_id: procedure.id, stable_id: 123 }
expect(response).to have_http_status(:success)
end
end

describe '#update_datasource' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :referentiel, stable_id: 123 }]) }
it 'works' do
type_de_champ_params = {
referentiel_adapter: 'url',
referentiel_presenter: 'exact_match',
referentiel_url: 'https://rnb-api.beta.gouv.fr/api/alpha/buildings/{id}/',
referentiel_hint: 'Identifiant unique du bâtiment dans le RNB, composé de 12 chiffre et lettre',
referentiel_test_data: 'PG46YY6YWCX8'
}
patch :update_datasource, params: { procedure_id: procedure.id, stable_id: 123, type_de_champ: type_de_champ_params }, format: :turbo_stream
tdc = procedure.draft_revision.types_de_champ.first

expect(tdc.referentiel_adapter).to eq(type_de_champ_params[:referentiel_adapter])
expect(tdc.referentiel_presenter).to eq(type_de_champ_params[:referentiel_presenter])
expect(tdc.referentiel_url).to eq(type_de_champ_params[:referentiel_url])
expect(tdc.referentiel_hint).to eq(type_de_champ_params[:referentiel_hint])
expect(tdc.referentiel_test_data).to eq(type_de_champ_params[:referentiel_test_data])
end
end
end

0 comments on commit d9ddf6a

Please sign in to comment.