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}</span>
93 <div class="form-group row">
94 <label class="col-sm-2 col-form-label">Title</label>
95 <div class="col-sm-10">
96 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} />
99 <div class="form-group row">
100 <label class="col-sm-2 col-form-label">Body</label>
101 <div class="col-sm-10">
102 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
105 {/* Cant change a community from an edit */}
107 <div class="form-group row">
108 <label class="col-sm-2 col-form-label">Community</label>
109 <div class="col-sm-10">
110 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
111 {this.state.communities.map(community =>
112 <option value={community.id}>{community.name}</option>
118 <div class="form-group row">
119 <div class="col-sm-10">
120 <button type="submit" class="btn btn-secondary mr-2">
121 {this.state.loading ?
122 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
123 this.props.post ? 'Save' : 'Create'}</button>
124 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
132 handlePostSubmit(i: PostForm, event: any) {
133 event.preventDefault();
135 WebSocketService.Instance.editPost(i.state.postForm);
137 WebSocketService.Instance.createPost(i.state.postForm);
139 i.state.loading = true;
143 copySuggestedTitle(i: PostForm) {
144 i.state.postForm.name = i.state.suggestedTitle;
145 i.state.suggestedTitle = undefined;
149 handlePostUrlChange(i: PostForm, event: any) {
150 i.state.postForm.url = event.target.value;
151 getPageTitle(i.state.postForm.url).then(d => {
152 i.state.suggestedTitle = d;
158 handlePostNameChange(i: PostForm, event: any) {
159 i.state.postForm.name = event.target.value;
163 handlePostBodyChange(i: PostForm, event: any) {
164 i.state.postForm.body = event.target.value;
168 handlePostCommunityChange(i: PostForm, event: any) {
169 i.state.postForm.community_id = Number(event.target.value);
173 handleCancel(i: PostForm) {
177 parseMessage(msg: any) {
178 let op: UserOperation = msgOp(msg);
181 this.state.loading = false;
182 this.setState(this.state);
184 } else if (op == UserOperation.ListCommunities) {
185 let res: ListCommunitiesResponse = msg;
186 this.state.communities = res.communities;
187 if (this.props.post) {
188 this.state.postForm.community_id = this.props.post.community_id;
189 } else if (this.props.prevCommunityName) {
190 let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
191 this.state.postForm.community_id = foundCommunityId;
193 this.state.postForm.community_id = res.communities[0].id;
195 this.setState(this.state);
196 } else if (op == UserOperation.CreatePost) {
197 this.state.loading = false;
198 let res: PostResponse = msg;
199 this.props.onCreate(res.post.id);
200 } else if (op == UserOperation.EditPost) {
201 this.state.loading = false;
202 let res: PostResponse = msg;
203 this.props.onEdit(res.post);