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';
10 communities: Array<Community>;
14 export class CreatePost extends Component<any, State> {
16 private subscription: Subscription;
17 private emptyState: State = {
26 constructor(props, context) {
27 super(props, context);
29 this.state = this.emptyState;
31 this.subscription = WebSocketService.Instance.subject
32 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
34 (msg) => this.parseMessage(msg),
35 (err) => console.error(err),
36 () => console.log('complete')
39 WebSocketService.Instance.listCommunities();
42 componentWillUnmount() {
43 this.subscription.unsubscribe();
48 <div class="container">
50 <div class="col-12 col-lg-6 mb-4">
61 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
62 <h3>Create a Post</h3>
63 <div class="form-group row">
64 <label class="col-sm-2 col-form-label">URL</label>
65 <div class="col-sm-10">
66 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
69 <div class="form-group row">
70 <label class="col-sm-2 col-form-label">Title</label>
71 <div class="col-sm-10">
72 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows="3" />
75 <div class="form-group row">
76 <label class="col-sm-2 col-form-label">Body</label>
77 <div class="col-sm-10">
78 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows="6" />
81 <div class="form-group row">
82 <label class="col-sm-2 col-form-label">Forum</label>
83 <div class="col-sm-10">
84 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
85 {this.state.communities.map(community =>
86 <option value={community.id}>{community.name}</option>
91 <div class="form-group row">
92 <div class="col-sm-10">
93 <button type="submit" class="btn btn-secondary">Create Post</button>
101 handlePostSubmit(i: CreatePost, event) {
102 event.preventDefault();
103 WebSocketService.Instance.createPost(i.state.postForm);
106 handlePostUrlChange(i: CreatePost, event) {
107 i.state.postForm.url = event.target.value;
111 handlePostNameChange(i: CreatePost, event) {
112 i.state.postForm.name = event.target.value;
116 handlePostBodyChange(i: CreatePost, event) {
117 i.state.postForm.body = event.target.value;
121 handlePostCommunityChange(i: CreatePost, event) {
122 i.state.postForm.community_id = Number(event.target.value);
126 parseMessage(msg: any) {
128 let op: UserOperation = msgOp(msg);
132 } else if (op == UserOperation.ListCommunities) {
133 let res: ListCommunitiesResponse = msg;
134 this.state.communities = res.communities;
135 this.setState(this.state);
136 } else if (op == UserOperation.CreatePost) {
137 let res: PostResponse = msg;
138 this.props.history.push(`/post/${res.post.id}`);