1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp, getPageTitle } from '../utils';
7 import * as autosize from 'autosize';
9 interface PostFormProps {
10 post?: Post; // If a post is given, that means this is an edit
11 prevCommunityName?: string;
13 onCreate?(id: number): any;
14 onEdit?(post: Post): any;
17 interface PostFormState {
19 communities: Array<Community>;
21 suggestedTitle: string;
24 export class PostForm extends Component<PostFormProps, PostFormState> {
26 private subscription: Subscription;
27 private emptyState: PostFormState = {
32 creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
36 suggestedTitle: undefined,
39 constructor(props: any, context: any) {
40 super(props, context);
42 this.state = this.emptyState;
44 if (this.props.post) {
45 this.state.postForm = {
46 body: this.props.post.body,
47 name: this.props.post.name,
48 community_id: this.props.post.community_id,
49 edit_id: this.props.post.id,
50 creator_id: this.props.post.creator_id,
51 url: this.props.post.url,
56 this.subscription = WebSocketService.Instance.subject
57 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
59 (msg) => this.parseMessage(msg),
60 (err) => console.error(err),
61 () => console.log('complete')
64 let listCommunitiesForm: ListCommunitiesForm = {
65 sort: SortType[SortType.TopAll],
69 WebSocketService.Instance.listCommunities(listCommunitiesForm);
73 autosize(document.querySelectorAll('textarea'));
76 componentWillUnmount() {
77 this.subscription.unsubscribe();
83 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
84 <div class="form-group row">
85 <label class="col-sm-2 col-form-label">URL</label>
86 <div class="col-sm-10">
87 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
88 {this.state.suggestedTitle &&
89 <span class="text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle}>copy suggested title: {this.state.suggestedTitle}
94 <div class="form-group row">
95 <label class="col-sm-2 col-form-label">Title</label>
96 <div class="col-sm-10">
97 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} />
100 <div class="form-group row">
101 <label class="col-sm-2 col-form-label">Body</label>
102 <div class="col-sm-10">
103 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
106 {/* Cant change a community from an edit */}
108 <div class="form-group row">
109 <label class="col-sm-2 col-form-label">Community</label>
110 <div class="col-sm-10">
111 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
112 {this.state.communities.map(community =>
113 <option value={community.id}>{community.name}</option>
119 <div class="form-group row">
120 <div class="col-sm-10">
121 <button type="submit" class="btn btn-secondary mr-2">
122 {this.state.loading ?
123 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
124 this.props.post ? 'Save' : 'Create'}</button>
125 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
133 handlePostSubmit(i: PostForm, event: any) {
134 event.preventDefault();
136 WebSocketService.Instance.editPost(i.state.postForm);
138 WebSocketService.Instance.createPost(i.state.postForm);
140 i.state.loading = true;
144 copySuggestedTitle(i: PostForm) {
145 i.state.postForm.name = i.state.suggestedTitle;
146 i.state.suggestedTitle = undefined;
150 handlePostUrlChange(i: PostForm, event: any) {
151 i.state.postForm.url = event.target.value;
152 getPageTitle(i.state.postForm.url).then(d => {
153 i.state.suggestedTitle = d;
159 handlePostNameChange(i: PostForm, event: any) {
160 i.state.postForm.name = event.target.value;
164 handlePostBodyChange(i: PostForm, event: any) {
165 i.state.postForm.body = event.target.value;
169 handlePostCommunityChange(i: PostForm, event: any) {
170 i.state.postForm.community_id = Number(event.target.value);
174 handleCancel(i: PostForm) {
178 parseMessage(msg: any) {
179 let op: UserOperation = msgOp(msg);
182 this.state.loading = false;
183 this.setState(this.state);
185 } else if (op == UserOperation.ListCommunities) {
186 let res: ListCommunitiesResponse = msg;
187 this.state.communities = res.communities;
188 if (this.props.post) {
189 this.state.postForm.community_id = this.props.post.community_id;
190 } else if (this.props.prevCommunityName) {
191 let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
192 this.state.postForm.community_id = foundCommunityId;
194 this.state.postForm.community_id = res.communities[0].id;
196 this.setState(this.state);
197 } else if (op == UserOperation.CreatePost) {
198 this.state.loading = false;
199 let res: PostResponse = msg;
200 this.props.onCreate(res.post.id);
201 } else if (op == UserOperation.EditPost) {
202 this.state.loading = false;
203 let res: PostResponse = msg;
204 this.props.onEdit(res.post);