From b1c2f531bb507478c6e92e7c7b0aad775c1a2eb3 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 8 Sep 2017 14:49:57 +0100 Subject: [PATCH 1/3] Fixed linter errors and removed inneeded api call --- src/app/auth/login.component.ts | 10 +- src/app/providers/api-service.ts | 220 ++++++++++--------------------- 2 files changed, 68 insertions(+), 162 deletions(-) diff --git a/src/app/auth/login.component.ts b/src/app/auth/login.component.ts index 8a28be0..46848a4 100644 --- a/src/app/auth/login.component.ts +++ b/src/app/auth/login.component.ts @@ -38,10 +38,6 @@ export class LoginComponent implements OnInit { } ); - this.api.graph_data(undefined).subscribe( - result => { console.log(result) } - ) - // get return url from route parameters or default to '/' this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; } @@ -54,17 +50,15 @@ export class LoginComponent implements OnInit { .subscribe( result => { console.log('logged in!'); - this.loginStatus = "success"; + this.loginStatus = 'success'; console.log(this.loginStatus); this.router.navigate([this.returnUrl]); }, error => { console.log( error._body ); - this.loginStatus = "send_failed"; + this.loginStatus = 'send_failed'; console.log(this.loginStatus); } ); } - - } diff --git a/src/app/providers/api-service.ts b/src/app/providers/api-service.ts index 35b2369..c892d1c 100644 --- a/src/app/providers/api-service.ts +++ b/src/app/providers/api-service.ts @@ -13,9 +13,9 @@ export class ApiService { constructor( private http: Http, ) { - if (localStorage.getItem('sessionKey') ) { - this.sessionKey = localStorage.getItem('sessionKey'); - } + if (localStorage.getItem('sessionKey') ) { + this.sessionKey = localStorage.getItem('sessionKey'); + } } public post(url: string, data: any = {}) { @@ -31,20 +31,20 @@ export class ApiService { // Login API public getSessionKey() { - console.log('get key'); - return this.sessionKey; + console.log('get key'); + return this.sessionKey; } public setSessionKey(key) { - console.log('set key'); - this.sessionKey = key; - localStorage.setItem('sessionKey', this.sessionKey); + console.log('set key'); + this.sessionKey = key; + localStorage.setItem('sessionKey', this.sessionKey); } public removeSessionKey() { - console.log('remove key'); - this.sessionKey = null; - localStorage.removeItem('sessionKey'); + console.log('remove key'); + this.sessionKey = null; + localStorage.removeItem('sessionKey'); } public register(data) { @@ -55,7 +55,7 @@ export class ApiService { } public login(data) { - let login_event = this.http.post( + const login_event = this.http.post( this.apiUrl + '/login', data ).map( response => response.json() ); @@ -66,22 +66,22 @@ export class ApiService { result.email, result.display_name || result.name ); - this.setUserType(result.user_type) + this.setUserType(result.user_type); } ); return login_event; } - public logout() { - console.log(this.sessionKey); - let key = this.sessionKey; - return this.http.post( - this.apiUrl + '/logout', - { - session_key : key, - } - ).map( response => { this.removeSessionKey(); return response.json() } ); - } + public logout() { + console.log(this.sessionKey); + const key = this.sessionKey; + return this.http.post( + this.apiUrl + '/logout', + { + session_key : key, + } + ).map( response => { this.removeSessionKey(); return response.json(); } ); + } // Submits feedback @@ -100,24 +100,24 @@ export class ApiService { // gets transaction list for log public transList(data) { - let key = this.sessionKey; - return this.http.post( - this.apiUrl + '/outgoing-transactions', - { + const key = this.sessionKey; + return this.http.post( + this.apiUrl + '/outgoing-transactions', + { session_key : key, page : data } - ).map( response => response.json() ); + ).map( response => response.json() ); } // Searches organisations used for transaction submission public search(data) { - data.session_key = this.sessionKey; - return this.http.post( - this.apiUrl + '/search', - data - ).map( response => response.json() ); + data.session_key = this.sessionKey; + return this.http.post( + this.apiUrl + '/search', + data + ).map( response => response.json() ); } // Uploads a transaction @@ -127,7 +127,7 @@ export class ApiService { return this.http.post( this.apiUrl + '/upload', data - ).map( response => response.json() ) + ).map( response => response.json() ); } // handles Org data added @@ -137,7 +137,7 @@ export class ApiService { return this.http.post( this.apiUrl + '/org/payroll', data - ).map( response => response.json() ) + ).map( response => response.json() ); } public orgSupplier(data) { @@ -145,7 +145,7 @@ export class ApiService { return this.http.post( this.apiUrl + '/org/supplier', data - ).map( response => response.json() ) + ).map( response => response.json() ); } public orgEmployee(data) { @@ -153,7 +153,7 @@ export class ApiService { return this.http.post( this.apiUrl + '/org/employee', data - ).map( response => response.json() ) + ).map( response => response.json() ); } // Handles user data interaction @@ -169,178 +169,90 @@ export class ApiService { public setUserInfo( email: string, display_name: string) { - console.log("set UserInfo"); - localStorage.setItem('email',email); - localStorage.setItem('displayname',display_name); + console.log('set UserInfo'); + localStorage.setItem('email', email); + localStorage.setItem('displayname', display_name); } // Sets usertype public setUserType(user_type: string) { - console.log("set UserType"); - localStorage.setItem('usertype',user_type); + console.log('set UserType'); + localStorage.setItem('usertype', user_type); } // Used for getting account details and updating public accountFullLoad() { - let key = this.sessionKey; - return this.http.post( + const key = this.sessionKey; + return this.http.post( this.apiUrl + '/user', { session_key : key }, - ).map( response => response.json() ); + ).map( response => response.json() ); } public accountEditUpdate(data) { - data.session_key = this.sessionKey; - return this.http.post( + data.session_key = this.sessionKey; + return this.http.post( this.apiUrl + '/user/account', data - ).map( response => response.json() ); + ).map( response => response.json() ); } // Deletes account details on logout public removeUserInfo() { - console.log("remove UserInfo"); + console.log('remove UserInfo'); localStorage.removeItem('email'); localStorage.removeItem('displayname'); } public getFullName() { - console.log("get Full Name"); + console.log('get Full Name'); localStorage.getItem('fullname'); } public getDisplayName() { - console.log("get Display Name"); + console.log('get Display Name'); localStorage.getItem('displayname'); } public getPostcode() { - console.log("get Postcode"); + console.log('get Postcode'); localStorage.getItem('postcode'); } public getYearOfBirth() { - console.log("get Year of Birth"); + console.log('get Year of Birth'); localStorage.getItem('yearofbirth'); } public getEmail() { - console.log("get email"); + console.log('get email'); localStorage.getItem('email'); } // Leaderboard Api public leaderboard_fetch(data) { - let key = this.sessionKey; + const key = this.sessionKey; return this.http.post( - this.apiUrl + '/stats/leaderboard', - { - session_key : key, + this.apiUrl + '/stats/leaderboard', + { + session_key : key, type : data - } - ).map( response => response.json() ); - } + } + ).map( response => response.json() ); + } // Basic Customer User stats API public basicStats() { - let key = this.sessionKey; + const key = this.sessionKey; return this.http.post( - this.apiUrl + '/stats', - { + this.apiUrl + '/stats', + { session_key : key, - } - ).map( response => response.json() ); - } - - // Fake chart data to mimic - - public graph_data(data) { - return Observable.of( - { - // graphstoshow is on server and changes every hour, listing what snippets & graphs to display - "elementstoshow" : - { - "graphs" : - { - customersthisweek : true, - customerslastweek : true, - customerslastmonth : true, - customerslastyear : true, - returningcustomerslastweek : true, - returningcustomerslastmonth : true, - returningcustomerslastyear : true, - noofcustomerssector : true, - percentofcustomerssector : true, - pointsthisweek : true, - percentlocalsuppliersvscompetitor : true, - }, - "snippets" : - { - customersthismonth: true, - moneyspentthismonth: true, - pointstotal: true, - averagetransactiontoday: true, - percentownlocalsupplier : false, - percentsinglecompetitorlocalsupplier : false, - }, - }, - "data" : - { - "customersthisweek" : - { - day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'], - customerno : [1,2,3,4,5,6,7], - returningcustomerno : [1,1,2,3,4,5,4], - }, - "customerslastweek" : - { - day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'], - customerno : [7,6,5,4,3,2,1], - returningcustomerno : [3,4,5,4,3,2,1], - }, - // can take differing size arrays, so any month works. Example here is for April - "customerslastmonth" : - { - day : ['April 1','April 2','April 3','April 4','April 5','April 6','April 7','April 8', - 'April 9','April 10','April 11','April 12','April 13','April 14','April 15','April 16', - 'April 17','April 18','April 19','April 20','April 21','April 22','April 23','April 24', - 'April 25','April 26','April 27','April 28','April 29','April 30'], - customerno : [7,6,5,4,3,2,1,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30], - returningcustomerno : [4,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24], - }, - "customerslastyear" : - { - month : ['January','February','March','April','May','June','July','August','September','October','November','December'], - customerno : [7,6,5,4,3,2,1,8,9,10,11,12], - returningcustomerno : [3,2,4,2,1,1,1,6,4,8,5,12], - }, - // If the number is potential or actual customers in their sector has yet to be determined - "customersinsector" : - { - percent : 76, - customerno : 34000, - }, - "pointsthisweek" : - { - day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'], - points : [1,2,3,4,5,6,7], - }, - "pointslastweek" : - { - day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'], - points : [1,2,3,4,5,6,7], - }, - "localsuppliers" : - { - percentownlocal : 50, - percentsinglecompetitorlocal : 65, - }, - }, - } - ) + } + ).map( response => response.json() ); } } From 7d637e84e458ad02a1979465886fcc2e6829da8b Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 8 Sep 2017 14:59:31 +0100 Subject: [PATCH 2/3] Fix race condition in login --- src/app/providers/api-service.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/app/providers/api-service.ts b/src/app/providers/api-service.ts index c892d1c..da05708 100644 --- a/src/app/providers/api-service.ts +++ b/src/app/providers/api-service.ts @@ -55,21 +55,23 @@ export class ApiService { } public login(data) { - const login_event = this.http.post( - this.apiUrl + '/login', - data - ).map( response => response.json() ); - login_event.subscribe( - result => { - this.setSessionKey(result.session_key); - this.setUserInfo( - result.email, - result.display_name || result.name + return this.http + .post( + this.apiUrl + '/login', + data + ) + .map( + result => { + const json = result.json(); + this.setSessionKey(json.session_key); + this.setUserInfo( + json.email, + json.display_name || json.name ); - this.setUserType(result.user_type); - } - ); - return login_event; + this.setUserType(json.user_type); + return json; + } + ); } public logout() { From cf1980d768a1b48459f3076381f717459bc99d31 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 8 Sep 2017 15:22:15 +0100 Subject: [PATCH 3/3] Stop using multiple apiService instances --- src/app/auth/login.component.ts | 1 - src/app/auth/register.component.ts | 3 +-- src/app/dashboard/account-edit.component.ts | 1 - src/app/dashboard/add-data.component.ts | 1 - src/app/dashboard/feedback.component.ts | 1 - .../dashboard/transaction-log.component.ts | 1 - src/app/layouts/full-layout.component.ts | 1 - src/app/providers/api-service.ts | 22 +++++++++++-------- 8 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/app/auth/login.component.ts b/src/app/auth/login.component.ts index 46848a4..77b2751 100644 --- a/src/app/auth/login.component.ts +++ b/src/app/auth/login.component.ts @@ -7,7 +7,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'login.component.html', - providers: [ApiService] }) export class LoginComponent implements OnInit { signin: FormGroup; diff --git a/src/app/auth/register.component.ts b/src/app/auth/register.component.ts index 4cc5eb4..ebc69d2 100644 --- a/src/app/auth/register.component.ts +++ b/src/app/auth/register.component.ts @@ -8,7 +8,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'register.component.html', - providers: [ApiService] }) export class RegisterComponent { @@ -134,4 +133,4 @@ export class RegisterComponent { } -} \ No newline at end of file +} diff --git a/src/app/dashboard/account-edit.component.ts b/src/app/dashboard/account-edit.component.ts index 707b93c..eedd3ad 100644 --- a/src/app/dashboard/account-edit.component.ts +++ b/src/app/dashboard/account-edit.component.ts @@ -6,7 +6,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'account-edit.component.html', - providers: [ApiService] }) export class AccountEditComponent implements OnInit { settingForm: FormGroup; diff --git a/src/app/dashboard/add-data.component.ts b/src/app/dashboard/add-data.component.ts index 3691210..de07cfa 100644 --- a/src/app/dashboard/add-data.component.ts +++ b/src/app/dashboard/add-data.component.ts @@ -8,7 +8,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'add-data.component.html', - providers: [ApiService] }) export class AddDataComponent { payrollForm: FormGroup; diff --git a/src/app/dashboard/feedback.component.ts b/src/app/dashboard/feedback.component.ts index f1f1f93..11d56d4 100644 --- a/src/app/dashboard/feedback.component.ts +++ b/src/app/dashboard/feedback.component.ts @@ -6,7 +6,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'feedback.component.html', - providers: [ApiService] }) export class FeedbackComponent { feedbackForm: FormGroup; diff --git a/src/app/dashboard/transaction-log.component.ts b/src/app/dashboard/transaction-log.component.ts index cf6f982..f707e83 100644 --- a/src/app/dashboard/transaction-log.component.ts +++ b/src/app/dashboard/transaction-log.component.ts @@ -11,7 +11,6 @@ import 'rxjs/add/operator/map'; @Component({ templateUrl: 'transaction-log.component.html', - providers: [ApiService] }) export class TransactionLogComponent { diff --git a/src/app/layouts/full-layout.component.ts b/src/app/layouts/full-layout.component.ts index 375151a..7007e35 100644 --- a/src/app/layouts/full-layout.component.ts +++ b/src/app/layouts/full-layout.component.ts @@ -5,7 +5,6 @@ import { Router } from '@angular/router'; @Component({ selector: 'app-dashboard', templateUrl: './full-layout.component.html', - providers: [ApiService] }) export class FullLayoutComponent implements OnInit { displayName: any; diff --git a/src/app/providers/api-service.ts b/src/app/providers/api-service.ts index da05708..e0f27a2 100644 --- a/src/app/providers/api-service.ts +++ b/src/app/providers/api-service.ts @@ -19,9 +19,7 @@ export class ApiService { } public post(url: string, data: any = {}) { - if ( this.sessionKey != null ) { - data.session_key = this.sessionKey; - } + data.session_key = this.sessionKey; return this.http.post( this.apiUrl + url, data @@ -77,12 +75,18 @@ export class ApiService { public logout() { console.log(this.sessionKey); const key = this.sessionKey; - return this.http.post( - this.apiUrl + '/logout', - { - session_key : key, - } - ).map( response => { this.removeSessionKey(); return response.json(); } ); + return this.http + .post( + this.apiUrl + '/logout', + { session_key : key }, + ) + .map( + response => { + localStorage.clear(); + this.sessionKey = null; + return response.json(); + } + ); } // Submits feedback