X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fcommunity%2Fcommunity-form.tsx;h=e44675c91c608256aad56729ef91fb0de0f118cf;hb=9f2289d46600385354d3673b0ef2ba921a1fbe49;hp=ecc2b8900fdd40bd72b66c502a739e0283ad6fd2;hpb=eed07b66aa6b115bdad0880ec0b67f0f91ff8e8b;p=lemmy-ui.git diff --git a/src/shared/components/community/community-form.tsx b/src/shared/components/community/community-form.tsx index ecc2b89..e44675c 100644 --- a/src/shared/components/community/community-form.tsx +++ b/src/shared/components/community/community-form.tsx @@ -1,40 +1,42 @@ +import { myAuthRequired } from "@utils/app"; +import { capitalizeFirstLetter, randomStr } from "@utils/helpers"; import { Component, linkEvent } from "inferno"; import { Prompt } from "inferno-router"; import { - CommunityResponse, CommunityView, CreateCommunity, EditCommunity, - UserOperation, + Language, } from "lemmy-js-client"; -import { Subscription } from "rxjs"; -import { i18n } from "../../i18next"; -import { WebSocketService } from "../../services"; -import { - authField, - capitalizeFirstLetter, - randomStr, - toast, - wsClient, - wsJsonToRes, - wsSubscribe, - wsUserOp, -} from "../../utils"; +import { I18NextService } from "../../services"; 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; + onUpsertCommunity(form: CreateCommunity | EditCommunity): void; + enableNsfw?: boolean; + loading?: boolean; } interface CommunityFormState { - communityForm: CreateCommunity; - loading: boolean; + 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< @@ -42,25 +44,15 @@ export class CommunityForm extends Component< CommunityFormState > { private id = `community-form-${randomStr()}`; - private subscription: Subscription; - private emptyState: CommunityFormState = { - communityForm: { - name: null, - title: null, - nsfw: false, - icon: null, - banner: null, - auth: authField(false), - }, - loading: false, + state: CommunityFormState = { + form: {}, + submitted: false, }; constructor(props: any, context: any) { super(props, context); - this.state = this.emptyState; - this.handleCommunityDescriptionChange = this.handleCommunityDescriptionChange.bind(this); @@ -70,262 +62,299 @@ export class CommunityForm extends Component< this.handleBannerUpload = this.handleBannerUpload.bind(this); this.handleBannerRemove = this.handleBannerRemove.bind(this); - let cv = this.props.community_view; - if (cv) { - this.state.communityForm = { - name: cv.community.name, - title: cv.community.title, - description: cv.community.description, - nsfw: cv.community.nsfw, - icon: cv.community.icon, - banner: cv.community.banner, - auth: authField(), - }; - } + this.handleDiscussionLanguageChange = + this.handleDiscussionLanguageChange.bind(this); - this.parseMessage = this.parseMessage.bind(this); - this.subscription = wsSubscribe(this.parseMessage); - } + const cv = this.props.community_view; - // TODO this should be checked out - componentDidUpdate() { - if ( - !this.state.loading && - (this.state.communityForm.name || - this.state.communityForm.title || - this.state.communityForm.description) - ) { - window.onbeforeunload = () => true; - } else { - window.onbeforeunload = undefined; + 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, + }, + }; } } - componentWillUnmount() { - this.subscription.unsubscribe(); - window.onbeforeunload = null; - } - render() { return ( - <> +