import ngModule from '../../module'; import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { constructor($element, $) { super($element, $); this.filter = { fields: [ 'id', 'isDefaultAddress', 'isActive', 'nickname', 'street', 'city', 'provinceFk', 'phone', 'mobile', 'isEqualizated' ], order: [ 'isDefaultAddress DESC', 'isActive DESC', 'nickname ASC'], include: [ { relation: 'observations', scope: { include: 'observationType' } }, { relation: 'province', scope: { fields: ['id', 'name'] } } ] }; } onStarClick(event) { event.stopPropagation(); event.preventDefault(); } setDefault(address) { let query = `Clients/${this.$params.id}`; let params = {defaultAddressFk: address.id}; this.$http.patch(query, params).then(res => { if (res.data) { this.client.defaultAddressFk = res.data.defaultAddressFk; this.sortAddresses(); this.vnApp.showSuccess(this.$t('Data saved!')); } }); } isDefaultAddress(address) { return this.client && this.client.defaultAddressFk === address.id; } /** * Sort address by default address */ sortAddresses() { if (!this.client || !this.addresses) return; this.addresses = this.addresses.sort((a, b) => { return this.isDefaultAddress(b) - this.isDefaultAddress(a); }); } exprBuilder(param, value) { switch (param) { case 'search': return /^\d+$/.test(value) ? {id: value} : {nickname: {like: `%${value}%`}}; } } } Controller.$inject = ['$element', '$scope']; ngModule.component('vnClientAddressIndex', { template: require('./index.html'), controller: Controller, bindings: { client: '<' } });