From 25dcb8f4f4f80e401d1d3154923e2dcd05664c76 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 26 Mar 2019 11:00:18 -0700 Subject: Adding a few endpoints. - Adding CreatePost, CreateComment, CreateCommunity --- ui/src/components/community.tsx | 72 +++++++++++++++++++ ui/src/components/create-community.tsx | 19 ++--- ui/src/components/create-post.tsx | 121 ++++++++++++++++++++++++++----- ui/src/components/login.tsx | 5 +- ui/src/components/navbar.tsx | 1 + ui/src/components/post.tsx | 126 +++++++++++++++++++++++++++++++++ ui/src/index.tsx | 10 ++- ui/src/interfaces.ts | 81 ++++++++++++++++++--- ui/src/services/UserService.ts | 8 +-- ui/src/services/WebSocketService.ts | 52 ++++++++++++-- 10 files changed, 440 insertions(+), 55 deletions(-) create mode 100644 ui/src/components/community.tsx create mode 100644 ui/src/components/post.tsx (limited to 'ui') diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx new file mode 100644 index 00000000..b0322635 --- /dev/null +++ b/ui/src/components/community.tsx @@ -0,0 +1,72 @@ +import { Component, linkEvent } from 'inferno'; +import { Subscription } from "rxjs"; +import { retryWhen, delay, take } from 'rxjs/operators'; +import { UserOperation, Community as CommunityI, CommunityResponse, Post } from '../interfaces'; +import { WebSocketService, UserService } from '../services'; +import { msgOp } from '../utils'; + +interface State { + community: CommunityI; + posts: Array; +} + +export class Community extends Component { + + private subscription: Subscription; + private emptyState: State = { + community: { + id: null, + name: null, + published: null + }, + posts: [] + } + + constructor(props, context) { + super(props, context); + + this.state = this.emptyState; + + console.log(this.props.match.params.id); + + 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') + ); + + let communityId = Number(this.props.match.params.id); + WebSocketService.Instance.getCommunity(communityId); + } + + componentWillUnmount() { + this.subscription.unsubscribe(); + } + + render() { + return ( +
+
+
+ {this.state.community.name} +
+
+
+ ) + } + + parseMessage(msg: any) { + console.log(msg); + let op: UserOperation = msgOp(msg); + if (msg.error) { + alert(msg.error); + return; + } else if (op == UserOperation.GetCommunity) { + let res: CommunityResponse = msg; + this.state.community = res.community; + this.setState(this.state); + } + } +} diff --git a/ui/src/components/create-community.tsx b/ui/src/components/create-community.tsx index 159147b6..0a0edae6 100644 --- a/ui/src/components/create-community.tsx +++ b/ui/src/components/create-community.tsx @@ -11,20 +11,20 @@ interface State { communityForm: CommunityForm; } -let emptyState: State = { - communityForm: { - name: null, - } -} - export class CreateCommunity extends Component { private subscription: Subscription; + private emptyState: State = { + communityForm: { + name: null, + } + } + constructor(props, context) { super(props, context); - this.state = emptyState; - + this.state = this.emptyState; + this.subscription = WebSocketService.Instance.subject .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .subscribe( @@ -89,7 +89,8 @@ export class CreateCommunity extends Component { return; } else { if (op == UserOperation.CreateCommunity) { - let community: Community = msg.data; + let community: Community = msg.community; + this.props.history.push(`/community/${community.id}`); } } } diff --git a/ui/src/components/create-post.tsx b/ui/src/components/create-post.tsx index bb6e60e2..9ddf8c97 100644 --- a/ui/src/components/create-post.tsx +++ b/ui/src/components/create-post.tsx @@ -1,56 +1,141 @@ import { Component, linkEvent } from 'inferno'; - -import { LoginForm, PostForm, UserOperation } from '../interfaces'; +import { Subscription } from "rxjs"; +import { retryWhen, delay, take } from 'rxjs/operators'; +import { PostForm, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp } from '../utils'; interface State { postForm: PostForm; + communities: Array; } -let emptyState: State = { - postForm: { - name: null, - url: null, - attributed_to: null - } -} export class CreatePost extends Component { + private subscription: Subscription; + private emptyState: State = { + postForm: { + name: null, + auth: null, + community_id: null + }, + communities: [] + } + constructor(props, context) { super(props, context); - this.state = emptyState; + this.state = this.emptyState; - WebSocketService.Instance.subject.subscribe( - (msg) => this.parseMessage(msg), - (err) => console.error(err), - () => console.log('complete') - ); + 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(); } + componentWillUnmount() { + this.subscription.unsubscribe(); + } render() { return (
- create post - {/* {this.postForm()} */} + {this.postForm()}
) } + postForm() { + return ( +
+
+

Create a Post

+
+ +
+ +
+
+
+ +
+