diff --git a/source/components/conversation/select/Select.js b/source/components/conversation/select/SelectGéo.js similarity index 80% rename from source/components/conversation/select/Select.js rename to source/components/conversation/select/SelectGéo.js index 82807388d..ab8336113 100644 --- a/source/components/conversation/select/Select.js +++ b/source/components/conversation/select/SelectGéo.js @@ -8,7 +8,9 @@ import './Select.css' let getOptions = input => input.length < 3 ? Promise.resolve({ options: [] }) - : fetch(`https://geo.api.gouv.fr/communes?nom=${input}`) + : fetch( + `https://geo.api.gouv.fr/communes?nom=${input}&fields=nom,code,departement,region&` + ) .then(response => { if (!response.ok) return [{ nom: 'Aucune commune trouvée', disabled: true }] @@ -31,7 +33,8 @@ export default class Select extends Component { submit } = this.props, submitOnChange = option => { - onChange(option) + // serialize to not mix our data schema and the API response's + onChange(JSON.stringify(option)) submit() } @@ -40,8 +43,8 @@ export default class Select extends Component { - nom + ` (${codeDepartement})` + optionRenderer={({ nom, departement }) => + nom + ` (${departement.nom})` } placeholder="Entrez le nom de commune" noResultsText="Nous n'avons trouvé aucune commune" diff --git a/source/engine/generateQuestions.js b/source/engine/generateQuestions.js index e1b72b21a..30eceb254 100644 --- a/source/engine/generateQuestions.js +++ b/source/engine/generateQuestions.js @@ -22,7 +22,7 @@ import { import React from 'react' import Question from 'Components/conversation/Question' import Input from 'Components/conversation/Input' -import Select from 'Components/conversation/select/Select' +import SelectGéo from 'Components/conversation/select/SelectGéo' import SelectAtmp from 'Components/conversation/select/SelectTauxRisque' import formValueTypes from 'Components/conversation/formValueTypes' @@ -116,6 +116,9 @@ export let getInputComponent = rules => dottedName => { }} /> ) + if (rule.API && rule.API === 'géo') + return + if (rule.API) throw new Error("Le seul API implémenté est l'API géo") if (rule.format == null) return ( @@ -141,10 +144,6 @@ export let getInputComponent = rules => dottedName => { /> ) - if (rule.api && rule.api == 'géo') return