$http interceptor

This commit is contained in:
Juan Ferrer Toribio 2017-01-14 00:34:16 +01:00
parent cbb436788a
commit e92b05e852
22 changed files with 160 additions and 148 deletions

View File

@ -1,5 +1,8 @@
<vn-vertical full-height class="bg-content">
<vn-main-menu></vn-main-menu>
<vn-topbar vn-empty></vn-topbar>
<vn-topbar vn-empty>
<vn-spinner enable="$root.loading"></vn-spinner>
<vn-snackbar></vn-snackbar>
<vn-main-menu></vn-main-menu>
</vn-topbar>
<vn-vertical vn-auto ui-view scrollable class="main-view"></vn-vertical>
</vn-vertical>

View File

@ -7,19 +7,35 @@ export const COMPONENT = {
};
module.component(NAME, COMPONENT);
vnAppInterceptor.$inject = ['$q'];
function vnAppInterceptor($q) {
vnAppInterceptor.$inject = ['$q', '$rootScope', '$document'];
function vnAppInterceptor($q, $rootScope, $document) {
$rootScope.loading = false;
function showMessage (message) {
let snackbar = $document.find('vn-snackbar').controller('vnSnackbar');
snackbar.show({message: message});
}
return {
request: function(config) {
$rootScope.loading = true;
return config;
},
requestError: function(rejection) {
return $q.reject(rejection);
},
response: function(response) {
switch(response.config.method)
{
case 'PUT':
case 'POST':
showMessage('Data saved!');
}
$rootScope.loading = false;
return response;
},
responseError: function(rejection) {
$rootScope.loading = false;
let message = rejection.data.error.message;
showMessage(`Error: ${message}`);
return $q.reject(rejection);
}
};

View File

@ -1,2 +1,2 @@
<header class="bg-dark-bar" style="height: 4.2em;">
<header class="bg-dark-bar" style="height: 4.2em; color: white;" ng-transclude>
</header>

View File

@ -2,6 +2,7 @@ import {module} from '../../module';
export const NAME = 'vnTopbar';
export const COMPONENT = {
template: require('./topbar.html')
template: require('./topbar.html'),
transclude: true
};
module.component(NAME, COMPONENT);

View File

@ -1,4 +1,4 @@
import {module as _module} from '../module';
import {module} from '../module';
import * as resolveFactory from '../resolveDefaultComponents';
import * as util from '../util';
@ -11,8 +11,18 @@ export function directive(resolve) {
restrict: 'E',
template: function(_, attrs) {
return resolve.getTemplate(_NAME, attrs);
}
},
controller: controller
}
}
module.directive(NAME, directive);
_module.directive(NAME, directive);
controller.$inject = ['$scope', '$element'];
function controller($scope, $element) {
let snackbar = $element[0].firstChild;
componentHandler.upgradeElement (snackbar);
this.show = function(data) {
snackbar.MaterialSnackbar.showSnackbar(data);
}
}

View File

@ -1,4 +1,4 @@
import {module as _module} from '../module';
import {module} from '../module';
import * as resolveFactory from '../resolveDefaultComponents';
import * as util from '../util';
@ -7,12 +7,35 @@ export const NAME = util.getName(_NAME);
directive.$inject = [resolveFactory.NAME];
export function directive(resolve) {
return{
return {
restrict: 'E',
template: function(_, attrs) {
scope: {
enable: '='
},
template: function(element, attrs) {
return resolve.getTemplate(_NAME, attrs);
}
},
controller: controller
}
}
module.directive(NAME, directive);
_module.directive(NAME, directive);
controller.$inject = ['$scope', '$element'];
function controller($scope, $element) {
let spinner = $element[0].firstChild;
componentHandler.upgradeElement (spinner);
this.start = function() {
spinner.MaterialSpinner.start();
}
this.stop = function() {
spinner.MaterialSpinner.stop();
}
$scope.$watch('enable', (newValue) => {
if (newValue)
this.start();
else
this.stop();
});
}

View File

@ -1,2 +1,5 @@
<div class="*[className]*">
</div>
<div
class="mdl-spinner mdl-spinner--single-color mdl-js-spinner"
ng-class="enable"
*[foo]*>
</div>

View File

@ -1,15 +1,9 @@
import {module} from '../module';
import template from './spinner.mdl.html';
export const NAME = 'vnSpinnerMdlFactory';
export function factory() {
return {
template: template,
default: {
className: 'mdl-spinner mdl-spinner--single-color mdl-js-spinner'
}
template: require('./spinner.mdl.html')
}
}
module.factory(NAME, factory);

View File

@ -8,22 +8,15 @@ export const COMPONENT = {
controller: function($http)
{
this.address = {};
$http.get('/client/api/Agencies').then(
json => this.agencies = json.data,
json => console.error(json.data.error.message)
json => this.agencies = json.data
);
$http.get('/client/api/Provinces').then(
json => this.provinces = json.data,
json => console.error(json.data.error.message)
json => this.provinces = json.data
);
this.submit = function(){
$http.post('/client/api/Addresses', this.address).then(
json => console.log(json.statusText),
json => console.error(json.data.error.message)
);
$http.post('/client/api/Addresses', this.address);
}
}
}

View File

@ -9,26 +9,18 @@ export const COMPONENT = {
{
this.address = {};
$http.get(`/client/api/Addresses/${$stateParams.addressId}`).then(
json => this.address = json.data,
json => console.error(json.data.error.message)
json => this.address = json.data
);
$http.get('/client/api/Agencies').then(
json => this.agencies = json.data,
json => console.error(json.data.error.message)
json => this.agencies = json.data
);
$http.get('/client/api/Provinces').then(
json => this.provinces = json.data,
json => console.error(json.data.error.message)
json => this.provinces = json.data
);
this.submit = function(){
$http.put('/client/api/Addresses', this.address).then(
json => console.log(json.statusText),
json => console.error(json.data.error.message)
);
}
$http.put('/client/api/Addresses', this.address);
}
}
}

View File

@ -11,14 +11,12 @@ export const COMPONENT = {
},
controller: function($http)
{
$http.get('/client/api/Addresses').then(
json => this.addresses = json.data,
json => console.error(json.data.error.message)
$http.get('/client/api/Addresses').then(
json => this.addresses = json.data
);
this.submit = function(){
$http.post('/client/api/Clients', this.model).then(
json => $state.go('clientCard.basicData',{id: json.data.id}),
json => console.error(json.data.error.message)
json => $state.go('clientCard.basicData',{id: json.data.id})
);
};
}

View File

@ -26,8 +26,7 @@ export const COMPONENT = {
}
$http.get('/client/api/SalesPeople').then(
json => this.sales = json.data,
json => console.error(json.data.error.message)
json => this.sales = json.data
);
function callback(transition) {
@ -44,10 +43,7 @@ export const COMPONENT = {
this.submit = function() {
if(!equalsObject(this.client, this.clientOld)){
this.client.modify = "BasicData";
$http.put('/client/api/Clients', this.client).then(
json => console.log(json.statusText),
json => console.error(json.data.error.message)
);
$http.put('/client/api/Clients', this.client);
}
};
}

View File

@ -11,10 +11,7 @@ export const COMPONENT = {
controller: function($http, $stateParams) {
this.client = null;
$http.get(`/client/api/Clients/${$stateParams.id}`).then(
json => {
this.client = json.data;
},
json => console.error(json.data.error.message)
json => this.client = json.data
);
this.items = [];

View File

@ -8,8 +8,7 @@ export const COMPONENT = {
controller: function($http, $state){
this.submit = function(){
$http.post('/client/api/Clients', this.model).then(
json => $state.go('clientCard.basicData',{id: json.data.id}),
json => console.error(json.data.error.message)
json => $state.go('clientCard.basicData',{id: json.data.id})
);
};
}

View File

@ -10,25 +10,17 @@ export const COMPONENT = {
},
controller: function($http) {
$http.get('/client/api/Countries').then(
json => this.countries = json.data,
json => console.error(json.data.error.message)
json => this.countries = json.data
);
$http.get('/client/api/Provinces').then(
json => this.provinces = json.data,
json => console.error(json.data.error.message)
json => this.provinces = json.data
);
$http.get('/client/api/PaymentMethods').then(
json => this.payments = json.data,
json => console.error(json.data.error.message)
json => this.payments = json.data
);
this.submit = function() {
$http.put('/client/api/Clients', this.client).then(
json => console.log(json.statusText),
json => console.error(json.data.error.message)
);
$http.put('/client/api/Clients', this.client);
};
}
};

View File

@ -43,8 +43,7 @@ export const COMPONENT = {
}
$http.get(queryStr).then(
json => this.clients = json.data,
json => console.error(json.data.error.message)
json => this.clients = json.data
);
};
this.filter = {};

View File

@ -1,8 +1,8 @@
<form name="form" ng-submit="form.$valid && note.submit()" pad-medium>
<form name="form" ng-submit="form.$valid && notes.submit()" pad-medium>
<vn-card>
<vn-vertical pad-large>
<vn-title>Notas</vn-title>
<vn-textfield label="Notas" field="note.model.notes" focus padd-medium-top></vn-textfield>
<vn-textfield label="Notas" field="notes.client.notes" focus padd-medium-top></vn-textfield>
<vn-submit margin-large-top label="Guardar"></vn-submit>
</vn-vertical>
</vn-card>

View File

@ -3,7 +3,16 @@ import {module} from '../../module';
export const NAME = 'vnClientNotes';
export const COMPONENT = {
controllerAs: 'note',
template: template
template: template,
controllerAs: 'notes',
bindings: {
client: '<'
},
controller: function($http) {
this.submit = function() {
$http.put('/client/api/Clients', this.client);
};
}
};
COMPONENT.controller.$inject = ['$http'];
module.component(NAME, COMPONENT);

View File

@ -10,10 +10,7 @@ export const COMPONENT = {
},
controller: function($http) {
this.submit = function() {
$http.put('/client/api/Clients', this.client).then(
json => console.log(json.statusText),
json => console.error(json.data.error.message)
);
$http.put('/client/api/Clients', this.client);
};
}
};

View File

@ -8,10 +8,10 @@
<vn-check label="Do not close session" field="login.model.remember"></vn-check>
<div class="footer">
<vn-submit label="Enter"></vn-submit>
<vn-spinner id="spinner"></vn-spinner>
<vn-spinner enable="login.loading"></vn-spinner>
</div>
</form>
</div>
</div>
<vn-snackbar id="snackbar"></vn-snackbar>
<vn-snackbar></vn-snackbar>
</div>

View File

@ -5,71 +5,61 @@ import style from './login.scss'
export const COMPONENT =
{
template: template,
controller: function($http) {
var self = this;
this.submit = function() {
var model = this.model;
if(model && model.email && model.password) {
setLoading(true);
model.appId = window.location.href;
$http.post('/account', this.model).then (
onLoginOk,
onLoginErr
);
}
else
showMessage('Please insert your email and password');
};
function onLoginOk (response) {
setLoading(false);
window.location = response.data.location +'?access_token='+ response.data.accessToken;
}
function onLoginErr(response) {
setLoading(false);
self.model.password = '';
var message;
switch(response.status) {
case 401:
message = 'Invalid credentials';
break;
case -1:
message = 'Can\'t contact with server';
break;
default:
message = 'Something went wrong';
}
showMessage(message);
}
function $(id) {
return document.getElementById(id);
}
function setLoading(isLoading) {
// FIXME: Al minimificar MaterialSpinner es undefined
/* var spinner = $('spinner').firstChild.MaterialSpinner;
if(isLoading)
spinner.start();
else
spinner.stop();
*/ }
function showMessage(message) {
// FIXME: Al minimificar no muestra la barra
var snackbar = $('snackbar').firstChild.MaterialSnackbar;
snackbar.showSnackbar({message: message});
}
},
controller: controller,
controllerAs: 'login'
};
COMPONENT.controller.$inject = ['$http'];
module.component('vnLogin', COMPONENT);
controller.$inject = ['$http', '$element'];
function controller($http, $element) {
var self = this;
this.submit = function() {
let model = this.model;
if(!(model && model.email && model.password)) {
showMessage('Please insert your email and password');
return;
}
this.loading = true;
model.appId = window.location.href;
$http.post('/account', this.model).then (
onLoginOk,
onLoginErr
);
};
function onLoginOk (response) {
self.loading = false;
window.location = response.data.location +'?access_token='+ response.data.accessToken;
}
function onLoginErr(response) {
self.loading = false;
self.model.password = '';
let message;
switch(response.status) {
case 401:
message = 'Invalid credentials';
break;
case -1:
message = 'Can\'t contact with server';
break;
default:
message = 'Something went wrong';
}
showMessage(message);
}
function showMessage(message) {
let snackbar = $element.find('vn-snackbar').controller('vnSnackbar');
snackbar.show({message: message});
}
}
module.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);

View File

@ -18,8 +18,8 @@
"NUf7o684TmteojFX9KmPOpaDLthjP5Def4wuy83Yjv31i43HHiWgV3FyBp6pX8Ue": "{\"id\":\"NUf7o684TmteojFX9KmPOpaDLthjP5Def4wuy83Yjv31i43HHiWgV3FyBp6pX8Ue\",\"ttl\":1209600,\"created\":\"2016-11-21T11:06:11.113Z\",\"userId\":1}"
},
"Client": {
"12": "{\"name\":\"Verdnatura\",\"id\":12,\"fi\":\"B97367486\",\"salesPerson\":\"2\",\"telefono\":\"963242100\",\"socialName\":\"Verdnatura Levante SL\",\"active\":true,\"user\":\"verdnatura\",\"fax\":\"963242100\",\"phone\":\"963242100\",\"email\":\"informatica@verdnatura.es\",\"surcharge\":true,\"cyc\":2345,\"credit\":1000,\"iban\":\"2352345234523452345\",\"street\":\"Avenida Espioca, 100\",\"city\":\"Silla\",\"postcode\":\"46013\",\"mobile\":\"654654654\",\"dueDay\":4,\"gestdoc\":23452343,\"province\":1,\"country\":\"1\",\"modify\":\"BasicData\"}",
"14": "{\"name\":\"Cliente 1\",\"id\":14,\"street\":\"Aaaaaaaaaa\",\"fi\":\"1234567890A\",\"socialName\":\"Cliente 1\",\"fax\":\"963242100\",\"dischargeDate\":\"01/01/2017\",\"telefono\":\"963242100\",\"salesPerson\":\"2\",\"email\":\"informatica@verdnatura.es\",\"city\":\"asdf\",\"postcode\":\"asdf\",\"phone\":\"asdf\",\"mobile\":\"asdf\",\"credit\":2345,\"cyc\":123,\"iban\":\"asdf\",\"dueDay\":345,\"gestdoc\":2435,\"surcharge\":true}"
"12": "{\"name\":\"Verdnatura\",\"id\":12,\"fi\":\"B97367486\",\"salesPerson\":\"2\",\"telefono\":\"963242100\",\"socialName\":\"Verdnatura Levante SL\",\"active\":true,\"user\":\"verdnatura\",\"fax\":\"963242100\",\"phone\":\"963242100\",\"email\":\"informatica@verdnatura.es\",\"surcharge\":true,\"cyc\":2345,\"credit\":1000,\"iban\":\"2352345234523452345\",\"street\":\"Avenida Espioca, 100\",\"city\":\"Silla\",\"postcode\":\"46013\",\"mobile\":\"654654654\",\"dueDay\":4,\"gestdoc\":23452343,\"province\":1,\"country\":\"1\",\"modify\":\"BasicData\",\"navigate\":true}",
"14": "{\"name\":\"Cliente 1\",\"id\":14,\"street\":\"Aaaaaaaaaa\",\"fi\":\"1234567890A\",\"socialName\":\"Cliente 1\",\"fax\":\"963242100\",\"dischargeDate\":\"01/01/2017\",\"telefono\":\"963242100\",\"salesPerson\":\"2\",\"email\":\"informatica@verdnatura.es\",\"city\":\"asdf\",\"postcode\":\"asdf\",\"phone\":\"asdf\",\"mobile\":\"asdf\",\"credit\":2345,\"cyc\":123,\"iban\":\"asdf\",\"dueDay\":345,\"gestdoc\":2435,\"surcharge\":true,\"navigate\":true}"
},
"PaymentMethod": {
"1": "{\"name\":\"Tarjeta\",\"id\":1}",