From 1f1197348193724bb2d5e01e43873868bea0f5e8 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 18 Dec 2019 23:24:20 +0100 Subject: [PATCH] tried to send userId to get all Posts --- src/app/components/feed/feed.component.ts | 21 ++++++++++---- src/app/services/feed/feed.service.ts | 35 +++++++++++++++++++++-- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/app/components/feed/feed.component.ts b/src/app/components/feed/feed.component.ts index cc1c40e..ca4b875 100644 --- a/src/app/components/feed/feed.component.ts +++ b/src/app/components/feed/feed.component.ts @@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core'; import { Post } from 'src/app/models/post'; import { FeedService } from 'src/app/services/feed/feed.service'; import { Actionlist } from 'src/app/models/actionlist'; +import { DatasharingService } from '../../services/datasharing.service'; +import { User } from 'src/app/models/user'; @Component({ selector: 'home-feed', @@ -22,10 +24,19 @@ export class FeedComponent implements OnInit { actionlist: Actionlist = new Actionlist(); - constructor(private feedService: FeedService) { } + loggedIn = false; + userId: number; + user: User; + + constructor(private feedService: FeedService,private data: DatasharingService) { } ngOnInit() { - this.feedService.getAllPostsRaw().subscribe(response => { + this.data.currentUserInfo.subscribe(user => { + this.user = user; + this.loggedIn = user.loggedIn; + this.userId = user.userID; + }); + this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => { this.feedNew = this.feedService.renderAllPosts(response.json()); this.parentSelectedPostList = this.feedNew; this.feedMostLiked = this.feedNew; @@ -44,7 +55,7 @@ export class FeedComponent implements OnInit { showNew() { console.log('showNew()'); - this.feedService.getAllPostsRaw().subscribe(response => { + this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => { this.feedNew = this.feedService.renderAllPosts(response.json()); this.parentSelectedPostList = this.feedNew; }); this.viewNew = true; @@ -53,7 +64,7 @@ export class FeedComponent implements OnInit { showMostLiked() { console.log('showMostLiked()'); - this.feedService.getAllPostsRaw().subscribe(response => { + this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => { this.feedMostLiked = this.feedService.renderAllPosts(response.json()); this.parentSelectedPostList = this.feedMostLiked; }); this.viewNew = false; @@ -62,7 +73,7 @@ export class FeedComponent implements OnInit { refresh($event) { - this.feedService.getAllPostsRaw().subscribe(response => { + this.feedService.getAllPostsRawByUserId(this.userId).subscribe(response => { this.parentSelectedPostList = this.feedService.renderAllPosts(response.json()); console.log('Refresh'); }); diff --git a/src/app/services/feed/feed.service.ts b/src/app/services/feed/feed.service.ts index 2b57a20..40351aa 100644 --- a/src/app/services/feed/feed.service.ts +++ b/src/app/services/feed/feed.service.ts @@ -85,6 +85,19 @@ export class FeedService { }); return this.posts; } + public getAllPostsById(userId: number): Array { + 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 { const url = environment.graphQLUrl; @@ -95,13 +108,29 @@ export class FeedService { 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() { const body = {query: `query { 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 {