import { Component, linkEvent } from 'inferno'; import { PostListings } from './post-listings'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp, getPageTitle } from '../utils'; import * as autosize from 'autosize'; interface PostFormProps { post?: Post; // If a post is given, that means this is an edit prevCommunityName?: string; onCancel?(): any; onCreate?(id: number): any; onEdit?(post: Post): any; } interface PostFormState { postForm: PostFormI; communities: Array; loading: boolean; suggestedTitle: string; suggestedPosts: Array; } export class PostForm extends Component { private subscription: Subscription; private emptyState: PostFormState = { postForm: { name: null, auth: null, community_id: null, creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null, }, communities: [], loading: false, suggestedTitle: undefined, suggestedPosts: [], } constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; if (this.props.post) { this.state.postForm = { body: this.props.post.body, name: this.props.post.name, community_id: this.props.post.community_id, edit_id: this.props.post.id, creator_id: this.props.post.creator_id, url: this.props.post.url, auth: null } } this.subscription = WebSocketService.Instance.subject .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .subscribe( (msg) => this.parseMessage(msg), (err) => console.error(err), () => console.log('complete') ); let listCommunitiesForm: ListCommunitiesForm = { sort: SortType[SortType.TopAll], limit: 9999, } WebSocketService.Instance.listCommunities(listCommunitiesForm); } componentDidMount() { autosize(document.querySelectorAll('textarea')); } componentWillUnmount() { this.subscription.unsubscribe(); } render() { return (
{this.state.suggestedTitle &&
copy suggested title: {this.state.suggestedTitle}
}