X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fcommunity%2Fcommunity-form.tsx;h=e44675c91c608256aad56729ef91fb0de0f118cf;hb=9f2289d46600385354d3673b0ef2ba921a1fbe49;hp=0f12a252dccc12d1a23aca7616899ae38ee79e5e;hpb=4730fe618bda3a34419395dc0d155b5f93c60222;p=lemmy-ui.git diff --git a/src/shared/components/community/community-form.tsx b/src/shared/components/community/community-form.tsx index 0f12a25..e44675c 100644 --- a/src/shared/components/community/community-form.tsx +++ b/src/shared/components/community/community-form.tsx @@ -1,39 +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 { UserService, WebSocketService } from "../../services"; -import { - authField, - capitalizeFirstLetter, - randomStr, - 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< @@ -41,26 +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, - posting_restricted_to_mods: false, - 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,326 +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, - posting_restricted_to_mods: cv.community.posting_restricted_to_mods, - 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 ( - <> +
- - {!this.props.community_view && ( -
- -
- -
-
- )} -
+ {!this.props.community_view && ( +
-
+
-
- -
- -
+ )} +
+ +
+
-
- -
- -
+
+
+ +
+
-
- -
- -
+
+
+ +
+ +
+
+
+ +
+
+
- {this.props.enableNsfw && ( -
- - {i18n.t("nsfw")} - -
-
- -
-
-
- )} -
- - {i18n.t("only_mods_can_post_in_community")} + {this.props.enableNsfw && ( +
+ + {I18NextService.i18n.t("nsfw")} -
-
+
+
-
-
+ )} +
+ + {I18NextService.i18n.t("only_mods_can_post_in_community")} + +
+
+ +
+
+
+ +
+
+ + {this.props.community_view && ( - {this.props.community_view && ( - - )} -
+ )}
- - +
+ ); } handleCreateCommunitySubmit(i: CommunityForm, event: any) { event.preventDefault(); - i.state.loading = true; - if (i.props.community_view) { - let form: EditCommunity = { - ...i.state.communityForm, - community_id: i.props.community_view.community.id, - }; - WebSocketService.Instance.send(wsClient.editCommunity(form)); + 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 { - WebSocketService.Instance.send( - wsClient.createCommunity(i.state.communityForm) - ); + 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, + }); + } } - i.setState(i.state); } handleCommunityNameChange(i: CommunityForm, event: any) { - i.state.communityForm.name = event.target.value; - i.setState(i.state); + i.setState(s => ((s.form.name = event.target.value), s)); } handleCommunityTitleChange(i: CommunityForm, event: any) { - i.state.communityForm.title = event.target.value; - i.setState(i.state); + i.setState(s => ((s.form.title = event.target.value), s)); } handleCommunityDescriptionChange(val: string) { - this.state.communityForm.description = val; - this.setState(this.state); + this.setState(s => ((s.form.description = val), s)); } handleCommunityNsfwChange(i: CommunityForm, event: any) { - i.state.communityForm.nsfw = event.target.checked; - i.setState(i.state); + i.setState(s => ((s.form.nsfw = event.target.checked), s)); } handleCommunityPostingRestrictedToMods(i: CommunityForm, event: any) { - i.state.communityForm.posting_restricted_to_mods = event.target.checked; - i.setState(i.state); + i.setState( + s => ((s.form.posting_restricted_to_mods = event.target.checked), s), + ); } handleCancel(i: CommunityForm) { - i.props.onCancel(); + i.props.onCancel?.(); } handleIconUpload(url: string) { - this.state.communityForm.icon = url; - this.setState(this.state); + this.setState(s => ((s.form.icon = url), s)); } handleIconRemove() { - this.state.communityForm.icon = ""; - this.setState(this.state); + this.setState(s => ((s.form.icon = ""), s)); } handleBannerUpload(url: string) { - this.state.communityForm.banner = url; - this.setState(this.state); + this.setState(s => ((s.form.banner = url), s)); } handleBannerRemove() { - this.state.communityForm.banner = ""; - this.setState(this.state); + this.setState(s => ((s.form.banner = ""), s)); } - parseMessage(msg: any) { - let op = wsUserOp(msg); - console.log(msg); - if (msg.error) { - // Errors handled by top level pages - // toast(i18n.t(msg.error), "danger"); - this.state.loading = false; - this.setState(this.state); - return; - } else if (op == UserOperation.CreateCommunity) { - let data = wsJsonToRes(msg).data; - this.state.loading = false; - this.props.onCreate(data.community_view); - - // Update myUserInfo - let community = data.community_view.community; - let person = UserService.Instance.myUserInfo.local_user_view.person; - UserService.Instance.myUserInfo.follows.push({ - community, - follower: person, - }); - UserService.Instance.myUserInfo.moderates.push({ - community, - moderator: person, - }); - } else if (op == UserOperation.EditCommunity) { - let data = wsJsonToRes(msg).data; - this.state.loading = false; - this.props.onEdit(data.community_view); - let community = data.community_view.community; - - let followFound = UserService.Instance.myUserInfo.follows.findIndex( - f => f.community.id == community.id - ); - if (followFound) { - UserService.Instance.myUserInfo.follows[followFound].community = - community; - } - - let moderatesFound = UserService.Instance.myUserInfo.moderates.findIndex( - f => f.community.id == community.id - ); - if (moderatesFound) { - UserService.Instance.myUserInfo.moderates[moderatesFound].community = - community; - } - } + handleDiscussionLanguageChange(val: number[]) { + this.setState(s => ((s.form.discussion_languages = val), s)); } }