import { Component } from 'inferno';
import { PostForm } from './post-form';
import { WebSocketService } from '../services';
+import { PostFormParams } from '../interfaces';
import { i18n } from '../i18next';
import { T } from 'inferno-i18next';
<div class="row">
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
<h5><T i18nKey="create_post">#</T></h5>
- <PostForm onCreate={this.handlePostCreate} prevCommunityName={this.prevCommunityName} />
+ <PostForm onCreate={this.handlePostCreate} params={this.params} />
</div>
</div>
</div>
)
}
+ get params(): PostFormParams {
+ let urlParams = new URLSearchParams(this.props.location.search);
+ let params: PostFormParams = {
+ name: urlParams.get("name"),
+ community: urlParams.get("community") || this.prevCommunityName,
+ body: urlParams.get("body"),
+ url: urlParams.get("url"),
+ };
+
+ return params;
+ }
+
get prevCommunityName(): string {
if (this.props.match.params.name) {
return this.props.match.params.name;
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 { PostForm as PostFormI, PostFormParams, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces';
import { WebSocketService, UserService } from '../services';
import { msgOp, getPageTitle, debounce, validURL, capitalizeFirstLetter } from '../utils';
import * as autosize from 'autosize';
interface PostFormProps {
post?: Post; // If a post is given, that means this is an edit
- prevCommunityName?: string;
+ params?: PostFormParams;
onCancel?(): any;
onCreate?(id: number): any;
onEdit?(post: Post): any;
}
}
+ if (this.props.params) {
+ this.state.postForm.name = this.props.params.name;
+ if (this.props.params.url) {
+ this.state.postForm.url = this.props.params.url;
+ }
+ if (this.props.params.body) {
+ this.state.postForm.body = this.props.params.body;
+ }
+ }
+
this.subscription = WebSocketService.Instance.subject
- .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
- .subscribe(
- (msg) => this.parseMessage(msg),
+ .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,
- }
+ let listCommunitiesForm: ListCommunitiesForm = {
+ sort: SortType[SortType.TopAll],
+ limit: 9999,
+ }
- WebSocketService.Instance.listCommunities(listCommunitiesForm);
+ WebSocketService.Instance.listCommunities(listCommunitiesForm);
}
componentDidMount() {
<textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
</div>
</div>
- {/* Cant change a community from an edit */}
{!this.props.post &&
<div class="form-group row">
<label class="col-sm-2 col-form-label"><T i18nKey="community">#</T></label>
this.state.communities = res.communities;
if (this.props.post) {
this.state.postForm.community_id = this.props.post.community_id;
- } else if (this.props.prevCommunityName) {
- let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
+ } else if (this.props.params && this.props.params.community) {
+ let foundCommunityId = res.communities.find(r => r.name == this.props.params.community).id;
this.state.postForm.community_id = foundCommunityId;
} else {
this.state.postForm.community_id = res.communities[0].id;
<span class="pointer" onClick={linkEvent(this, this.handleSavePostClick)}>{post.saved ? i18n.t('unsave') : i18n.t('save')}</span>
</li>
<li className="list-inline-item mr-2">
- <span class="pointer" onClick={linkEvent(this, this.handleCrossPostClick)}><T i18nKey="cross_post">#</T></span>
+ <Link className="text-muted" to={`/create_post${this.crossPostParams}`}><T i18nKey="cross_post">#</T></Link>
</li>
{this.myPost &&
<>
WebSocketService.Instance.savePost(form);
}
- handleCrossPostClick(i: PostListing) {
- let params = `?name=${i.props.post.name}`;
- if (i.props.post.url) {
- params += `&url=${i.props.post.url}`;
+ get crossPostParams(): string {
+ let params = `?name=${this.props.post.name}`;
+ if (this.props.post.url) {
+ params += `&url=${this.props.post.url}`;
}
- if (i.props.post.body) {
- params += `&body=${i.props.post.body}`;
+ if (this.props.post.body) {
+ params += `&body=${this.props.post.body}`;
}
- i.context.router.history.push(`/create_post${params}`);
+ return params;
}
handleModRemoveShow(i: PostListing) {
this.state.scrolled = true;
this.markScrolledAsRead(this.state.scrolled_comment_id);
}
+
+ // Necessary if you are on a post and you click another post (same route)
+ if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
+ // Couldnt get a refresh working. This does for now.
+ location.reload();
+
+ // let currentId = this.props.match.params.id;
+ // WebSocketService.Instance.getPost(currentId);
+ // this.context.router.history.push('/sponsors');
+ // this.context.refresh();
+ // this.context.router.history.push(_lastProps.location.pathname);
+
+ }
}
markScrolledAsRead(commentId: number) {
} else if (op == UserOperation.GetPost) {
let res: GetPostResponse = msg;
this.state.post = res.post;
- this.state.post = res.post;
this.state.comments = res.comments;
this.state.community = res.community;
this.state.moderators = res.moderators;
document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
// Get cross-posts
- let form: SearchForm = {
- q: res.post.url,
- type_: SearchType[SearchType.Url],
- sort: SortType[SortType.TopAll],
- page: 1,
- limit: 6,
- };
-
- WebSocketService.Instance.search(form);
-
+ if (this.state.post.url) {
+ let form: SearchForm = {
+ q: this.state.post.url,
+ type_: SearchType[SearchType.Url],
+ sort: SortType[SortType.TopAll],
+ page: 1,
+ limit: 6,
+ };
+ WebSocketService.Instance.search(form);
+ }
+
this.setState(this.state);
} else if (op == UserOperation.CreateComment) {
let res: CommentResponse = msg;
)}
</ul>
<Link class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted || community.removed) && 'no-click'}`}
- to={`/create_post/c/${community.name}`}><T i18nKey="create_a_post">#</T></Link>
+ to={`/create_post?community=${community.name}`}><T i18nKey="create_a_post">#</T></Link>
<div>
{community.subscribed
? <button class="btn btn-sm btn-secondary btn-block" onClick={linkEvent(community.id, this.handleUnsubscribe)}><T i18nKey="unsubscribe">#</T></button>
<Route path={`/home/type/:type/sort/:sort/page/:page`} component={Main} />
<Route exact path={`/`} component={Main} />
<Route path={`/login`} component={Login} />
- <Route path={`/create_post/c/:name`} component={CreatePost} />
<Route path={`/create_post`} component={CreatePost} />
<Route path={`/create_community`} component={CreateCommunity} />
<Route path={`/communities/page/:page`} component={Communities} />
auth: string;
}
+export interface PostFormParams {
+ name: string;
+ url?: string;
+ body?: string;
+ community?: string;
+}
+
export interface GetPostResponse {
op: string;
post: Post;