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, debounce, capitalizeFirstLetter } from '../utils';
8 import * as autosize from 'autosize';
9 import { i18n } from '../i18next';
10 import { T } from 'inferno-i18next';
12 interface PostFormProps {
13 post?: Post; // If a post is given, that means this is an edit
14 prevCommunityName?: string;
16 onCreate?(id: number): any;
17 onEdit?(post: Post): any;
20 interface PostFormState {
22 communities: Array<Community>;
24 suggestedTitle: string;
25 suggestedPosts: Array<Post>;
28 export class PostForm extends Component<PostFormProps, PostFormState> {
30 private subscription: Subscription;
31 private emptyState: PostFormState = {
36 creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
40 suggestedTitle: undefined,
44 constructor(props: any, context: any) {
45 super(props, context);
47 this.state = this.emptyState;
49 if (this.props.post) {
50 this.state.postForm = {
51 body: this.props.post.body,
52 name: this.props.post.name,
53 community_id: this.props.post.community_id,
54 edit_id: this.props.post.id,
55 creator_id: this.props.post.creator_id,
56 url: this.props.post.url,
61 this.subscription = WebSocketService.Instance.subject
62 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
64 (msg) => this.parseMessage(msg),
65 (err) => console.error(err),
66 () => console.log('complete')
69 let listCommunitiesForm: ListCommunitiesForm = {
70 sort: SortType[SortType.TopAll],
74 WebSocketService.Instance.listCommunities(listCommunitiesForm);
78 autosize(document.querySelectorAll('textarea'));
81 componentWillUnmount() {
82 this.subscription.unsubscribe();
88 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
89 <div class="form-group row">
90 <label class="col-sm-2 col-form-label"><T i18nKey="url">#</T></label>
91 <div class="col-sm-10">
92 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, debounce(this.handlePostUrlChange))} />
93 {this.state.suggestedTitle &&
94 <div class="mt-1 text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle)}><T i18nKey="copy_suggested_title" interpolation={{title: this.state.suggestedTitle}}>#</T></div>
98 <div class="form-group row">
99 <label class="col-sm-2 col-form-label"><T i18nKey="title">#</T></label>
100 <div class="col-sm-10">
101 <textarea value={this.state.postForm.name} onInput={linkEvent(this, debounce(this.handlePostNameChange))} class="form-control" required rows={2} minLength={3} maxLength={100} />
102 {this.state.suggestedPosts.length > 0 &&
104 <div class="my-1 text-muted small font-weight-bold"><T i18nKey="related_posts">#</T></div>
105 <PostListings posts={this.state.suggestedPosts} />
110 <div class="form-group row">
111 <label class="col-sm-2 col-form-label"><T i18nKey="body">#</T></label>
112 <div class="col-sm-10">
113 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
116 {/* Cant change a community from an edit */}
118 <div class="form-group row">
119 <label class="col-sm-2 col-form-label"><T i18nKey="community">#</T></label>
120 <div class="col-sm-10">
121 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
122 {this.state.communities.map(community =>
123 <option value={community.id}>{community.name}</option>
129 <div class="form-group row">
130 <div class="col-sm-10">
131 <button type="submit" class="btn btn-secondary mr-2">
132 {this.state.loading ?
133 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
134 this.props.post ? capitalizeFirstLetter(i18n.t('save')) : capitalizeFirstLetter(i18n.t('create'))}</button>
135 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}><T i18nKey="cancel">#</T></button>}
143 handlePostSubmit(i: PostForm, event: any) {
144 event.preventDefault();
146 WebSocketService.Instance.editPost(i.state.postForm);
148 WebSocketService.Instance.createPost(i.state.postForm);
150 i.state.loading = true;
154 copySuggestedTitle(i: PostForm) {
155 i.state.postForm.name = i.state.suggestedTitle;
156 i.state.suggestedTitle = undefined;
160 handlePostUrlChange(i: PostForm, event: any) {
161 i.state.postForm.url = event.target.value;
162 getPageTitle(i.state.postForm.url).then(d => {
163 i.state.suggestedTitle = d;
169 handlePostNameChange(i: PostForm, event: any) {
170 i.state.postForm.name = event.target.value;
171 let form: SearchForm = {
172 q: i.state.postForm.name,
173 type_: SearchType[SearchType.Posts],
174 sort: SortType[SortType.TopAll],
175 community_id: i.state.postForm.community_id,
180 if (i.state.postForm.name !== '') {
181 WebSocketService.Instance.search(form);
183 i.state.suggestedPosts = [];
189 handlePostBodyChange(i: PostForm, event: any) {
190 i.state.postForm.body = event.target.value;
194 handlePostCommunityChange(i: PostForm, event: any) {
195 i.state.postForm.community_id = Number(event.target.value);
199 handleCancel(i: PostForm) {
203 parseMessage(msg: any) {
204 let op: UserOperation = msgOp(msg);
206 alert(i18n.t(msg.error));
207 this.state.loading = false;
208 this.setState(this.state);
210 } else if (op == UserOperation.ListCommunities) {
211 let res: ListCommunitiesResponse = msg;
212 this.state.communities = res.communities;
213 if (this.props.post) {
214 this.state.postForm.community_id = this.props.post.community_id;
215 } else if (this.props.prevCommunityName) {
216 let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
217 this.state.postForm.community_id = foundCommunityId;
219 this.state.postForm.community_id = res.communities[0].id;
221 this.setState(this.state);
222 } else if (op == UserOperation.CreatePost) {
223 this.state.loading = false;
224 let res: PostResponse = msg;
225 this.props.onCreate(res.post.id);
226 } else if (op == UserOperation.EditPost) {
227 this.state.loading = false;
228 let res: PostResponse = msg;
229 this.props.onEdit(res.post);
230 } else if (op == UserOperation.Search) {
231 let res: SearchResponse = msg;
232 this.state.suggestedPosts = res.posts;
233 this.setState(this.state);