import ngModule from '../../module'; import './style.scss'; class Controller { constructor($http, $scope, $stateParams, $translate, vnApp) { this.$http = $http; this.$scope = $scope; this.$stateParams = $stateParams; this.$translate = $translate; this.vnApp = vnApp; 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.$stateParams.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.$translate.instant('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); }); } } Controller.$inject = ['$http', '$scope', '$stateParams', '$translate', 'vnApp']; ngModule.component('vnClientAddressIndex', { template: require('./index.html'), controller: Controller, bindings: { client: '<' } });