Sha256: 83b75fb08c30781d803334806c51983d41147ec7e29e9f8a4dca053b8947214e

Contents?: true

Size: 872 Bytes

Versions: 2

Compression:

Stored size: 872 Bytes

Contents

const inputs = {
  street: 'logradouro',
  neighborhood: 'bairro',
  city: 'localidade',
  state: 'uf',
  ibge: 'ibge',
};

const ready = () => {
  const zipCodeInput = $('[data-provider="zipcode"]');
  zipCodeInput.keyup(() => {
    const zipcode = zipCodeInput.val().replace(/[^0-9]/g, '');

    if(zipcode.length === 8) {
      $.get(`https://viacep.com.br/ws/${zipcode}/json`).then(response => {
        let event;
        if (response.erro) {
          event = new Event('zipcode.error');
        } else {
          event = new Event('zipcode.success');
        }

        document.dispatchEvent(event);

        for(var key in inputs) {
          $(`[data-provider="'${key}'"]`).val(response[inputs[key]]);
        }
      });
    };
  })
};

if (typeof Turbolinks == "undefined") {
  $(document).ready(ready);
} else {
  $(document).on("turbolinks:load", ready);
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
autocomplete_zipcode-1.2.0 vendor/assets/javascripts/autocomplete_zipcode.js
autocomplete_zipcode-1.1.0 vendor/assets/javascripts/autocomplete_zipcode.js