1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { PostForm, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp } from '../utils';
7 import { MomentTime } from './moment-time';
11 communities: Array<Community>;
15 export class CreatePost extends Component<any, State> {
17 private subscription: Subscription;
18 private emptyState: State = {
27 constructor(props, context) {
28 super(props, context);
30 this.state = this.emptyState;
32 this.subscription = WebSocketService.Instance.subject
33 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
35 (msg) => this.parseMessage(msg),
36 (err) => console.error(err),
37 () => console.log('complete')
40 WebSocketService.Instance.listCommunities();
43 componentWillUnmount() {
44 this.subscription.unsubscribe();
49 <div class="container">
51 <div class="col-12 col-lg-6 mb-4">
62 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
63 <h3>Create a Post</h3>
64 <div class="form-group row">
65 <label class="col-sm-2 col-form-label">URL</label>
66 <div class="col-sm-10">
67 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
70 <div class="form-group row">
71 <label class="col-sm-2 col-form-label">Title</label>
72 <div class="col-sm-10">
73 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={3} />
76 <div class="form-group row">
77 <label class="col-sm-2 col-form-label">Body</label>
78 <div class="col-sm-10">
79 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={6} />
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">Forum</label>
84 <div class="col-sm-10">
85 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
86 {this.state.communities.map(community =>
87 <option value={community.id}>{community.name}</option>
92 <div class="form-group row">
93 <div class="col-sm-10">
94 <button type="submit" class="btn btn-secondary">Create Post</button>
102 handlePostSubmit(i: CreatePost, event) {
103 event.preventDefault();
104 WebSocketService.Instance.createPost(i.state.postForm);
107 handlePostUrlChange(i: CreatePost, event) {
108 i.state.postForm.url = event.target.value;
111 handlePostNameChange(i: CreatePost, event) {
112 i.state.postForm.name = event.target.value;
115 handlePostBodyChange(i: CreatePost, event) {
116 i.state.postForm.body = event.target.value;
119 handlePostCommunityChange(i: CreatePost, event) {
120 i.state.postForm.community_id = Number(event.target.value);
123 parseMessage(msg: any) {
125 let op: UserOperation = msgOp(msg);
129 } else if (op == UserOperation.ListCommunities) {
130 let res: ListCommunitiesResponse = msg;
131 this.state.communities = res.communities;
132 this.state.postForm.community_id = res.communities[0].id; // TODO set it to the default community
133 this.setState(this.state);
134 } else if (op == UserOperation.CreatePost) {
135 let res: PostResponse = msg;
136 this.props.history.push(`/post/${res.post.id}`);