Merge pull request #11 from Pear-Trading/TBSliver/Login-Fix

Fix errors found on logout then login again
This commit is contained in:
Tom Bloor 2017-09-08 15:22:57 +01:00 committed by GitHub
commit d3eee9901c
8 changed files with 90 additions and 185 deletions

View file

@ -7,7 +7,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'login.component.html', templateUrl: 'login.component.html',
providers: [ApiService]
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
signin: FormGroup; signin: FormGroup;
@ -38,10 +37,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 '/' // get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
} }
@ -54,17 +49,15 @@ export class LoginComponent implements OnInit {
.subscribe( .subscribe(
result => { result => {
console.log('logged in!'); console.log('logged in!');
this.loginStatus = "success"; this.loginStatus = 'success';
console.log(this.loginStatus); console.log(this.loginStatus);
this.router.navigate([this.returnUrl]); this.router.navigate([this.returnUrl]);
}, },
error => { error => {
console.log( error._body ); console.log( error._body );
this.loginStatus = "send_failed"; this.loginStatus = 'send_failed';
console.log(this.loginStatus); console.log(this.loginStatus);
} }
); );
} }
} }

View file

@ -8,7 +8,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'register.component.html', templateUrl: 'register.component.html',
providers: [ApiService]
}) })
export class RegisterComponent { export class RegisterComponent {

View file

@ -6,7 +6,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'account-edit.component.html', templateUrl: 'account-edit.component.html',
providers: [ApiService]
}) })
export class AccountEditComponent implements OnInit { export class AccountEditComponent implements OnInit {
settingForm: FormGroup; settingForm: FormGroup;

View file

@ -8,7 +8,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'add-data.component.html', templateUrl: 'add-data.component.html',
providers: [ApiService]
}) })
export class AddDataComponent { export class AddDataComponent {
payrollForm: FormGroup; payrollForm: FormGroup;

View file

@ -6,7 +6,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'feedback.component.html', templateUrl: 'feedback.component.html',
providers: [ApiService]
}) })
export class FeedbackComponent { export class FeedbackComponent {
feedbackForm: FormGroup; feedbackForm: FormGroup;

View file

@ -11,7 +11,6 @@ import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'transaction-log.component.html', templateUrl: 'transaction-log.component.html',
providers: [ApiService]
}) })
export class TransactionLogComponent { export class TransactionLogComponent {

View file

@ -5,7 +5,6 @@ import { Router } from '@angular/router';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
templateUrl: './full-layout.component.html', templateUrl: './full-layout.component.html',
providers: [ApiService]
}) })
export class FullLayoutComponent implements OnInit { export class FullLayoutComponent implements OnInit {
displayName: any; displayName: any;

View file

@ -13,15 +13,13 @@ export class ApiService {
constructor( constructor(
private http: Http, private http: Http,
) { ) {
if (localStorage.getItem('sessionKey') ) { if (localStorage.getItem('sessionKey') ) {
this.sessionKey = localStorage.getItem('sessionKey'); this.sessionKey = localStorage.getItem('sessionKey');
} }
} }
public post(url: string, data: any = {}) { public post(url: string, data: any = {}) {
if ( this.sessionKey != null ) { data.session_key = this.sessionKey;
data.session_key = this.sessionKey;
}
return this.http.post( return this.http.post(
this.apiUrl + url, this.apiUrl + url,
data data
@ -31,20 +29,20 @@ export class ApiService {
// Login API // Login API
public getSessionKey() { public getSessionKey() {
console.log('get key'); console.log('get key');
return this.sessionKey; return this.sessionKey;
} }
public setSessionKey(key) { public setSessionKey(key) {
console.log('set key'); console.log('set key');
this.sessionKey = key; this.sessionKey = key;
localStorage.setItem('sessionKey', this.sessionKey); localStorage.setItem('sessionKey', this.sessionKey);
} }
public removeSessionKey() { public removeSessionKey() {
console.log('remove key'); console.log('remove key');
this.sessionKey = null; this.sessionKey = null;
localStorage.removeItem('sessionKey'); localStorage.removeItem('sessionKey');
} }
public register(data) { public register(data) {
@ -55,33 +53,41 @@ export class ApiService {
} }
public login(data) { public login(data) {
let login_event = this.http.post( return this.http
this.apiUrl + '/login', .post(
data this.apiUrl + '/login',
).map( response => response.json() ); data
login_event.subscribe( )
result => { .map(
this.setSessionKey(result.session_key); result => {
this.setUserInfo( const json = result.json();
result.email, this.setSessionKey(json.session_key);
result.display_name || result.name this.setUserInfo(
json.email,
json.display_name || json.name
); );
this.setUserType(result.user_type) this.setUserType(json.user_type);
} return json;
); }
return login_event; );
} }
public logout() { public logout() {
console.log(this.sessionKey); console.log(this.sessionKey);
let key = this.sessionKey; const key = this.sessionKey;
return this.http.post( return this.http
this.apiUrl + '/logout', .post(
{ this.apiUrl + '/logout',
session_key : key, { session_key : key },
} )
).map( response => { this.removeSessionKey(); return response.json() } ); .map(
} response => {
localStorage.clear();
this.sessionKey = null;
return response.json();
}
);
}
// Submits feedback // Submits feedback
@ -100,24 +106,24 @@ export class ApiService {
// gets transaction list for log // gets transaction list for log
public transList(data) { public transList(data) {
let key = this.sessionKey; const key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/outgoing-transactions', this.apiUrl + '/outgoing-transactions',
{ {
session_key : key, session_key : key,
page : data page : data
} }
).map( response => response.json() ); ).map( response => response.json() );
} }
// Searches organisations used for transaction submission // Searches organisations used for transaction submission
public search(data) { public search(data) {
data.session_key = this.sessionKey; data.session_key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/search', this.apiUrl + '/search',
data data
).map( response => response.json() ); ).map( response => response.json() );
} }
// Uploads a transaction // Uploads a transaction
@ -127,7 +133,7 @@ export class ApiService {
return this.http.post( return this.http.post(
this.apiUrl + '/upload', this.apiUrl + '/upload',
data data
).map( response => response.json() ) ).map( response => response.json() );
} }
// handles Org data added // handles Org data added
@ -137,7 +143,7 @@ export class ApiService {
return this.http.post( return this.http.post(
this.apiUrl + '/org/payroll', this.apiUrl + '/org/payroll',
data data
).map( response => response.json() ) ).map( response => response.json() );
} }
public orgSupplier(data) { public orgSupplier(data) {
@ -145,7 +151,7 @@ export class ApiService {
return this.http.post( return this.http.post(
this.apiUrl + '/org/supplier', this.apiUrl + '/org/supplier',
data data
).map( response => response.json() ) ).map( response => response.json() );
} }
public orgEmployee(data) { public orgEmployee(data) {
@ -153,7 +159,7 @@ export class ApiService {
return this.http.post( return this.http.post(
this.apiUrl + '/org/employee', this.apiUrl + '/org/employee',
data data
).map( response => response.json() ) ).map( response => response.json() );
} }
// Handles user data interaction // Handles user data interaction
@ -169,178 +175,90 @@ export class ApiService {
public setUserInfo( public setUserInfo(
email: string, email: string,
display_name: string) { display_name: string) {
console.log("set UserInfo"); console.log('set UserInfo');
localStorage.setItem('email',email); localStorage.setItem('email', email);
localStorage.setItem('displayname',display_name); localStorage.setItem('displayname', display_name);
} }
// Sets usertype // Sets usertype
public setUserType(user_type: string) { public setUserType(user_type: string) {
console.log("set UserType"); console.log('set UserType');
localStorage.setItem('usertype',user_type); localStorage.setItem('usertype', user_type);
} }
// Used for getting account details and updating // Used for getting account details and updating
public accountFullLoad() { public accountFullLoad() {
let key = this.sessionKey; const key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/user', this.apiUrl + '/user',
{ session_key : key }, { session_key : key },
).map( response => response.json() ); ).map( response => response.json() );
} }
public accountEditUpdate(data) { public accountEditUpdate(data) {
data.session_key = this.sessionKey; data.session_key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/user/account', this.apiUrl + '/user/account',
data data
).map( response => response.json() ); ).map( response => response.json() );
} }
// Deletes account details on logout // Deletes account details on logout
public removeUserInfo() { public removeUserInfo() {
console.log("remove UserInfo"); console.log('remove UserInfo');
localStorage.removeItem('email'); localStorage.removeItem('email');
localStorage.removeItem('displayname'); localStorage.removeItem('displayname');
} }
public getFullName() { public getFullName() {
console.log("get Full Name"); console.log('get Full Name');
localStorage.getItem('fullname'); localStorage.getItem('fullname');
} }
public getDisplayName() { public getDisplayName() {
console.log("get Display Name"); console.log('get Display Name');
localStorage.getItem('displayname'); localStorage.getItem('displayname');
} }
public getPostcode() { public getPostcode() {
console.log("get Postcode"); console.log('get Postcode');
localStorage.getItem('postcode'); localStorage.getItem('postcode');
} }
public getYearOfBirth() { public getYearOfBirth() {
console.log("get Year of Birth"); console.log('get Year of Birth');
localStorage.getItem('yearofbirth'); localStorage.getItem('yearofbirth');
} }
public getEmail() { public getEmail() {
console.log("get email"); console.log('get email');
localStorage.getItem('email'); localStorage.getItem('email');
} }
// Leaderboard Api // Leaderboard Api
public leaderboard_fetch(data) { public leaderboard_fetch(data) {
let key = this.sessionKey; const key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/stats/leaderboard', this.apiUrl + '/stats/leaderboard',
{ {
session_key : key, session_key : key,
type : data type : data
} }
).map( response => response.json() ); ).map( response => response.json() );
} }
// Basic Customer User stats API // Basic Customer User stats API
public basicStats() { public basicStats() {
let key = this.sessionKey; const key = this.sessionKey;
return this.http.post( return this.http.post(
this.apiUrl + '/stats', this.apiUrl + '/stats',
{ {
session_key : key, session_key : key,
} }
).map( response => response.json() ); ).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,
},
},
}
)
} }
} }