45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import ngModule from '../../module';
|
|
|
|
class Controller {
|
|
constructor($http, $scope, $stateParams) {
|
|
this.$http = $http;
|
|
this.$scope = $scope;
|
|
this.$stateParams = $stateParams;
|
|
}
|
|
|
|
setDefault(address) {
|
|
let query = `/client/api/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();
|
|
}
|
|
});
|
|
}
|
|
|
|
isDefaultAddress(address) {
|
|
if (this.client)
|
|
return 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'];
|
|
|
|
ngModule.component('vnClientAddressIndex', {
|
|
template: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
client: '<'
|
|
}
|
|
});
|