new address list

This commit is contained in:
Dani Herrero 2017-09-28 14:34:18 +02:00
parent 70e4b22e8a
commit ecf88f6b66
8 changed files with 41 additions and 23 deletions

View File

@ -12,7 +12,6 @@
<vn-title>Address</vn-title>
<vn-horizontal>
<vn-check vn-one label="Enabled" field="$ctrl.address.enabled"></vn-check>
<vn-check vn-one label="Default" field="$ctrl.address.default"></vn-check>
<vn-check vn-one label="Is equalizated" field="$ctrl.address.isEqualizated"></vn-check>
</vn-horizontal>
<vn-horizontal>

View File

@ -1,14 +1,18 @@
<mg-ajax path="/client/api/Clients/{{index.params.id}}/addressesList" options="vnIndex"></mg-ajax>
<mg-ajax path="/client/api/Clients/{{index.params.id}}/addressesList" options="mgIndex"></mg-ajax>
<vn-vertical pad-medium>
<vn-card>
<vn-vertical pad-large>
<vn-horizontal>
<vn-title vn-one>Addresses</vn-title>
</vn-horizontal>
<vn-horizontal ng-repeat="i in index.model" class="pad-medium-top" style="align-items: center;">
<vn-horizontal ng-repeat="i in index.model track by i.id" class="pad-medium-top" style="align-items: center;">
<vn-auto style="border-radius: .5em;" class="pad-small border-solid"
ng-class="{'bg-dark-item': i.default,'bg-opacity-item': !i.enabled && !i.default}">
<vn-horizontal style="align-items: center;">
<vn-none pad-medium-h style="color:#FFA410;">
<i class="material-icons" ng-if="i.default">star</i>
<i class="material-icons pointer" ng-if="!i.default&&i.enabled" vn-tooltip="Set as default" tooltip-position="left" ng-click="$ctrl.setDefault(i.id)">star_border</i>
</vn-none>
<vn-auto>
<div><b>{{::i.consignee}}</b></div>
<div>{{::i.street}}</div>

View File

@ -1,5 +1,19 @@
import ngModule from '../module';
class ClientAddresses {
constructor($http, $scope) {
this.$http = $http;
this.$scope = $scope;
}
setDefault(id) {
this.$http.patch(`/client/api/Addresses/${id}`, {id: id, default: 1}).then(() => {
this.$scope.index.accept();
});
}
}
ClientAddresses.$inject = ['$http', '$scope'];
ngModule.component('vnClientAddresses', {
template: require('./addresses.html')
template: require('./addresses.html'),
controller: ClientAddresses
});

View File

@ -1,3 +1,4 @@
{
"Addresses": "Consignatarios"
"Addresses": "Consignatarios",
"Set as default": "Establecer como predeterminado"
}

View File

@ -1,4 +1,4 @@
<mg-ajax path="/client/api/Clients/filter" options="vnIndex"></mg-ajax>
<mg-ajax path="/client/api/Clients/filter" options="mgIndex"></mg-ajax>
<div margin-medium>
<div style="max-width: 40em; margin: 0 auto;">
<vn-card>

View File

@ -3,7 +3,7 @@ import {module} from '../module';
index.$inject = ['mgIndex'];
function index(mgIndex) {
return Object.assign({}, mgIndex, {
init: 'index.filter={page: 1, size: 4}'
init: 'index.filter={page: 1, size: 5}'
});
}
module.factory('vnIndex', index);

View File

@ -37,7 +37,8 @@ module.exports = function(Client) {
clientFk: id
},
skip: (params.page - 1) * params.size,
limit: params.size
limit: params.size,
order: ['default DESC', 'enabled DESC']
};
Client.app.models.Address.find(filter, function(err, instances) {

View File

@ -16,10 +16,10 @@ module.exports = function(Address) {
next();
}
Address.beforeRemote('prototype.updateAttributes',function(ctx, modelInstance, next){
var data = ctx.req.body;
data.id = ctx.req.params.id;
getAddress(ctx, data, next);
Address.beforeRemote('prototype.patchAttributes',function(ctx, modelInstance, next){
let newData = ctx.req.body;
newData.id = ctx.req.params.id;
getAddress(ctx, newData, next);
});
Address.beforeRemote('findById', function(ctx, modelInstance, next) {
@ -34,19 +34,18 @@ module.exports = function(Address) {
next();
});
function getAddress(ctx, data, next){
var address = Address.findOne( {where: { id: data.id}}, function (err, address){
if(address)
callbackGetAddress(ctx, data, address, next);
function getAddress(ctx, newData, next){
Address.findOne( {where: { id: newData.id}}, function (err, oldData){
if(oldData)
callbackGetAddress(ctx, newData, oldData, next);
});
}
function callbackGetAddress(ctx, data, address, next){
if (data.default){
removeAllDefault(address.client);
next();
function callbackGetAddress(ctx, newData, oldData, next){
if (newData.default){
removeAllDefault(oldData.client, next);
}
else if (address.default && data.hasOwnProperty('default') && !data.default)
else if (oldData.default && newData.hasOwnProperty('default') && !newData.default)
next(generateErrorDefaultAddress());
else
next();
@ -59,8 +58,8 @@ module.exports = function(Address) {
return ctx.instance;
}
function removeAllDefault(cl){
Address.update({client: cl}, {default: false});
function removeAllDefault(client, next){
Address.updateAll({clientFk: client.id, default: true}, {default: false}, next);
}
function generateErrorDefaultAddress(){