2020-09-06 16:15:25 +00:00
|
|
|
import { Component } from 'inferno';
|
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { PostForm } from './post-form';
|
2020-09-11 18:09:21 +00:00
|
|
|
import { HtmlTags } from './html-tags';
|
2021-02-11 20:35:27 +00:00
|
|
|
import { Spinner } from './icon';
|
2020-09-07 22:24:48 +00:00
|
|
|
import {
|
2020-12-24 22:05:57 +00:00
|
|
|
authField,
|
2020-09-08 18:44:55 +00:00
|
|
|
isBrowser,
|
2020-09-07 22:24:48 +00:00
|
|
|
setIsoData,
|
2020-12-24 22:05:57 +00:00
|
|
|
setOptionalAuth,
|
2020-09-07 22:24:48 +00:00
|
|
|
toast,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2020-09-07 22:24:48 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsSubscribe,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsUserOp,
|
2020-09-07 22:24:48 +00:00
|
|
|
} from '../utils';
|
|
|
|
import { UserService, WebSocketService } from '../services';
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
2020-09-07 22:24:48 +00:00
|
|
|
ListCommunitiesResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
CommunityView,
|
|
|
|
SiteView,
|
|
|
|
ListCommunities,
|
2020-09-07 22:24:48 +00:00
|
|
|
SortType,
|
2021-01-26 15:59:38 +00:00
|
|
|
ListingType,
|
2020-12-24 01:58:27 +00:00
|
|
|
PostView,
|
2020-09-06 16:15:25 +00:00
|
|
|
} from 'lemmy-js-client';
|
|
|
|
import { i18n } from '../i18next';
|
2020-12-24 01:58:27 +00:00
|
|
|
import { InitialFetchRequest, PostFormParams } from 'shared/interfaces';
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CreatePostState {
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: SiteView;
|
|
|
|
communities: CommunityView[];
|
2020-09-07 22:24:48 +00:00
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CreatePost extends Component<any, CreatePostState> {
|
2020-09-07 22:24:48 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreatePostState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: this.isoData.site_res.site_view,
|
2020-09-07 22:24:48 +00:00
|
|
|
communities: [],
|
|
|
|
loading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.handlePostCreate = this.handlePostCreate.bind(this);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-09-09 20:33:40 +00:00
|
|
|
if (!UserService.Instance.user && isBrowser()) {
|
2020-09-06 16:15:25 +00:00
|
|
|
toast(i18n.t('not_logged_in'), 'danger');
|
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.communities = this.isoData.routeData[0].communities;
|
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
refetch() {
|
2020-12-24 01:58:27 +00:00
|
|
|
let listCommunitiesForm: ListCommunities = {
|
2021-01-26 15:59:38 +00:00
|
|
|
type_: ListingType.All,
|
2020-09-07 22:24:48 +00:00
|
|
|
sort: SortType.TopAll,
|
|
|
|
limit: 9999,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(false),
|
2020-09-07 22:24:48 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listCommunities(listCommunitiesForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-08 18:44:55 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2020-12-24 01:58:27 +00:00
|
|
|
return `${i18n.t('create_post')} - ${this.state.site_view.site.name}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-07 22:24:48 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-07 22:24:48 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
|
|
|
<h5>{i18n.t('create_post')}</h5>
|
|
|
|
<PostForm
|
|
|
|
communities={this.state.communities}
|
|
|
|
onCreate={this.handlePostCreate}
|
|
|
|
params={this.params}
|
2020-12-24 01:58:27 +00:00
|
|
|
enableDownvotes={this.state.site_view.site.enable_downvotes}
|
|
|
|
enableNsfw={this.state.site_view.site.enable_nsfw}
|
2020-09-07 22:24:48 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2020-09-07 22:24:48 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get params(): PostFormParams {
|
|
|
|
let urlParams = new URLSearchParams(this.props.location.search);
|
|
|
|
let params: PostFormParams = {
|
|
|
|
name: urlParams.get('title'),
|
2020-11-17 01:34:41 +00:00
|
|
|
community_name: urlParams.get('community_name') || this.prevCommunityName,
|
|
|
|
community_id: urlParams.get('community_id')
|
|
|
|
? Number(urlParams.get('community_id')) || this.prevCommunityId
|
|
|
|
: null,
|
2020-09-06 16:15:25 +00:00
|
|
|
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;
|
|
|
|
} else if (this.props.location.state) {
|
|
|
|
let lastLocation = this.props.location.state.prevPath;
|
|
|
|
if (lastLocation.includes('/c/')) {
|
|
|
|
return lastLocation.split('/c/')[1];
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
return null;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 01:34:41 +00:00
|
|
|
get prevCommunityId(): number {
|
|
|
|
if (this.props.match.params.id) {
|
|
|
|
return this.props.match.params.id;
|
|
|
|
} else if (this.props.location.state) {
|
|
|
|
let lastLocation = this.props.location.state.prevPath;
|
|
|
|
if (lastLocation.includes('/community/')) {
|
|
|
|
return Number(lastLocation.split('/community/')[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handlePostCreate(post_view: PostView) {
|
|
|
|
this.props.history.push(`/post/${post_view.post.id}`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2020-12-24 01:58:27 +00:00
|
|
|
let listCommunitiesForm: ListCommunities = {
|
2021-01-26 15:59:38 +00:00
|
|
|
type_: ListingType.All,
|
2020-09-07 22:24:48 +00:00
|
|
|
sort: SortType.TopAll,
|
|
|
|
limit: 9999,
|
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
setOptionalAuth(listCommunitiesForm, req.auth);
|
2020-11-12 21:56:46 +00:00
|
|
|
return [req.client.listCommunities(listCommunitiesForm)];
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
|
|
|
toast(i18n.t(msg.error), 'danger');
|
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.ListCommunities) {
|
|
|
|
let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
|
2020-09-07 22:24:48 +00:00
|
|
|
this.state.communities = data.communities;
|
|
|
|
this.state.loading = false;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|