Merge branch 'max_dev' of Software_Engineering_I/greenvironment-frontend into master

master
Max_ES 5 years ago committed by Gitea
commit b176a420e7

@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { Post } from 'src/app/models/post'; import { Post } from 'src/app/models/post';
import { FeedService } from 'src/app/services/feed/feed.service'; import { FeedService } from 'src/app/services/feed/feed.service';
import { Actionlist } from 'src/app/models/actionlist'; import { Actionlist } from 'src/app/models/actionlist';
import { DatasharingService } from '../../services/datasharing.service';
import { User } from 'src/app/models/user';
@Component({ @Component({
selector: 'home-feed', selector: 'home-feed',
@ -22,10 +24,19 @@ export class FeedComponent implements OnInit {
actionlist: Actionlist = new Actionlist(); actionlist: Actionlist = new Actionlist();
constructor(private feedService: FeedService) { } loggedIn = false;
userId: number;
user: User;
constructor(private feedService: FeedService,private data: DatasharingService) { }
ngOnInit() { ngOnInit() {
this.feedService.getAllPostsRaw().subscribe(response => { this.data.currentUserInfo.subscribe(user => {
this.user = user;
this.loggedIn = user.loggedIn;
if(this.loggedIn) this.userId = user.userID;
});
this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => {
this.feedNew = this.feedService.renderAllPosts(response.json()); this.feedNew = this.feedService.renderAllPosts(response.json());
this.parentSelectedPostList = this.feedNew; this.parentSelectedPostList = this.feedNew;
this.feedMostLiked = this.feedNew; this.feedMostLiked = this.feedNew;
@ -44,7 +55,7 @@ export class FeedComponent implements OnInit {
showNew() { showNew() {
console.log('showNew()'); console.log('showNew()');
this.feedService.getAllPostsRaw().subscribe(response => { this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => {
this.feedNew = this.feedService.renderAllPosts(response.json()); this.feedNew = this.feedService.renderAllPosts(response.json());
this.parentSelectedPostList = this.feedNew; }); this.parentSelectedPostList = this.feedNew; });
this.viewNew = true; this.viewNew = true;
@ -53,7 +64,7 @@ export class FeedComponent implements OnInit {
showMostLiked() { showMostLiked() {
console.log('showMostLiked()'); console.log('showMostLiked()');
this.feedService.getAllPostsRaw().subscribe(response => { this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => {
this.feedMostLiked = this.feedService.renderAllPosts(response.json()); this.feedMostLiked = this.feedService.renderAllPosts(response.json());
this.parentSelectedPostList = this.feedMostLiked; }); this.parentSelectedPostList = this.feedMostLiked; });
this.viewNew = false; this.viewNew = false;
@ -62,7 +73,7 @@ export class FeedComponent implements OnInit {
refresh($event) { refresh($event) {
this.feedService.getAllPostsRaw().subscribe(response => { this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => {
this.parentSelectedPostList = this.feedService.renderAllPosts(response.json()); this.parentSelectedPostList = this.feedService.renderAllPosts(response.json());
console.log('Refresh'); console.log('Refresh');
}); });

@ -1,15 +1,15 @@
export class User { export class User {
constructor( loggedIn : boolean
public userID: number, userID : number
public loggedIn: boolean, username : string
public username: string, handle : string
public handle: string, email : string
public email: string, points : number
public points: number, level : number
public level: number,
public friendIDs: number[], friendIDs : number[]
public groupIDs: number[], groupIDs : number[]
public chatIDs: number[], chatIDs : number[]
public requestIDs: number[],
) {} requestIDs : number[]
} }

@ -85,6 +85,19 @@ export class FeedService {
}); });
return this.posts; return this.posts;
} }
public getAllPostsById(userId: number): Array<Post> {
const url = environment.graphQLUrl;
const headers = new Headers();
headers.set('Content-Type', 'application/json');
this.http.post(url, this.getBodyForGetAllPostsByUserId(userId))
.subscribe(response => {
this.posts = this.renderAllPosts(response.json());
console.log(response);
});
return this.posts;
}
public getAllPostsRaw(): any { public getAllPostsRaw(): any {
const url = environment.graphQLUrl; const url = environment.graphQLUrl;
@ -95,13 +108,29 @@ export class FeedService {
return this.http.post(url, this.getBodyForGetAllPosts()); return this.http.post(url, this.getBodyForGetAllPosts());
} }
public getAllPostsRawByUserId(userId: number): any {
const url = environment.graphQLUrl;
const headers = new Headers();
headers.set('Content-Type', 'application/json');
return this.http.post(url, this.getBodyForGetAllPostsByUserId(userId));
}
getBodyForGetAllPostsByUserId(pUserId: number) {
const body = {query: `query ($userId: ID) {
getPosts (first: 1000, offset: 0) {id, content, htmlContent, upvotes, downvotes, userVote, author{name, handle, id}, createdAt}
}`, variables: {
userId: pUserId
}};
return body
}
getBodyForGetAllPosts() { getBodyForGetAllPosts() {
const body = {query: `query { const body = {query: `query {
getPosts (first: 1000, offset: 0) {id, content, htmlContent, upvotes, downvotes, userVote, author{name, handle, id}, createdAt} getPosts (first: 1000, offset: 0) {id, content, htmlContent, upvotes, downvotes, userVote, author{name, handle, id}, createdAt}
}` }`
}; }
return body
return body;
} }
public renderAllPosts(pResponse: any): Array<Post> { public renderAllPosts(pResponse: any): Array<Post> {

@ -34,22 +34,21 @@ export class LoginService {
} }
public updateUserInfo(response : any){ public updateUserInfo(response : any){
const loginData = response.data.login; const user: User = new User();
const user: User = new User( user.loggedIn = true;
loginData.login.id, user.userID = response.data.login.id;
true, user.username = response.data.login.name;
loginData.name, user.handle = response.data.login.handle;
loginData.handle, user.email = response.data.login.email;
loginData.email, user.points = response.data.login.points;
loginData.points, user.level = response.data.login.level;
loginData.level, user.friendIDs = response.data.login.friends;
loginData.friends, user.groupIDs = response.data.login.groups;
loginData.groups, user.chatIDs = response.data.login.chats;
loginData.chats, user.requestIDs = response.data.login.requests;
loginData.requests,
); this.data.changeUserInfo(user)
this.data.changeUserInfo(user);
} }
public buildJson(login: Login): any { public buildJson(login: Login): any {

Loading…
Cancel
Save