1 import { Component } from "inferno";
2 import { Subscription } from "rxjs";
3 import { PostForm } from "./post-form";
4 import { HtmlTags } from "./html-tags";
5 import { Spinner } from "./icon";
17 import { UserService, WebSocketService } from "../services";
20 ListCommunitiesResponse,
27 } from "lemmy-js-client";
28 import { i18n } from "../i18next";
29 import { InitialFetchRequest, PostFormParams } from "shared/interfaces";
31 interface CreatePostState {
33 communities: CommunityView[];
37 export class CreatePost extends Component<any, CreatePostState> {
38 private isoData = setIsoData(this.context);
39 private subscription: Subscription;
40 private emptyState: CreatePostState = {
41 site_view: this.isoData.site_res.site_view,
46 constructor(props: any, context: any) {
47 super(props, context);
48 this.handlePostCreate = this.handlePostCreate.bind(this);
49 this.state = this.emptyState;
51 if (!UserService.Instance.user && isBrowser()) {
52 toast(i18n.t("not_logged_in"), "danger");
53 this.context.router.history.push(`/login`);
56 this.parseMessage = this.parseMessage.bind(this);
57 this.subscription = wsSubscribe(this.parseMessage);
59 // Only fetch the data if coming from another route
60 if (this.isoData.path == this.context.router.route.match.url) {
61 this.state.communities = this.isoData.routeData[0].communities;
62 this.state.loading = false;
69 let listCommunitiesForm: ListCommunities = {
70 type_: ListingType.All,
71 sort: SortType.TopAll,
73 auth: authField(false),
75 WebSocketService.Instance.send(
76 wsClient.listCommunities(listCommunitiesForm)
80 componentWillUnmount() {
82 this.subscription.unsubscribe();
86 get documentTitle(): string {
87 return `${i18n.t("create_post")} - ${this.state.site_view.site.name}`;
92 <div class="container">
94 title={this.documentTitle}
95 path={this.context.router.route.match.url}
97 {this.state.loading ? (
103 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
104 <h5>{i18n.t("create_post")}</h5>
106 communities={this.state.communities}
107 onCreate={this.handlePostCreate}
109 enableDownvotes={this.state.site_view.site.enable_downvotes}
110 enableNsfw={this.state.site_view.site.enable_nsfw}
119 get params(): PostFormParams {
120 let urlParams = new URLSearchParams(this.props.location.search);
121 let params: PostFormParams = {
122 name: urlParams.get("title"),
123 community_name: urlParams.get("community_name") || this.prevCommunityName,
124 community_id: urlParams.get("community_id")
125 ? Number(urlParams.get("community_id")) || this.prevCommunityId
127 body: urlParams.get("body"),
128 url: urlParams.get("url"),
134 get prevCommunityName(): string {
135 if (this.props.match.params.name) {
136 return this.props.match.params.name;
137 } else if (this.props.location.state) {
138 let lastLocation = this.props.location.state.prevPath;
139 if (lastLocation.includes("/c/")) {
140 return lastLocation.split("/c/")[1];
146 get prevCommunityId(): number {
147 if (this.props.match.params.id) {
148 return this.props.match.params.id;
149 } else if (this.props.location.state) {
150 let lastLocation = this.props.location.state.prevPath;
151 if (lastLocation.includes("/community/")) {
152 return Number(lastLocation.split("/community/")[1]);
158 handlePostCreate(post_view: PostView) {
159 this.props.history.push(`/post/${post_view.post.id}`);
162 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
163 let listCommunitiesForm: ListCommunities = {
164 type_: ListingType.All,
165 sort: SortType.TopAll,
168 setOptionalAuth(listCommunitiesForm, req.auth);
169 return [req.client.listCommunities(listCommunitiesForm)];
172 parseMessage(msg: any) {
173 let op = wsUserOp(msg);
175 toast(i18n.t(msg.error), "danger");
177 } else if (op == UserOperation.ListCommunities) {
178 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
179 this.state.communities = data.communities;
180 this.state.loading = false;
181 this.setState(this.state);