1 import { Component, linkEvent } from 'inferno';
2 import { PostListings } from './post-listings';
3 import { Subscription } from "rxjs";
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, getPageTitle } from '../utils';
8 import * as autosize from 'autosize';
10 interface PostFormProps {
11 post?: Post; // If a post is given, that means this is an edit
12 prevCommunityName?: string;
14 onCreate?(id: number): any;
15 onEdit?(post: Post): any;
18 interface PostFormState {
20 communities: Array<Community>;
22 suggestedTitle: string;
23 suggestedPosts: Array<Post>;
26 export class PostForm extends Component<PostFormProps, PostFormState> {
28 private subscription: Subscription;
29 private emptyState: PostFormState = {
34 creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
38 suggestedTitle: undefined,
42 constructor(props: any, context: any) {
43 super(props, context);
45 this.state = this.emptyState;
47 if (this.props.post) {
48 this.state.postForm = {
49 body: this.props.post.body,
50 name: this.props.post.name,
51 community_id: this.props.post.community_id,
52 edit_id: this.props.post.id,
53 creator_id: this.props.post.creator_id,
54 url: this.props.post.url,
59 this.subscription = WebSocketService.Instance.subject
60 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
62 (msg) => this.parseMessage(msg),
63 (err) => console.error(err),
64 () => console.log('complete')
67 let listCommunitiesForm: ListCommunitiesForm = {
68 sort: SortType[SortType.TopAll],
72 WebSocketService.Instance.listCommunities(listCommunitiesForm);
76 autosize(document.querySelectorAll('textarea'));
79 componentWillUnmount() {
80 this.subscription.unsubscribe();
86 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
87 <div class="form-group row">
88 <label class="col-sm-2 col-form-label">URL</label>
89 <div class="col-sm-10">
90 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
91 {this.state.suggestedTitle &&
92 <div class="mt-1 text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle)}>copy suggested title: {this.state.suggestedTitle}</div>
96 <div class="form-group row">
97 <label class="col-sm-2 col-form-label">Title</label>
98 <div class="col-sm-10">
99 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} />
100 {this.state.suggestedPosts.length > 0 &&
102 <div class="my-1 text-muted small font-weight-bold">These posts might be related</div>
103 <PostListings posts={this.state.suggestedPosts} />
108 <div class="form-group row">
109 <label class="col-sm-2 col-form-label">Body</label>
110 <div class="col-sm-10">
111 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
114 {/* Cant change a community from an edit */}
116 <div class="form-group row">
117 <label class="col-sm-2 col-form-label">Community</label>
118 <div class="col-sm-10">
119 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
120 {this.state.communities.map(community =>
121 <option value={community.id}>{community.name}</option>
127 <div class="form-group row">
128 <div class="col-sm-10">
129 <button type="submit" class="btn btn-secondary mr-2">
130 {this.state.loading ?
131 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
132 this.props.post ? 'Save' : 'Create'}</button>
133 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
141 handlePostSubmit(i: PostForm, event: any) {
142 event.preventDefault();
144 WebSocketService.Instance.editPost(i.state.postForm);
146 WebSocketService.Instance.createPost(i.state.postForm);
148 i.state.loading = true;
152 copySuggestedTitle(i: PostForm) {
153 i.state.postForm.name = i.state.suggestedTitle;
154 i.state.suggestedTitle = undefined;
158 handlePostUrlChange(i: PostForm, event: any) {
159 i.state.postForm.url = event.target.value;
160 getPageTitle(i.state.postForm.url).then(d => {
161 i.state.suggestedTitle = d;
167 handlePostNameChange(i: PostForm, event: any) {
168 i.state.postForm.name = event.target.value;
170 let form: SearchForm = {
171 q: i.state.postForm.name,
172 type_: SearchType[SearchType.Posts],
173 sort: SortType[SortType.TopAll],
174 community_id: i.state.postForm.community_id,
179 if (i.state.postForm.name !== '') {
180 WebSocketService.Instance.search(form);
182 i.state.suggestedPosts = [];
188 handlePostBodyChange(i: PostForm, event: any) {
189 i.state.postForm.body = event.target.value;
193 handlePostCommunityChange(i: PostForm, event: any) {
194 i.state.postForm.community_id = Number(event.target.value);
198 handleCancel(i: PostForm) {
202 parseMessage(msg: any) {
203 let op: UserOperation = msgOp(msg);
206 this.state.loading = false;
207 this.setState(this.state);
209 } else if (op == UserOperation.ListCommunities) {
210 let res: ListCommunitiesResponse = msg;
211 this.state.communities = res.communities;
212 if (this.props.post) {
213 this.state.postForm.community_id = this.props.post.community_id;
214 } else if (this.props.prevCommunityName) {
215 let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
216 this.state.postForm.community_id = foundCommunityId;
218 this.state.postForm.community_id = res.communities[0].id;
220 this.setState(this.state);
221 } else if (op == UserOperation.CreatePost) {
222 this.state.loading = false;
223 let res: PostResponse = msg;
224 this.props.onCreate(res.post.id);
225 } else if (op == UserOperation.EditPost) {
226 this.state.loading = false;
227 let res: PostResponse = msg;
228 this.props.onEdit(res.post);
229 } else if (op == UserOperation.Search) {
230 let res: SearchResponse = msg;
231 this.state.suggestedPosts = res.posts;
232 this.setState(this.state);