import { Component, linkEvent } from "inferno";
import { Prompt } from "inferno-router";
import {
CommunityResponse,
CommunityView,
CreateCommunity,
EditCommunity,
Language,
UserOperation,
wsJsonToRes,
wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import {
capitalizeFirstLetter,
myAuth,
randomStr,
wsClient,
wsSubscribe,
} from "../../utils";
import { Icon, Spinner } from "../common/icon";
import { ImageUploadForm } from "../common/image-upload-form";
import { LanguageSelect } from "../common/language-select";
import { MarkdownTextArea } from "../common/markdown-textarea";
interface CommunityFormProps {
community_view?: CommunityView; // If a community is given, that means this is an edit
allLanguages: Language[];
siteLanguages: number[];
communityLanguages?: number[];
onCancel?(): any;
onCreate?(community: CommunityView): any;
onEdit?(community: CommunityView): any;
enableNsfw?: boolean;
}
interface CommunityFormState {
form: {
name?: string;
title?: string;
description?: string;
icon?: string;
banner?: string;
nsfw?: boolean;
posting_restricted_to_mods?: boolean;
discussion_languages?: number[];
};
loading: boolean;
}
export class CommunityForm extends Component<
CommunityFormProps,
CommunityFormState
> {
private id = `community-form-${randomStr()}`;
private subscription?: Subscription;
state: CommunityFormState = {
form: {},
loading: false,
};
constructor(props: any, context: any) {
super(props, context);
this.handleCommunityDescriptionChange =
this.handleCommunityDescriptionChange.bind(this);
this.handleIconUpload = this.handleIconUpload.bind(this);
this.handleIconRemove = this.handleIconRemove.bind(this);
this.handleBannerUpload = this.handleBannerUpload.bind(this);
this.handleBannerRemove = this.handleBannerRemove.bind(this);
this.handleDiscussionLanguageChange =
this.handleDiscussionLanguageChange.bind(this);
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
let cv = this.props.community_view;
if (cv) {
this.state = {
form: {
name: cv.community.name,
title: cv.community.title,
description: cv.community.description,
nsfw: cv.community.nsfw,
icon: cv.community.icon,
banner: cv.community.banner,
posting_restricted_to_mods: cv.community.posting_restricted_to_mods,
discussion_languages: this.props.communityLanguages,
},
loading: false,
};
}
}
componentDidUpdate() {
if (
!this.state.loading &&
(this.state.form.name ||
this.state.form.title ||
this.state.form.description)
) {
window.onbeforeunload = () => true;
} else {
window.onbeforeunload = null;
}
}
componentWillUnmount() {
this.subscription?.unsubscribe();
window.onbeforeunload = null;
}
render() {
return (
<>