Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(users): deprecate all existing API, use rxjs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir authored and joshuawilson committed Mar 10, 2017
1 parent 10a62e6 commit 5087e6c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 45 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"karma-phantomjs-launcher": "1.0.2",
"karma-sourcemap-loader": "0.3.7",
"karma-webpack": "2.0.2",
"lodash": "4.17.4",
"mocha": "3.2.0",
"moment": "2.17.1",
"ng-router-loader": "2.1.0",
Expand Down
127 changes: 82 additions & 45 deletions src/app/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,144 @@
import { Injectable, Inject } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Observable } from 'rxjs';

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/catch';
import { cloneDeep } from 'lodash';

import { Broadcaster } from '../shared/broadcaster.service';
import { AuthenticationService } from '../auth/authentication.service';
import { Logger } from '../shared/logger.service';
import { AUTH_API_URL } from '../shared/auth-api';
import { User } from './user';
import { Observable } from 'rxjs';

/**
* Provides user and user list methods to retrieve current or user list details
*
* The UserService should be injected at the root of the application to ensure it is a singleton
* getUser and getAllUsers return observables that can be subscribed to for information
*/

@Injectable()
export class UserService {

userData: User = {} as User;
allUserData: User[] = [];
/**
* @deprecated since v0.4.4. Use {@link #loggedInUser} instead.
*/
private userData: User = {} as User;

/**
* @deprecated since v0.4.4. No replacement method is provided.
*/
private allUserData: User[] = [];

private headers = new Headers({'Content-Type': 'application/json'});
private headers = new Headers({ 'Content-Type': 'application/json' });
private userUrl: string; // URL to web api
private usersUrl: string; // URL to web api


constructor(private http: Http,
private logger: Logger,
private auth: AuthenticationService,
private broadcaster: Broadcaster,
@Inject(AUTH_API_URL) apiUrl: string
private logger: Logger,
private auth: AuthenticationService,
private broadcaster: Broadcaster,
@Inject(AUTH_API_URL) apiUrl: string
) {
this.userUrl = apiUrl + 'user';
this.usersUrl = apiUrl + 'users';
this.broadcaster.on<string>('logout')
.subscribe(message => {
this.resetUser();
});
// TODO Remove these when we remove deprecated methods
this.loggedInUser.subscribe(val => this.userData = val);
this.getAllUsers().subscribe(val => this.allUserData = val);
}

/**
* The currently logged in user
*/
get loggedInUser(): Observable<User> {
return this.http
.get(this.userUrl, { headers: this.headers })
.map(response => {
let userData = cloneDeep(response.json().data as User);
userData.attributes.primaryEmail = userData.attributes.email;
return userData;
});
}

/**
* Get the User object for a given user id, or null if no user is found
* @param userId the userId to search for
*/
getUserByUserId(userId: string): Observable<User> {
return this.getAllUsers().map(val => {
for (let u of val) {
if (userId === u.id) {
return u;
}
}
return null;
});
}

/**
* Get the User object for a given username, or null if no user is found
* @param username the username to search for
*/
getUserByUsername(username: string): Observable<User> {
return this.getAllUsers().map(val => {
for (let u of val) {
if (username === u.attributes.username) {
return u;
}
}
return null;
});
}


/**
* @deprecated since v0.4.4. Use {@link #loggedInUser} instead.
*/
getSavedLoggedInUser(): User {
return this.userData;
}

/**
* @deprecated since v0.4.4. No replacement is provided.
*/
getLocallySavedUsers(): User[] {
return this.allUserData;
}

/**
* @deprecated since v0.4.4. Use {@link #loggedInUser} instead.
* Get currently logged in user
*
* @returns Observable<User>
*/
getUser(): Observable<User> {
// Check if we have the user data if not then check if the user is logged in.
// We need the auth key to get the user data. So either we already the data or we don't have the keys
// in either case don't try to get the data.
if (Object.keys(this.userData).length || !this.auth.isLoggedIn()) {
return Observable.of(this.userData);
} else {
return this.http
.get(this.userUrl, {headers: this.headers})
.map(response => {
let userData = response.json().data as User;
// The reference of this.userData is
// being used in Header
// So updating the value like that
this.userData.attributes = {
fullName: userData.attributes.fullName,
imageURL: userData.attributes.imageURL,
username: userData.attributes.username,
bio: userData.attributes.bio,
primaryEmail: userData.attributes.email
};
this.userData.id = userData.id;
// this.profile.initDefaults(this.userData);
return this.userData;
});
}
return this.loggedInUser;
}

/**
* @deprecated since v0.4.4. No replacement is provided.
* Get all users
*
* @returns Observable<User[]>
*/
getAllUsers(): Observable<User[]> {
if (this.allUserData.length) {
return Observable.of(this.allUserData);
} else {
return this.http
.get(this.usersUrl, {headers: this.headers})
.map(response => {
this.allUserData = response.json().data as User[];
return this.allUserData;
});
}
return this.http
.get(this.usersUrl, { headers: this.headers })
.map(response => {
this.allUserData = response.json().data as User[];
return this.allUserData;
});
}

/**
* @deprecated since v0.4.4. No replacement is provided.
*
*/
resetUser(): void {
this.userData = {} as User;
}
Expand Down

0 comments on commit 5087e6c

Please sign in to comment.