Frontend: rewrite address-based search form

We give up on the autocomplete library we tried in favor of a new one.

We also lose our minds trying to align some FREAKING FLEX-BOXES before
kind of giving up.
This commit is contained in:
Félix Baylac-Jacqué 2022-02-18 18:39:51 +01:00
parent b69ec04aff
commit 75c606b3de
No known key found for this signature in database
GPG key ID: EFD315F31848DBA4
2 changed files with 120 additions and 86 deletions

View file

@ -15,9 +15,11 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.6/dist/autoComplete.min.js"></script> <script src="https://unpkg.com/@trevoreyre/autocomplete-js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.6/dist/css/autoComplete.02.min.css"> <link
rel="stylesheet"
href="https://unpkg.com/@trevoreyre/autocomplete-js/dist/style.css"
/>
<title>Aquilenet: Éligibilité FTTH</title> <title>Aquilenet: Éligibilité FTTH</title>
<style> <style>
{% include 'style.css'%} {% include 'style.css'%}
@ -66,27 +68,40 @@
<div id="methodAddress" class="collapse testMethods"> <div id="methodAddress" class="collapse testMethods">
<h2 class="text-center form-title">Test d'éligibilité par adresse</h2> <h2 class="text-center form-title">Test d'éligibilité par adresse</h2>
<form id="formAddressTest" method="post" action="/test/address"> <form id="formAddressTest" method="post" action="/test/address">
<div class="form-group row" id="communeForm"> <div id="commune-form">
<label class="form-label col-sm-2 my-1" for="commune-autocomplete">Commune</label> <label class="form-label" for="commune-autocomplete-input">Commune</label>
<input id="commune-autocomplete" class="col-sm-9" type="search" dir="ltr" spellcheck=false autocorrect="off" autocomplete="off" class="form-control" autocapitalize="off"/> <div class="autocomplete" id="commune-autocomplete">
<input id="commune-autocomplete-input" class="autocomplete-input"
spellcheck="false" autocorrect="off" autocomplete="off"
autocapitalize="off" placeholder="Nom/Code Postal de la Commune"/>
<ul class="autocomplete-result-list"/>
</div> </div>
<div class="form-row collapse" id="voieForm"> </div>
<div class="col-sm-3 my-1"> <div class="collapse" id="voieForm">
<div id="voieFormContent">
<div id="street-number-form">
<label class="form-label" for="numeroVoieInput">Numéro de voie</label> <label class="form-label" for="numeroVoieInput">Numéro de voie</label>
<input required type="text" name="numeroVoie" class="form-control" id="numeroVoieInput" <input required type="text" name="numeroVoie" class="form-control" id="numeroVoieInput"
aria-describedby="numeroVoieHelp" placeholder="Numéro de voie" aria-describedby="numeroVoieHelp" placeholder="Numéro de voie"
oninvalid="this.setCustomValidity('Veuillez renseigner le numéro de voie')" oninvalid="this.setCustomValidity('Veuillez renseigner le numéro de voie')"
oninput="setCustomValidity('')"> oninput="setCustomValidity('')">
</div> </div>
<div class="col-sm-7 my-1"> <div id="street-name-form">
<label class="form-label" for="fantoir-autocomplete">Nom de voie</label> <label class="form-label" for="street-name-autocomplete-input">Nom de voie</label>
<input id="fantoir-autocomplete" class="form-control" type="search" dir="ltr" spellcheck=false autocorrect="off" autocomplete="off" autocapitalize="off"/> <div class="autocomplete" id="street-name-autocomplete">
<input id="street-name-autocomplete-input" class="autocomplete-input"
spellcheck="false" autocorrect="off" autocomplete="off"
autocapitalize="off" placeholder="Nom de voie"/>
<ul class="autocomplete-result-list"/>
</div>
</div> </div>
</div> </div>
<br> </div>
<button id="btnTestAdresse" type="submit" class="btn btn-sable collapse">Tester l'adresse</button> <div id="btnTestAdresseContainer"><button id="btnTestAdresse" type="submit" class="btn btn-sable collapse">Tester l'adresse</button></div>
</form> </form>
<br> <br>
<button id="buttonReturnPto" type="button" data-toggle="collapse" data-target=".testMethods" <button id="buttonReturnPto" type="button" data-toggle="collapse" data-target=".testMethods"
class="btn btn-link page-nav-btn" aria-expanded="false" aria-controls="methodPto methodAddress">Revenir au test par class="btn btn-link page-nav-btn" aria-expanded="false" aria-controls="methodPto methodAddress">Revenir au test par
@ -99,18 +114,12 @@
<script> <script>
$(document).ready(function () { $(document).ready(function () {
// AbortController allows to cancel promises
const controllerCommunes = new AbortController();
const { signalCommunes } = controllerCommunes;
const controllerVoies = new AbortController();
const { signalVoies } = controllerVoies;
// Global variables // Global variables
var communes = [] let communes = []
var codeInsee = 0 let codeInsee = 0
var codeRivoli = "" let codeRivoli = ""
var voies = [] let voies = []
var voie = "" let voie = ""
function sanitizeInputStr(inputStr) { function sanitizeInputStr(inputStr) {
@ -134,7 +143,7 @@
// add spinner to button // add spinner to button
$(buttonSelector).html( $(buttonSelector).html(
`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>${buttonText}` `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>${buttonText}`
); );
} }
$('#methodAddress').on('show.bs.collapse', function () { $('#methodAddress').on('show.bs.collapse', function () {
@ -152,72 +161,43 @@
$('#numeroVoieInput').focus(); $('#numeroVoieInput').focus();
}); });
const autoCompleteCommune = new autoComplete({ const communeAutocompleteOptions = {
selector: "#commune-autocomplete", search: async (query) => {
placeHolder: "Code postal/nom de commune...", const api = "addresses/communes?limit=100";
data: { const reqUrl = query === '' ? api : `${api}&s=${query}`;
src: async (query) => { const source = await fetch(reqUrl);
const api = "addresses/communes?limit=100"; const data = await source.json();
const reqUrl = query === '' ? api : api + "&s=" + query return data;
const source = await fetch(reqUrl, { signalCommunes });
const data = await source.json();
return data;
},
keys: [ "codeZip", "nom" ],
}, },
resultList: { onSubmit: (res) => {
element: (list, data) => { const searchInput = $('#commune-autocomplete-input');
} searchInput.val(`${res.codeZip} - ${res.nom}`);
$("#voieForm").collapse("show");
codeInsee = res.codeInsee;
}, },
resultItem: { debounceTime: 300,
highlight: true renderResult: (res, props) => `<li ${props}>${res.codeZip} - ${res.nom}</li>`
}, };
debounce: 300, const autocompleteCommune = new Autocomplete("#commune-autocomplete", communeAutocompleteOptions);
events: {
input: {
selection: (event) => {
const selection = event.detail.selection.value;
autoCompleteCommune.input.value = selection.codeZip + " - " + selection.nom ;
codeInsee = selection.codeInsee;
$("#voieForm").collapse("show");
}
}
}
});
const autoCompleteFantoir = new autoComplete({ const voieAutocompleteOptions = {
selector: "#fantoir-autocomplete", search: async (query) => {
placeHolder: "Nom de la voie...", const api = `addresses/fantoirvoies/${codeInsee}?limit=100`;
data: { const reqUrl = query === '' ? api : `${api}&s=${query}`;
src: async (query) => { const source = await fetch(reqUrl);
const api = "addresses/fantoirvoies/" + codeInsee + "?limit=100"; const data = await source.json();
const reqUrl = query === '' ? api : api + "&s=" + query return Object.entries(data).map(e => {return {"name": e[0], "value": e[1]}; });
const source = await fetch(reqUrl, { signalVoies });
const data = await source.json();
return Object.entries(data).map(e => {return {"name": e[0], "value": e[1]}; });
},
keys: [ "name" ],
}, },
resultList: { onSubmit: (res) => {
element: (list, data) => { const searchInput = $("#street-name-autocomplete-input");
} searchInput.val(res.name);
codeRivoli=res.value;
$('#btnTestAdresse').collapse('show');
}, },
resultItem: { debounceTime: 300,
highlight: true renderResult: (res, props) => `<li ${props}>${res.name}</li>`
}, };
debounce: 300, const autocompleteVoie = new Autocomplete("#street-name-autocomplete", voieAutocompleteOptions);
events: {
input: {
selection: (event) => {
const selection = event.detail.selection.value;
autoCompleteFantoir.input.value = selection.name;
codeRivoli = selection.value;
$('#btnTestAdresse').collapse('show');
}
}
}
});
$('#formAddressTest').submit(function(eventObj) { $('#formAddressTest').submit(function(eventObj) {
setButtonSpinner("#btnTestAdresse", "Test..."); setButtonSpinner("#btnTestAdresse", "Test...");
$('#formAddressTest').append(`<input type="hidden" class="form-control" name="codeInsee" value="${codeInsee}" />`); $('#formAddressTest').append(`<input type="hidden" class="form-control" name="codeInsee" value="${codeInsee}" />`);

View file

@ -99,3 +99,57 @@ td {
padding-left: 1em; padding-left: 1em;
padding-right: 1em; padding-right: 1em;
} }
/*
********************
Form Styles
*******************
*/
#commune-form {
margin-top: 2em;
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
#commune-autocomplete {
margin-left: 1em;
width: 65%;
}
#voieFormContent {
display: flex;
flex-direction: row;
align-items: center;
margin-top: 1em;
width: 100%;
justify-content: center;
}
#voieFormContent > div {
margin-right: 1em;
}
#street-number-form {
width: 20%;
}
#street-name-form {
width: 51%;
}
#numeroVoieInput {
padding: 12px;
height: 3em;
}
#btnTestAdresseContainer {
margin-top: 2em;
display: flex;
width: 100%;
justify-content: center;
flex-direction: row;
}