Use delegation in SIRET champ code

This commit is contained in:
Paul Chavard 2018-08-16 18:04:18 +02:00
parent e8e62b0e63
commit 2500cd516b

View file

@ -1,37 +1,35 @@
import $ from 'jquery';
addEventListener('turbolinks:load', () => {
$('[data-siret]').on('input', evt => {
$(document).on('input', '[data-siret]', evt => {
const input = $(evt.target);
const value = input.val();
const url = input.attr('data-siret');
const url = input.data('siret');
switch (value.length) {
case 0:
input.removeAttr('data-invalid');
$.get(`${url}?siret=blank`);
input.removeData('invalid');
$.get(url, { siret: 'blank' });
break;
case 14:
input.attr('disabled', 'disabled');
input.attr('disabled', true);
$('.spinner').show();
$.get(`${url}?siret=${value}`).then(
$.get(url, { siret: value }).then(
() => {
input.removeAttr('data-invalid');
input.removeData('invalid');
input.removeAttr('disabled');
$('.spinner').hide();
},
() => {
input.removeAttr('disabled');
input.attr('data-invalid', true);
input.data('invalid', true);
$('.spinner').hide();
}
);
break;
default:
if (!input.attr('data-invalid')) {
input.attr('data-invalid', true);
$.get(`${url}?siret=invalid`);
if (!input.data('invalid')) {
input.data('invalid', true);
$.get(url, { siret: 'invalid' });
}
}
});
});