import { myAuthRequired } from "@utils/app"; import { capitalizeFirstLetter, randomStr } from "@utils/helpers"; import { Component, linkEvent } from "inferno"; import { CommunityView, CreateCommunity, EditCommunity, Language, } from "lemmy-js-client"; import { i18n } from "../../i18next"; 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"; import NavigationPrompt from "../common/navigation-prompt"; interface CommunityFormProps { community_view?: CommunityView; // If a community is given, that means this is an edit allLanguages: Language[]; siteLanguages: number[]; communityLanguages?: number[]; onCancel?(): any; onUpsertCommunity(form: CreateCommunity | EditCommunity): void; enableNsfw?: boolean; loading?: boolean; } interface CommunityFormState { form: { name?: string; title?: string; description?: string; icon?: string; banner?: string; nsfw?: boolean; posting_restricted_to_mods?: boolean; discussion_languages?: number[]; }; submitted: boolean; } export class CommunityForm extends Component< CommunityFormProps, CommunityFormState > { private id = `community-form-${randomStr()}`; state: CommunityFormState = { form: {}, submitted: 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); const cv = this.props.community_view; if (cv) { this.state = { ...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, }, }; } } render() { return (
{!this.props.community_view && (
)}
{this.props.enableNsfw && (
{i18n.t("nsfw")}
)}
{i18n.t("only_mods_can_post_in_community")}
{this.props.community_view && ( )}
); } handleCreateCommunitySubmit(i: CommunityForm, event: any) { event.preventDefault(); i.setState({ submitted: true }); const cForm = i.state.form; const auth = myAuthRequired(); const cv = i.props.community_view; if (cv) { i.props.onUpsertCommunity({ community_id: cv.community.id, title: cForm.title, description: cForm.description, icon: cForm.icon, banner: cForm.banner, nsfw: cForm.nsfw, posting_restricted_to_mods: cForm.posting_restricted_to_mods, discussion_languages: cForm.discussion_languages, auth, }); } else { if (cForm.title && cForm.name) { i.props.onUpsertCommunity({ name: cForm.name, title: cForm.title, description: cForm.description, icon: cForm.icon, banner: cForm.banner, nsfw: cForm.nsfw, posting_restricted_to_mods: cForm.posting_restricted_to_mods, discussion_languages: cForm.discussion_languages, auth, }); } } } handleCommunityNameChange(i: CommunityForm, event: any) { i.setState(s => ((s.form.name = event.target.value), s)); } handleCommunityTitleChange(i: CommunityForm, event: any) { i.setState(s => ((s.form.title = event.target.value), s)); } handleCommunityDescriptionChange(val: string) { this.setState(s => ((s.form.description = val), s)); } handleCommunityNsfwChange(i: CommunityForm, event: any) { i.setState(s => ((s.form.nsfw = event.target.checked), s)); } handleCommunityPostingRestrictedToMods(i: CommunityForm, event: any) { i.setState( s => ((s.form.posting_restricted_to_mods = event.target.checked), s) ); } handleCancel(i: CommunityForm) { i.props.onCancel?.(); } handleIconUpload(url: string) { this.setState(s => ((s.form.icon = url), s)); } handleIconRemove() { this.setState(s => ((s.form.icon = ""), s)); } handleBannerUpload(url: string) { this.setState(s => ((s.form.banner = url), s)); } handleBannerRemove() { this.setState(s => ((s.form.banner = ""), s)); } handleDiscussionLanguageChange(val: number[]) { this.setState(s => ((s.form.discussion_languages = val), s)); } }