From 5e23f00672b1a6b6e76694622da19c108ce4e3a5 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 4 Apr 2019 13:00:19 -0700 Subject: [PATCH] Adding forum / community page - Adding the page. Fixes #17. - Adding number of comments for community. - Add sorting to that table. --- .../up.sql | 3 +- server/src/actions/community_view.rs | 4 +- ui/src/components/communities.tsx | 80 +++++++++++++++++++ ui/src/components/create-community.tsx | 2 +- ui/src/components/navbar.tsx | 2 +- ui/src/components/sidebar.tsx | 9 ++- ui/src/index.html | 2 + ui/src/index.tsx | 2 + ui/src/interfaces.ts | 1 + 9 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 ui/src/components/communities.tsx diff --git a/server/migrations/2019-04-03-155205_create_community_view/up.sql b/server/migrations/2019-04-03-155205_create_community_view/up.sql index 7497250..d26a313 100644 --- a/server/migrations/2019-04-03-155205_create_community_view/up.sql +++ b/server/migrations/2019-04-03-155205_create_community_view/up.sql @@ -3,7 +3,8 @@ select *, (select name from user_ u where c.creator_id = u.id) as creator_name, (select name from category ct where c.category_id = ct.id) as category_name, (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers, -(select count(*) from post p where p.community_id = c.id) as number_of_posts +(select count(*) from post p where p.community_id = c.id) as number_of_posts, +(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments from community c; create view community_moderator_view as diff --git a/server/src/actions/community_view.rs b/server/src/actions/community_view.rs index 9da6215..03d822a 100644 --- a/server/src/actions/community_view.rs +++ b/server/src/actions/community_view.rs @@ -17,6 +17,7 @@ table! { category_name -> Varchar, number_of_subscribers -> BigInt, number_of_posts -> BigInt, + number_of_comments -> BigInt, } } @@ -34,7 +35,8 @@ pub struct CommunityView { pub creator_name: String, pub category_name: String, pub number_of_subscribers: i64, - pub number_of_posts: i64 + pub number_of_posts: i64, + pub number_of_comments: i64 } impl CommunityView { diff --git a/ui/src/components/communities.tsx b/ui/src/components/communities.tsx new file mode 100644 index 0000000..411aebe --- /dev/null +++ b/ui/src/components/communities.tsx @@ -0,0 +1,80 @@ +import { Component, linkEvent } from 'inferno'; +import { Link } from 'inferno-router'; +import { Subscription } from "rxjs"; +import { retryWhen, delay, take } from 'rxjs/operators'; +import { UserOperation, Community, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CommentSortType, CreatePostLikeResponse, ListCommunitiesResponse } from '../interfaces'; +import { WebSocketService, UserService } from '../services'; +import { msgOp, hotRank,mdToHtml } from '../utils'; + +interface CommunitiesState { + communities: Array; +} + +export class Communities extends Component { + private subscription: Subscription; + private emptyState: CommunitiesState = { + communities: [] + } + + constructor(props, context) { + super(props, context); + this.state = this.emptyState; + this.subscription = WebSocketService.Instance.subject + .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) + .subscribe( + (msg) => this.parseMessage(msg), + (err) => console.error(err), + () => console.log('complete') + ); + WebSocketService.Instance.listCommunities(); + } + + render() { + return ( +
+
+ + + + + + + + + + + + + {this.state.communities.map(community => + + + + + + + + + )} + +
NameTitleCategorySubscribersPostsComments
{community.name}{community.title}{community.category_name}{community.number_of_subscribers}{community.number_of_posts}{community.number_of_comments}
+
+
+ ); + } + + + + parseMessage(msg: any) { + console.log(msg); + let op: UserOperation = msgOp(msg); + if (msg.error) { + alert(msg.error); + return; + } else if (op == UserOperation.ListCommunities) { + let res: ListCommunitiesResponse = msg; + this.state.communities = res.communities; + this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers); + this.setState(this.state); + } + } +} diff --git a/ui/src/components/create-community.tsx b/ui/src/components/create-community.tsx index c5149f0..e21db8a 100644 --- a/ui/src/components/create-community.tsx +++ b/ui/src/components/create-community.tsx @@ -64,7 +64,7 @@ export class CreateCommunity extends Component {
- +
diff --git a/ui/src/components/navbar.tsx b/ui/src/components/navbar.tsx index 1af592b..4442499 100644 --- a/ui/src/components/navbar.tsx +++ b/ui/src/components/navbar.tsx @@ -37,7 +37,7 @@ export class Navbar extends Component { About