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 } 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>;
23 export class PostForm extends Component<PostFormProps, PostFormState> {
25 private subscription: Subscription;
26 private emptyState: PostFormState = {
31 creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
37 constructor(props: any, context: any) {
38 super(props, context);
40 this.state = this.emptyState;
42 if (this.props.post) {
43 this.state.postForm = {
44 body: this.props.post.body,
45 name: this.props.post.name,
46 community_id: this.props.post.community_id,
47 edit_id: this.props.post.id,
48 creator_id: this.props.post.creator_id,
49 url: this.props.post.url,
54 this.subscription = WebSocketService.Instance.subject
55 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
57 (msg) => this.parseMessage(msg),
58 (err) => console.error(err),
59 () => console.log('complete')
62 let listCommunitiesForm: ListCommunitiesForm = {
63 sort: SortType[SortType.TopAll],
67 WebSocketService.Instance.listCommunities(listCommunitiesForm);
71 autosize(document.querySelectorAll('textarea'));
74 componentWillUnmount() {
75 this.subscription.unsubscribe();
81 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">URL</label>
84 <div class="col-sm-10">
85 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
88 <div class="form-group row">
89 <label class="col-sm-2 col-form-label">Title</label>
90 <div class="col-sm-10">
91 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} />
94 <div class="form-group row">
95 <label class="col-sm-2 col-form-label">Body</label>
96 <div class="col-sm-10">
97 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
100 {/* Cant change a community from an edit */}
102 <div class="form-group row">
103 <label class="col-sm-2 col-form-label">Community</label>
104 <div class="col-sm-10">
105 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
106 {this.state.communities.map(community =>
107 <option value={community.id}>{community.name}</option>
113 <div class="form-group row">
114 <div class="col-sm-10">
115 <button type="submit" class="btn btn-secondary mr-2">
116 {this.state.loading ?
117 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
118 this.props.post ? 'Save' : 'Create'}</button>
119 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
127 handlePostSubmit(i: PostForm, event: any) {
128 event.preventDefault();
130 WebSocketService.Instance.editPost(i.state.postForm);
132 WebSocketService.Instance.createPost(i.state.postForm);
134 i.state.loading = true;
138 handlePostUrlChange(i: PostForm, event: any) {
139 i.state.postForm.url = event.target.value;
143 handlePostNameChange(i: PostForm, event: any) {
144 i.state.postForm.name = event.target.value;
148 handlePostBodyChange(i: PostForm, event: any) {
149 i.state.postForm.body = event.target.value;
153 handlePostCommunityChange(i: PostForm, event: any) {
154 i.state.postForm.community_id = Number(event.target.value);
158 handleCancel(i: PostForm) {
162 parseMessage(msg: any) {
163 let op: UserOperation = msgOp(msg);
166 this.state.loading = false;
167 this.setState(this.state);
169 } else if (op == UserOperation.ListCommunities) {
170 let res: ListCommunitiesResponse = msg;
171 this.state.communities = res.communities;
172 if (this.props.post) {
173 this.state.postForm.community_id = this.props.post.community_id;
174 } else if (this.props.prevCommunityName) {
175 let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
176 this.state.postForm.community_id = foundCommunityId;
178 this.state.postForm.community_id = res.communities[0].id;
180 this.setState(this.state);
181 } else if (op == UserOperation.CreatePost) {
182 this.state.loading = false;
183 let res: PostResponse = msg;
184 this.props.onCreate(res.post.id);
185 } else if (op == UserOperation.EditPost) {
186 this.state.loading = false;
187 let res: PostResponse = msg;
188 this.props.onEdit(res.post);