From 86b154ad339cc292246e3eeb880ece23b3ab3ad3 Mon Sep 17 00:00:00 2001 From: Nadim Salloum Date: Wed, 16 Jun 2021 14:40:10 +0300 Subject: [PATCH] add plz lookup --- .../Contacts/Components/ContactFormFields.vue | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/resources/js/Pages/Contacts/Components/ContactFormFields.vue b/resources/js/Pages/Contacts/Components/ContactFormFields.vue index 2ceb9f9..3802d68 100644 --- a/resources/js/Pages/Contacts/Components/ContactFormFields.vue +++ b/resources/js/Pages/Contacts/Components/ContactFormFields.vue @@ -73,6 +73,8 @@ import JetLabel from '@/Jetstream/Label.vue'; import JetInput from '@/Jetstream/Input.vue'; import JetInputError from '@/Jetstream/InputError'; +import { isNull } from 'lodash'; +// import { throttle } from 'lodash'; export default { components: { @@ -83,5 +85,26 @@ export default { props: { form: Object, }, + watch: { + 'form.zip': function(newVal, oldVal) { + this.fetchCity(newVal, oldVal); + }, + }, + methods: { + fetchCity(newVal, oldVal) { + if (newVal !== oldVal && newVal.length === 4 && this.form.country === 'CH') { + axios.get(`https://swisspost.opendatasoft.com/api/records/1.0/search/?dataset=plz_verzeichnis_v2&q=&facet=ortbez18&refine.postleitzahl=${newVal}`) + .then((response) => { + let records = response.data.records; + if (records.length > 1) { + records = records.filter(rec => rec.geometry); + } + if (records[0]) { + this.form.city = records[0].fields.ortbez18; + } + }); + } + }, + }, };