|
|
@ -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;
|
|
|
|