From 3643709a802269bb67d39d1752aa1dcbfca6a4e9 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 24 Feb 2020 11:27:36 +0100 Subject: [PATCH] Added datalist auto completion --- front/core/components/datalist/index.js | 2 +- .../client/front/address/create/index.html | 76 +++++++++---------- modules/client/front/address/create/index.js | 60 ++++++++++----- modules/client/front/address/edit/index.html | 66 ++++++++-------- modules/client/front/address/edit/index.js | 48 +++++++++++- modules/client/front/create/index.html | 11 ++- modules/client/front/create/index.js | 8 +- modules/client/front/fiscal-data/index.html | 22 +++++- modules/client/front/fiscal-data/index.js | 19 +++-- modules/client/front/postcode/index.html | 2 +- modules/client/front/postcode/index.js | 31 ++++---- 11 files changed, 210 insertions(+), 135 deletions(-) diff --git a/front/core/components/datalist/index.js b/front/core/components/datalist/index.js index adf33b50a..d52e6ca23 100644 --- a/front/core/components/datalist/index.js +++ b/front/core/components/datalist/index.js @@ -27,7 +27,7 @@ export default class Datalist extends Textfield { value = value == '' || value == null ? null : value; oldValue = oldValue == '' || oldValue == null ? null : oldValue; - this.refreshSelection(); + if (oldValue === undefined) this.refreshSelection(); if (!value || value === oldValue && this.modelData != null) return; diff --git a/modules/client/front/address/create/index.html b/modules/client/front/address/create/index.html index 1c70a1cbd..383f37d0a 100644 --- a/modules/client/front/address/create/index.html +++ b/modules/client/front/address/create/index.html @@ -39,61 +39,53 @@ - + rule> + {{name}} ({{country.country}}) - - - - + - - + + {{code}} - {{town.name}} ({{town.province.name}}, + {{town.province.country.country}}) + + + + + + { if (this.address.isDefaultAddress) @@ -51,6 +31,46 @@ export default class Controller extends Component { return this.$http.post(`CustomsAgents`, this.newCustomsAgent) .then(res => this.address.customsAgentFk = res.data.id); } + + get town() { + return this._town; + } + + // Town auto complete + set town(selection) { + this._town = selection; + + if (!selection) return; + + const province = selection.province; + const postcodes = selection.postcodes; + + this.address.provinceId = province.id; + + if (postcodes.length === 1) + this.address.postalCode = postcodes[0].code; + } + + get postcode() { + return this._postcode; + } + + // Postcode auto complete + set postcode(selection) { + this._postcode = selection; + + if (!selection) return; + + const town = selection.town; + const province = town.province; + + this.address.city = town.name; + this.address.provinceId = province.id; + } + + onResponse(response) { + this.address.postalCode = response.code; + } } Controller.$inject = ['$element', '$scope']; diff --git a/modules/client/front/address/edit/index.html b/modules/client/front/address/edit/index.html index 035608120..a5a9d8e0a 100644 --- a/modules/client/front/address/edit/index.html +++ b/modules/client/front/address/edit/index.html @@ -65,49 +65,43 @@ value-field="id" label="Province"> - - - - + + {{name}}, {{province.name}} + ({{province.country.country}}) + + + - - - + + {{code}} - {{town.name}} ({{town.province.name}}, + {{town.province.country.country}}) + + + + + + this.$.model.save(true)) @@ -39,6 +35,50 @@ export default class Controller extends Component { return this.$http.post(`CustomsAgents`, this.newCustomsAgent) .then(res => this.address.customsAgentFk = res.data.id); } + + get town() { + return this._town; + } + + // Town auto complete + set town(selection) { + const oldValue = this._town; + this._town = selection; + + if (!selection || oldValue === null || oldValue === undefined) + return; + + const province = selection.province; + const postcodes = selection.postcodes; + + this.address.provinceFk = province.id; + + if (postcodes.length === 1) + this.address.postalCode = postcodes[0].code; + } + + get postcode() { + return this._postcode; + } + + // Postcode auto complete + set postcode(selection) { + const oldValue = this._postcode; + this._postcode = selection; + + if (!selection || oldValue === null || oldValue === undefined) + return; + + const town = selection.town; + const province = town.province; + + this.address.city = town.name; + this.address.provinceFk = province.id; + } + + onResponse(response) { + this.address.postalCode = response.code; + } } ngModule.component('vnClientAddressEdit', { diff --git a/modules/client/front/create/index.html b/modules/client/front/create/index.html index 23676a36f..faffc118f 100644 --- a/modules/client/front/create/index.html +++ b/modules/client/front/create/index.html @@ -96,9 +96,16 @@ show-field="code" rule> - {{code}}, {{town.name}} - {{town.province.name}} - ({{town.province.country.country}}) + {{code}} - {{town.name}} ({{town.province.name}}, + {{town.province.country.country}}) + + + + diff --git a/modules/client/front/create/index.js b/modules/client/front/create/index.js index 3201f7097..a663717d6 100644 --- a/modules/client/front/create/index.js +++ b/modules/client/front/create/index.js @@ -12,10 +12,6 @@ export default class Controller { }; } - onResponse(response) { - this.client.postcode = response.code; - } - onSubmit() { return this.$.watcher.submit().then( json => this.$state.go('client.card.basicData', {id: json.data.id}) @@ -76,6 +72,10 @@ export default class Controller { this.client.provinceFk = province.id; this.client.countryFk = country.id; } + + onResponse(response) { + this.client.postcode = response.code; + } } Controller.$inject = ['$scope', '$state', '$http', '$translate', 'vnApp']; diff --git a/modules/client/front/fiscal-data/index.html b/modules/client/front/fiscal-data/index.html index 98d0e9318..d29e31f64 100644 --- a/modules/client/front/fiscal-data/index.html +++ b/modules/client/front/fiscal-data/index.html @@ -64,7 +64,10 @@ where="{provinceFk: province.selection.id}" show-field="name" value-field="name"> - {{name}} ({{province.name}}, {{province.country.country}}) + + {{name}}, {{province.name}} + ({{province.country.country}}) + - {{code}} - {{town.name}} ({{town.province.name}}, {{town.province.country.country}}) + + {{code}} - {{town.name}} ({{town.province.name}}, + {{town.province.country.country}}) + + + + + @@ -148,3 +161,8 @@ message="Found a client with this data" on-accept="$ctrl.onAcceptDuplication()"> + + + \ No newline at end of file diff --git a/modules/client/front/fiscal-data/index.js b/modules/client/front/fiscal-data/index.js index 42aa8032a..e1669b1fa 100644 --- a/modules/client/front/fiscal-data/index.js +++ b/modules/client/front/fiscal-data/index.js @@ -86,7 +86,8 @@ export default class Controller extends Component { const oldValue = this._province; this._province = selection; - if (!selection || !oldValue) return; + if (!selection || oldValue === undefined) + return; const country = selection.country; @@ -102,7 +103,8 @@ export default class Controller extends Component { const oldValue = this._town; this._town = selection; - if (!selection || !oldValue) return; + if (!selection || oldValue === undefined) + return; const province = selection.province; const country = province.country; @@ -123,17 +125,24 @@ export default class Controller extends Component { set postcode(selection) { const oldValue = this._postcode; this._postcode = selection; - console.log(selection); - if (!selection || !oldValue) return; + console.log(oldValue); + if (!selection || oldValue === undefined) + return; + + console.log('setter'); const town = selection.town; const province = town.province; const country = province.country; - console.log(province.id); + this.client.city = town.name; this.client.provinceFk = province.id; this.client.countryFk = country.id; } + + onResponse(response) { + this.client.postcode = response.code; + } } ngModule.component('vnClientFiscalData', { diff --git a/modules/client/front/postcode/index.html b/modules/client/front/postcode/index.html index 55990281c..fd81534ed 100644 --- a/modules/client/front/postcode/index.html +++ b/modules/client/front/postcode/index.html @@ -1,7 +1,7 @@ + on-accept="$ctrl.onAccept()">
New postcode

Please, ensure you put the correct data!

diff --git a/modules/client/front/postcode/index.js b/modules/client/front/postcode/index.js index bbf0b1953..836ea9a81 100644 --- a/modules/client/front/postcode/index.js +++ b/modules/client/front/postcode/index.js @@ -35,25 +35,20 @@ class Controller extends Component { this.$.postcode.focus(); } - onResponse(response) { - if (response == 'accept') { - try { - if (!this.data.code) - throw new Error(`The postcode can't be empty`); - if (!this.data.townFk) - throw new Error(`The town can't be empty`); + onAccept() { + try { + if (!this.data.code) + throw new Error(`The postcode can't be empty`); + if (!this.data.townFk) + throw new Error(`The town can't be empty`); - this.$http.patch(`postcodes`, this.data).then(response => { - if (response.data) { - this.vnApp.showMessage(this.$translate.instant('The postcode has been saved')); - - this.emit('response', {response: response.data}); - } - }); - } catch (e) { - this.vnApp.showError(this.$translate.instant(e.message)); - return false; - } + this.$http.patch(`postcodes`, this.data).then(res => { + this.vnApp.showMessage(this.$translate.instant('The postcode has been saved')); + this.emit('response', {$response: res.data}); + }); + } catch (e) { + this.vnApp.showError(this.$translate.instant(e.message)); + return false; } return true; }