2019-03-23 01:42:57 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
|
|
|
import { PostForm, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse } from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
|
|
|
import { msgOp } from '../utils';
|
2019-03-27 19:54:55 +00:00
|
|
|
import { MomentTime } from './moment-time';
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
postForm: PostForm;
|
2019-03-26 18:00:18 +00:00
|
|
|
communities: Array<Community>;
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class CreatePost extends Component<any, State> {
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: State = {
|
|
|
|
postForm: {
|
|
|
|
name: null,
|
|
|
|
auth: null,
|
|
|
|
community_id: null
|
|
|
|
},
|
|
|
|
communities: []
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
this.state = this.emptyState;
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
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();
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 mb-4">
|
2019-03-26 18:00:18 +00:00
|
|
|
{this.postForm()}
|
2019-03-23 01:42:57 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
postForm() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePostSubmit)}>
|
|
|
|
<h3>Create a Post</h3>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">URL</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">Title</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows="3" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">Body</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows="6" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">Forum</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
|
|
|
|
{this.state.communities.map(community =>
|
|
|
|
<option value={community.id}>{community.name}</option>
|
|
|
|
)}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<button type="submit" class="btn btn-secondary">Create Post</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostSubmit(i: CreatePost, event) {
|
|
|
|
event.preventDefault();
|
|
|
|
WebSocketService.Instance.createPost(i.state.postForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostUrlChange(i: CreatePost, event) {
|
|
|
|
i.state.postForm.url = event.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNameChange(i: CreatePost, event) {
|
|
|
|
i.state.postForm.name = event.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostBodyChange(i: CreatePost, event) {
|
|
|
|
i.state.postForm.body = event.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostCommunityChange(i: CreatePost, event) {
|
|
|
|
i.state.postForm.community_id = Number(event.target.value);
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
alert(msg.error);
|
|
|
|
return;
|
2019-03-26 18:00:18 +00:00
|
|
|
} else if (op == UserOperation.ListCommunities) {
|
|
|
|
let res: ListCommunitiesResponse = msg;
|
|
|
|
this.state.communities = res.communities;
|
2019-03-27 16:59:21 +00:00
|
|
|
this.state.postForm.community_id = res.communities[0].id; // TODO set it to the default community
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreatePost) {
|
|
|
|
let res: PostResponse = msg;
|
|
|
|
this.props.history.push(`/post/${res.post.id}`);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|