Refactor profile and self service

master
trivernis 5 years ago
parent 0be475e79a
commit f9ef6c2669

@ -15,7 +15,7 @@ export class AppComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUserInfo.subscribe(user => {
if (user.loggedIn !== true) { if (user.loggedIn !== true) {
this.selfservice.checkIfLoggedIn(); this.selfservice.checkIfLoggedIn().subscribe();
} }
}); });
} }

@ -58,9 +58,7 @@ export class ProfileComponent implements OnInit {
this.userProfile = response; this.userProfile = response;
// tslint:disable-next-line:max-line-length // tslint:disable-next-line:max-line-length
this.userProfile.allowedToSendRequest = this.requestService.isAllowedToSendRequest(this.userProfile.userID, this.self); this.userProfile.allowedToSendRequest = this.requestService.isAllowedToSendRequest(this.userProfile.userID, this.self);
if (this.userProfile.userID === this.self.userID) { this.ownProfile = this.userProfile.userID === this.self.userID;
this.ownProfile = true;
} else {this.ownProfile = false; }
this.rankname = this.levellist.getLevelName(this.userProfile.level); this.rankname = this.levellist.getLevelName(this.userProfile.level);
} else { this.profileNotFound = true; } } else { this.profileNotFound = true; }
this.loading = false; this.loading = false;

@ -46,20 +46,30 @@ export class User {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
if (userDataResponse.friends) {
this.friends = userDataResponse.friends this.friends = userDataResponse.friends
.map(friend => new FriendInfo( .map(friend => new FriendInfo(
friend.id, friend.name, friend.id, friend.name,
friend.level, friend.level,
this.buildProfilePictureUrl(friend.profilePicture) this.buildProfilePictureUrl(friend.profilePicture)
)); ));
}
if (userDataResponse.groups) {
this.groups = userDataResponse.groups this.groups = userDataResponse.groups
.map(group => new GroupInfo(group.id, group.name)); .map(group => new GroupInfo(group.id, group.name));
}
if (userDataResponse.chats) {
this.chatIDs = userDataResponse.chats.map(chat => chat.id); this.chatIDs = userDataResponse.chats.map(chat => chat.id);
}
if (userDataResponse.sentRequests) {
this.sentRequestUserIDs = userDataResponse.sentRequests this.sentRequestUserIDs = userDataResponse.sentRequests
.map(request => request.receiver.id); .map(request => request.receiver.id);
}
if (userDataResponse.receivedRequests) {
this.receivedRequests = userDataResponse.receivedRequests this.receivedRequests = userDataResponse.receivedRequests
.map(request => new FriendRequest(request.id, request.sender.id, request.sender.handle, request.sender.name)); .map(request => new FriendRequest(request.id, request.sender.id, request.sender.handle, request.sender.name));
} }
}
buildProfilePictureUrl(path: string): string { buildProfilePictureUrl(path: string): string {
if (path) { if (path) {

@ -1,34 +1,14 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import { Http } from '@angular/http'; import {HttpClient} from '@angular/common/http';
import {Post} from 'src/app/models/post'; import {Post} from 'src/app/models/post';
import {Author} from 'src/app/models/author'; import {Author} from 'src/app/models/author';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import {User} from 'src/app/models/user'; import {User} from 'src/app/models/user';
import { Observable, Subject } from 'rxjs'; import {Subject} from 'rxjs';
import {Activity} from 'src/app/models/activity'; import {Activity} from 'src/app/models/activity';
import {BaseService} from '../base.service';
@Injectable({ const graphqlGetProfileQuery = `query($userId: ID) {
providedIn: 'root'
})
export class ProfileService {
public proflile: Subject<any> = new Subject();
constructor(private http: Http) { }
public getUserData(userId: string) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
// return this.renderProfile(this.http.post(environment.graphQLUrl, this.buildGetProfileJson(userId)));
this.http.post(environment.graphQLUrl, this.buildGetProfileJson(userId)).subscribe(result => {
// push onto subject
this.proflile.next(this.renderProfile(result.json()));
return this.proflile;
});
}
public buildGetProfileJson(id: string): any {
const body = {query: `query($userId: ID) {
getUser(userId:$userId){ getUser(userId:$userId){
id id
handle handle
@ -64,29 +44,54 @@ export class ProfileService {
createdAt createdAt
} }
} }
}`, variables: { }`;
@Injectable({
providedIn: 'root'
})
export class ProfileService extends BaseService {
constructor(private http: HttpClient) {
super();
}
public proflile: Subject<any> = new Subject();
/**
* Builds the request body of a getProfile request
* @param id
*/
private static buildGetProfileBody(id: string): any {
return {
query: graphqlGetProfileQuery,
variables: {
userId: id, userId: id,
}}; }
return body; };
} }
public renderProfile(response: any): User { /**
* Returns the data for the specified user.
* @param userId
*/
public getUserData(userId: string) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, ProfileService.buildGetProfileBody(userId)).subscribe(result => {
this.proflile.next(this.getProfileData(result));
return this.proflile;
});
}
/**
* Returns a userinstance filled with profile data
* @param response
*/
public getProfileData(response: any): User {
const posts = new Array<Post>(); const posts = new Array<Post>();
const profile = new User(); const profile = new User();
if (response.data.getUser != null) { if (response.data.getUser) {
profile.assignFromResponse(response.data.getUser);
profile.userID = response.data.getUser.id;
profile.username = response.data.getUser.name;
profile.handle = response.data.getUser.handle;
profile.points = response.data.getUser.points;
profile.level = response.data.getUser.level;
profile.friendCount = response.data.getUser.friendCount;
profile.groupCount = response.data.getUser.groupCount;
if (response.data.getUser.profilePicture) {
profile.profilePicture = environment.greenvironmentUrl + response.data.getUser.profilePicture;
} else {
profile.profilePicture = 'assets/images/default-profilepic.svg';
}
const temp = new Date(Number(response.data.getUser.joinedAt)); const temp = new Date(Number(response.data.getUser.joinedAt));
const date = temp.toLocaleString('en-GB'); const date = temp.toLocaleString('en-GB');
profile.joinedAt = date; profile.joinedAt = date;

@ -1,41 +1,87 @@
import { Injectable, EventEmitter, Output } from '@angular/core'; import {Injectable} from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http';
import {User} from 'src/app/models/user'; import {User} from 'src/app/models/user';
import {DatasharingService} from '../datasharing.service'; import {DatasharingService} from '../datasharing.service';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import {FriendRequest} from 'src/app/models/friendRequest'; import {FriendRequest} from 'src/app/models/friendRequest';
import {FriendInfo} from 'src/app/models/friendinfo'; import {FriendInfo} from 'src/app/models/friendinfo';
import { GroupInfo } from 'src/app/models/groupinfo'; import {HttpClient} from '@angular/common/http';
import {tap} from 'rxjs/operators';
import {BaseService} from '../base.service';
const getSelfGraphqlQuery = `{
getSelf{
id,
name,
email,
handle,
points,
level,
profilePicture,
receivedRequests{id, sender{name, handle, id}},
sentRequests{receiver{id}},
friends {
id,
name,
level,
profilePicture,
},
groups {
id,
name
},
chats{
id
},
settings
}
}`;
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class SelfService { export class SelfService extends BaseService {
constructor(private http: Http, private data: DatasharingService, private router: Router) { } constructor(private http: HttpClient, private data: DatasharingService) {
super();
}
/**
* Builds the getself request body
*/
private static buildGetSelfBody(): any {
return {
query: getSelfGraphqlQuery, variables: {}
};
}
/**
* Checks if the user is still logged in
*/
public checkIfLoggedIn() { public checkIfLoggedIn() {
const url = environment.graphQLUrl; const url = environment.graphQLUrl;
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
return this.http.post(url, this.buildJson()) return this.http.post(url, SelfService.buildGetSelfBody(), {headers: this.headers})
.subscribe(response => { .pipe(tap(response => {
this.stillLoggedIn(); this.stillLoggedIn();
this.updateUserInfo(response.json()); this.updateUserInfo(response);
}, error => { }, error => {
this.notLoggedIn(); this.notLoggedIn();
// this.fakeLogin(); }));
}
);
} }
public stillLoggedIn() { public stillLoggedIn() {
} }
public notLoggedIn() { public notLoggedIn() {
} }
/**
* Updates the info on a user
* @param response
*/
public updateUserInfo(response: any) { public updateUserInfo(response: any) {
const user = new User(); const user = new User();
user.assignFromResponse(response.data.getSelf); user.assignFromResponse(response.data.getSelf);
@ -63,36 +109,4 @@ export class SelfService {
this.data.changeUserInfo(user); this.data.changeUserInfo(user);
} }
public buildJson(): any {
const body = {query: `{
getSelf{
id,
name,
email,
handle,
points,
level,
profilePicture,
receivedRequests{id, sender{name, handle, id}},
sentRequests{receiver{id}},
friends {
id,
name,
level,
profilePicture,
},
groups {
id,
name
},
chats{
id
},
settings
}
}`, variables: {
}};
return body;
}
} }

Loading…
Cancel
Save