Posts with activityies work now, most liked button works

master
Max 5 years ago
parent 894f8c498d
commit dc467d431a

@ -48,9 +48,15 @@
<!-- Name Column --> <!-- Name Column -->
<ng-container matColumnDef="name"> <ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> action </th> <th mat-header-cell *matHeaderCellDef mat-sort-header> action </th>
<td mat-cell *matCellDef="let action"> {{action.name}} </td> <td mat-cell *matCellDef="let action"> {{action.name}} </td>
</ng-container> </ng-container>
<!-- decriotion Column -->
<ng-container matColumnDef="description">
<th mat-header-cell *matHeaderCellDef mat-sort-header> description </th>
<td mat-cell *matCellDef="let action"> {{action.description}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

@ -3,6 +3,7 @@ import { Activitylist } from 'src/app/models/activity';
import { Levellist } from 'src/app/models/levellist'; import { Levellist } from 'src/app/models/levellist';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import { ActivityService } from 'src/app/services/activity/activity.service';
@Component({ @Component({
selector: 'app-about', selector: 'app-about',
@ -13,16 +14,21 @@ export class AboutComponent implements OnInit {
actionlist: Activitylist = new Activitylist(); actionlist: Activitylist = new Activitylist();
levellist: Levellist = new Levellist(); levellist: Levellist = new Levellist();
displayedColumns = ['points', 'name']; displayedColumns = ['points', 'name', 'description'];
dataSource = new MatTableDataSource(this.actionlist.Actions); dataSource = new MatTableDataSource(this.actionlist.Actions);
displayedLevelColumns = ['level', 'name']; displayedLevelColumns = ['level', 'name'];
levelSource = this.levellist.levels; levelSource = this.levellist.levels;
constructor() { } constructor(private activityService: ActivityService) { }
@ViewChild(MatSort, {static: true}) sort: MatSort; @ViewChild(MatSort, {static: true}) sort: MatSort;
ngOnInit() { ngOnInit() {
this.dataSource.sort = this.sort; this.activityService.getActivitys();
this.activityService.activitylist.subscribe(response => {
this.actionlist = response;
this.dataSource = new MatTableDataSource(this.actionlist.Actions);
this.dataSource.sort = this.sort;
});
} }
} }

@ -13,14 +13,14 @@
</p> </p>
<mat-form-field id="action-chooser" *ngIf="checked"> <mat-form-field id="action-chooser" *ngIf="checked">
<mat-label>What did you do?</mat-label> <mat-label>What did you do?</mat-label>
<mat-select [(ngModel)]="selectedValue" name="action"> <mat-select [(ngModel)]="activityId" name="action">
<mat-option>nothing ;)</mat-option> <mat-option>nothing ;)</mat-option>
<mat-option *ngFor="let action of actionlist.Actions" [value]="action.points"> <mat-option *ngFor="let action of actionlist.Actions" [value]="action.id" [matTooltip]="action.description" matTooltipShowDelay="200">
{{action.name}} {{action.name}}
</mat-option> </mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<button mat-raised-button *ngIf="empty" color="primary" id="post-button" (click)=createPost(content)> <button mat-raised-button *ngIf="empty" color="primary" id="post-button" (click)=createPost(content,activityId)>
POST POST
</button> </button>
</mat-card-content> </mat-card-content>
@ -44,12 +44,7 @@
</div> </div>
<div id="complete-feed"> <div id="complete-feed">
<div id="feedlist"> <div id="feedlist">
<div *ngIf = "viewNew"> <feed-postlist [childPostList]="parentSelectedPostList"></feed-postlist>
<feed-postlist (voteEvent)="refresh($event)" [childPostList]="parentSelectedPostList"></feed-postlist>
</div>
<div *ngIf = "viewMostLiked">
<feed-postlist (voteEvent)="refresh($event)" [childPostList]="parentSelectedPostList"></feed-postlist>
</div>
<mat-spinner *ngIf="loading" style="margin:0 auto; margin-top: 5em;" diameter="50"></mat-spinner> <mat-spinner *ngIf="loading" style="margin:0 auto; margin-top: 5em;" diameter="50"></mat-spinner>
</div> </div>
</div> </div>

@ -3,6 +3,7 @@ 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 { Activitylist } from 'src/app/models/activity'; import { Activitylist } from 'src/app/models/activity';
import { DatasharingService } from '../../services/datasharing.service'; import { DatasharingService } from '../../services/datasharing.service';
import { ActivityService } from 'src/app/services/activity/activity.service';
import { User } from 'src/app/models/user'; import { User } from 'src/app/models/user';
@Component({ @Component({
@ -12,70 +13,57 @@ import { User } from 'src/app/models/user';
}) })
export class FeedComponent implements OnInit { export class FeedComponent implements OnInit {
loading = true; loading = true;
checked: boolean; // if the "I protected the environment."-box is checked checked: boolean; // if the "I protected the environment."-box is checked
empty: boolean; empty: any;
// points value of the green action // id of the green activity
value: any; value: any;
viewNew = true;
viewMostLiked = false;
feedNew: Array<Post>;
feedMostLiked: Array<Post>;
parentSelectedPostList: Array<Post>; parentSelectedPostList: Array<Post>;
actionlist: Activitylist = new Activitylist(); actionlist: Activitylist = new Activitylist();
loggedIn = false; loggedIn = false;
userId: number;
user: User; user: User;
constructor(private feedService: FeedService, private data: DatasharingService) { } constructor(
private feedService: FeedService,
private data: DatasharingService,
private activityService: ActivityService
) { }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUserInfo.subscribe(user => {
this.user = user; this.user = user;
this.loggedIn = user.loggedIn; this.loggedIn = user.loggedIn;
}); });
this.feedService.getAllPostsRaw().subscribe(response => { this.activityService.getActivitys();
this.loading = false; this.activityService.activitylist.subscribe(response => {
this.feedNew = this.feedService.renderAllPosts(response.json()); this.actionlist = response;
this.parentSelectedPostList = this.feedNew; });
this.feedMostLiked = this.feedNew; this.feedService.getNewPosts();
this.feedService.posts.subscribe(response => {
if (response.length > 0) {this.loading = false; }
this.parentSelectedPostList = response;
}); });
} }
createPost(pElement) { createPost(pElement, activityId: string) {
this.feedService.createPost(pElement.value); if (pElement && activityId) {
this.feedService.createPostActivity(pElement.value, activityId);
pElement.value = ''; pElement.value = '';
this.feedService.getAllPostsRaw().subscribe(response => { this.empty = '';
this.feedNew = this.feedService.renderAllPosts(response.json()); } else if (pElement) {
this.parentSelectedPostList = this.feedNew; this.feedService.createPost(pElement.value);
this.feedMostLiked = this.feedNew; }); pElement.value = '';
this.empty = '';
}
} }
showNew() { showNew() {
this.feedService.getAllPostsRaw().subscribe(response => { this.feedService.getNewPosts();
this.feedNew = this.feedService.renderAllPosts(response.json());
this.parentSelectedPostList = this.feedNew; });
this.viewNew = true;
this.viewMostLiked = false;
} }
showMostLiked() { showMostLiked() {
this.feedService.getAllPostsRaw().subscribe(response => { this.feedService.getMostLikedPosts();
this.feedMostLiked = this.feedService.renderAllPosts(response.json());
this.parentSelectedPostList = this.feedMostLiked; });
this.viewNew = false;
this.viewMostLiked = true;
} }
refresh($event) {
this.feedService.getAllPostsRaw().subscribe(response => {
this.parentSelectedPostList = this.feedService.renderAllPosts(response.json());
});
}
} }

@ -56,6 +56,11 @@
<mat-icon aria-hidden="false" *ngIf="!post.userVote || post.userVote == 'UPVOTE'">keyboard_arrow_down</mat-icon> <mat-icon aria-hidden="false" *ngIf="!post.userVote || post.userVote == 'UPVOTE'">keyboard_arrow_down</mat-icon>
</button> </button>
{{post.downvotes}} {{post.downvotes}}
<div *ngIf="post.activity" class="activity-info">
<span class="span" [matTooltip]="post.activity.description" matTooltipShowDelay="200">
{{post.activity.points}} points earned through {{post.activity.name}}
</span>
</div>
</mat-card-actions> </mat-card-actions>
</mat-card> </mat-card>

@ -20,5 +20,15 @@
max-width: 100% max-width: 100%
height: auto height: auto
border-radius: 4px border-radius: 4px
.mat-button
min-width: 32px !important
padding: 0
margin: 0
margin-left: 8px
.activity-info
display: contents
.span
margin-left: 32px

@ -17,7 +17,6 @@ export class HomeComponent implements OnInit {
this.data.currentUserInfo.subscribe(user => { this.data.currentUserInfo.subscribe(user => {
this.loggedIn = user.loggedIn; this.loggedIn = user.loggedIn;
}); });
this.feedService.getAllPosts();
} }
} }

@ -1,10 +1,18 @@
export interface Action { export class Activity {
id: number; id: number;
name: string; name: string;
description: string;
points: number; points: number;
constructor(id: number, name: string, description: string, points: number) {
this.id = id;
this.name = name;
this.description = description;
this.points = points;
}
} }
export class Activitylist { export class Activitylist {
Actions: Action[] = new Array(); Actions: Activity[] = new Array();
} }

@ -1,4 +1,5 @@
import { Author } from './author'; import { Author } from './author';
import { Activity } from './activity';
export class Post { export class Post {
id: number; id: number;
@ -10,6 +11,7 @@ export class Post {
userVote: string; userVote: string;
deletable: boolean; deletable: boolean;
author: Author; author: Author;
activity: Activity;
// TODO: constructor properties need normal names // TODO: constructor properties need normal names
constructor( constructor(
@ -21,7 +23,8 @@ export class Post {
pUserVote: string, pUserVote: string,
pDeletable: boolean, pDeletable: boolean,
pDate: string, pDate: string,
pAuthor: Author pAuthor: Author,
pactivity: Activity
) { ) {
this.id = pId; this.id = pId;
this.content = pContent; this.content = pContent;
@ -32,5 +35,6 @@ export class Post {
this.deletable = pDeletable; this.deletable = pDeletable;
this.date = pDate; this.date = pDate;
this.author = pAuthor; this.author = pAuthor;
this.activity = pactivity;
} }
} }

@ -1,16 +1,16 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { Activitylist } from 'src/app/models/activity'; import { Activitylist, Activity } from 'src/app/models/activity';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { Http } from '@angular/http'; import { Http } from '@angular/http';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ActivityService { export class ActivityService {
private activitylist = new BehaviorSubject<Activitylist>(new Activitylist()); public activitylist = new BehaviorSubject<Activitylist>(new Activitylist());
currentActivityList = this.activitylist.asObservable();
constructor(private http: Http) { } constructor(private http: Http) { }
@ -19,53 +19,34 @@ export class ActivityService {
} }
public getActivitys() { public getActivitys() {
const headers = new Headers(); if (this.activitylist.getValue().Actions.length < 1) {
headers.set('Content-Type', 'application/json'); const headers = new Headers();
this.http.post(environment.graphQLUrl, this.buildJson()).subscribe(result => { headers.set('Content-Type', 'application/json');
// push onto subject this.http.post(environment.graphQLUrl, this.buildJson()).subscribe(result => {
this.activitylist.next(this.renderActivity(result.json())); // push onto subject
}); this.activitylist.next(this.renderActivity(result.json()));
});
}
} }
public buildJson(id: string): any { public buildJson(): any {
const body = {query: `query($userId: ID) { const body = {query: `query{getActivities{
getUser(userId:$userId){ id name description points
id }}`, variables: {
handle
name
profilePicture
points
level
friendCount
groupCount
joinedAt
friends{
id
}
posts{
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
author{
name,
handle,
id},
createdAt
}
}
}`, variables: {
userId: id,
}}; }};
return body; return body;
} }
public renderActivity(response: any): Activitylist { public renderActivity(response: any): Activitylist {
const activitylist = new Activitylist(); const activitylist = new Activitylist();
// activitylist.push(); for (const activity of response.data.getActivities) {
activitylist.Actions.push(new Activity(
activity.id,
activity.name,
activity.description,
activity.points));
}
return activitylist; return activitylist;
} }
} }

@ -3,29 +3,88 @@ import { Http } from '@angular/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 { Activity } from 'src/app/models/activity';
import { BehaviorSubject } from 'rxjs';
import { User } from 'src/app/models/user';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class FeedService { export class FeedService {
posts: Array<Post>; public posts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
public newPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
public mostLikedPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
constructor(private http: Http) { } constructor(private http: Http) { }
public createPost(pContent: String) { public createPost(pContent: String) {
const url = environment.graphQLUrl;
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
const body = {query: `mutation($content: String!) { const body = {query: `mutation($content: String!) {
createPost(content: $content) {id} createPost(content: $content) {
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
activity{
id
name
description
points
},
author{
name,
handle,
id},
createdAt}
}`, variables: { }`, variables: {
content: pContent content: pContent
}}; }};
return this.http.post(environment.graphQLUrl, body).subscribe(response => {
const updatedposts = this.newPosts.getValue();
updatedposts.unshift(this.renderPost(response.json()));
this.newPosts.next(updatedposts);
});
}
this.http.post(url, body).subscribe(response => { public createPostActivity(pContent: String, activityId: String) {
}); const headers = new Headers();
headers.set('Content-Type', 'application/json');
const body = {query: `mutation($content: String!, $id: ID) {
createPost(content: $content activityId: $id) {
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
activity{
id
name
description
points
},
author{
name,
handle,
id},
createdAt}
}`, variables: {
content: pContent,
id: activityId
}};
return this.http.post(environment.graphQLUrl, body).subscribe(response => {
const updatedposts = this.newPosts.getValue();
updatedposts.unshift(this.renderPost(response.json()));
this.newPosts.next(updatedposts);
});
} }
public upvote(pPostID: number): any { public upvote(pPostID: number): any {
@ -71,26 +130,31 @@ export class FeedService {
return this.http.post(environment.graphQLUrl, body); return this.http.post(environment.graphQLUrl, body);
} }
public getAllPosts(): Array<Post> { public getNewPosts() {
const url = environment.graphQLUrl; if (this.newPosts.getValue().length === 0) {
const headers = new Headers();
const headers = new Headers(); headers.set('Content-Type', 'application/json');
headers.set('Content-Type', 'application/json'); this.http.post(environment.graphQLUrl, this.buildJsonNew())
.subscribe(response => {
this.http.post(url, this.getBodyForGetAllPosts()) this.newPosts.next(this.renderAllPosts(response.json()));
.subscribe(response => { this.posts.next(this.newPosts.getValue());
this.posts = this.renderAllPosts(response.json());
}); });
return this.posts; } else {this.posts.next(this.newPosts.getValue()); }
} }
public getAllPostsRaw(): any { public getMostLikedPosts() {
const headers = new Headers(); if (this.mostLikedPosts.getValue().length === 0) {
headers.set('Content-Type', 'application/json'); const headers = new Headers();
return this.http.post(environment.graphQLUrl, this.getBodyForGetAllPosts()); headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.buildJsonMostLiked())
.subscribe(response => {
this.mostLikedPosts.next(this.renderAllPosts(response.json()));
this.posts.next(this.mostLikedPosts.getValue());
});
} else {this.posts.next(this.mostLikedPosts.getValue()); }
} }
getBodyForGetAllPosts() { buildJsonNew() {
const body = {query: `{ const body = {query: `{
getPosts (first: 1000, offset: 0) { getPosts (first: 1000, offset: 0) {
id, id,
@ -99,7 +163,13 @@ export class FeedService {
upvotes, upvotes,
downvotes, downvotes,
userVote, userVote,
deletable deletable,
activity{
id
name
description
points
},
author{ author{
name, name,
handle, handle,
@ -110,6 +180,56 @@ export class FeedService {
return body; return body;
} }
buildJsonMostLiked() {
const body = {query: `{
getPosts (first: 1000, offset: 0, sort: TOP) {
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
activity{
id
name
description
points
},
author{
name,
handle,
id},
createdAt}
}`, variables: {
}};
return body;
}
public renderPost(response: any): Post {
const post = response.data.createPost;
const id: number = post.id;
const content: string = post.content;
const htmlContent: string = post.htmlContent;
const upvotes: number = post.upvotes;
const downvotes: number = post.downvotes;
const userVote: string = post.userVote;
const deletable: boolean = post.deletable;
const author = new Author(post.author.id, post.author.name, post.author.handle);
const temp = new Date(Number(post.createdAt));
const date = temp.toLocaleString('en-GB');
let activity: Activity;
if (post.activity) {
activity = new Activity(
post.activity.id,
post.activity.name,
post.activity.description,
post.activity.points);
} else { activity = null; }
return new Post(id, content, htmlContent, upvotes, downvotes, userVote, deletable, date, author, activity);
}
public renderAllPosts(pResponse: any): Array<Post> { public renderAllPosts(pResponse: any): Array<Post> {
const posts = new Array<Post>(); const posts = new Array<Post>();
// let options = {year: 'numeric', month: 'short', day: 'numeric', hour: '' } // let options = {year: 'numeric', month: 'short', day: 'numeric', hour: '' }
@ -124,8 +244,15 @@ export class FeedService {
const author = new Author(post.author.id, post.author.name, post.author.handle); const author = new Author(post.author.id, post.author.name, post.author.handle);
const temp = new Date(Number(post.createdAt)); const temp = new Date(Number(post.createdAt));
const date = temp.toLocaleString('en-GB'); const date = temp.toLocaleString('en-GB');
let activity: Activity;
posts.push(new Post(id, content, htmlContent, upvotes, downvotes, userVote, deletable, date, author)); if (post.activity) {
activity = new Activity(
post.activity.id,
post.activity.name,
post.activity.description,
post.activity.points);
} else { activity = null; }
posts.push(new Post(id, content, htmlContent, upvotes, downvotes, userVote, deletable, date, author, activity));
} }
return posts; return posts;
} }

@ -5,6 +5,7 @@ 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 { Observable, Subject } from 'rxjs';
import { Activity } from 'src/app/models/activity';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -49,6 +50,12 @@ export class ProfileService {
downvotes, downvotes,
userVote, userVote,
deletable, deletable,
activity{
id
name
description
points
},
author{ author{
name, name,
handle, handle,
@ -88,7 +95,17 @@ export class ProfileService {
const author = new Author(post.author.id, post.author.name, post.author.handle); const author = new Author(post.author.id, post.author.name, post.author.handle);
const ptemp = new Date(Number(post.createdAt)); const ptemp = new Date(Number(post.createdAt));
const pdate = ptemp.toLocaleString('en-GB'); const pdate = ptemp.toLocaleString('en-GB');
posts.push(new Post(id, content, htmlContent, upvotes, downvotes, userVote, deletable, pdate, author)); let activity: Activity;
if (post.activity) {
activity = new Activity(
post.activity.id,
post.activity.name,
post.activity.description,
post.activity.points);
} else { activity = null; }
// tslint:disable-next-line: max-line-length
posts.push(new Post(id, content, htmlContent, upvotes, downvotes, userVote, deletable, pdate, author, activity));
} }
profile.posts = posts; profile.posts = posts;
return profile; return profile;

Loading…
Cancel
Save