X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fhome%2Fsite-form.tsx;h=3b451e66ab10bd04493faea8b1ece5e4deda08e7;hb=2b1af707c3df6126b3e6890106c03c60ad49b1be;hp=40588ac8cf1ed29df848aabea87d83726de092dd;hpb=f61037f5d89f12818c8100f907a98b74e980112a;p=lemmy-ui.git diff --git a/src/shared/components/home/site-form.tsx b/src/shared/components/home/site-form.tsx index 40588ac..3b451e6 100644 --- a/src/shared/components/home/site-form.tsx +++ b/src/shared/components/home/site-form.tsx @@ -7,18 +7,12 @@ import { import { CreateSite, EditSite, - GetFederatedInstancesResponse, GetSiteResponse, + Instance, ListingType, } from "lemmy-js-client"; import { i18n } from "../../i18next"; -import { WebSocketService } from "../../services"; -import { - capitalizeFirstLetter, - fetchThemeList, - myAuth, - wsClient, -} from "../../utils"; +import { capitalizeFirstLetter, myAuthRequired } from "../../utils"; import { Icon, Spinner } from "../common/icon"; import { ImageUploadForm } from "../common/image-upload-form"; import { LanguageSelect } from "../common/language-select"; @@ -27,35 +21,73 @@ import { MarkdownTextArea } from "../common/markdown-textarea"; import NavigationPrompt from "../common/navigation-prompt"; interface SiteFormProps { - siteRes: GetSiteResponse; - instancesRes?: GetFederatedInstancesResponse; + blockedInstances?: Instance[]; + allowedInstances?: Instance[]; showLocal?: boolean; + themeList?: string[]; + onSaveSite(form: EditSite): void; + siteRes: GetSiteResponse; } interface SiteFormState { siteForm: EditSite; loading: boolean; - themeList?: string[]; instance_select: { allowed_instances: string; blocked_instances: string; }; + submitted: boolean; } type InstanceKey = "allowed_instances" | "blocked_instances"; export class SiteForm extends Component { state: SiteFormState = { - siteForm: { - auth: "TODO", - }, + siteForm: this.initSiteForm(), loading: false, instance_select: { allowed_instances: "", blocked_instances: "", }, + submitted: false, }; + initSiteForm(): EditSite { + const site = this.props.siteRes.site_view.site; + const ls = this.props.siteRes.site_view.local_site; + return { + name: site.name, + sidebar: site.sidebar, + description: site.description, + enable_downvotes: ls.enable_downvotes, + registration_mode: ls.registration_mode, + enable_nsfw: ls.enable_nsfw, + community_creation_admin_only: ls.community_creation_admin_only, + icon: site.icon, + banner: site.banner, + require_email_verification: ls.require_email_verification, + application_question: ls.application_question, + private_instance: ls.private_instance, + default_theme: ls.default_theme, + default_post_listing_type: ls.default_post_listing_type, + legal_information: ls.legal_information, + application_email_admins: ls.application_email_admins, + reports_email_admins: ls.reports_email_admins, + hide_modlog_mod_names: ls.hide_modlog_mod_names, + discussion_languages: this.props.siteRes.discussion_languages, + slur_filter_regex: ls.slur_filter_regex, + actor_name_max_length: ls.actor_name_max_length, + federation_enabled: ls.federation_enabled, + federation_debug: ls.federation_debug, + federation_worker_count: ls.federation_worker_count, + captcha_enabled: ls.captcha_enabled, + captcha_difficulty: ls.captcha_difficulty, + allowed_instances: this.props.allowedInstances?.map(i => i.domain), + blocked_instances: this.props.blockedInstances?.map(i => i.domain), + auth: "TODO", + }; + } + constructor(props: any, context: any) { super(props, context); @@ -75,53 +107,9 @@ export class SiteForm extends Component { this.handleDiscussionLanguageChange = this.handleDiscussionLanguageChange.bind(this); - - const site = this.props.siteRes.site_view.site; - const ls = this.props.siteRes.site_view.local_site; - this.state = { - ...this.state, - siteForm: { - name: site.name, - sidebar: site.sidebar, - description: site.description, - enable_downvotes: ls.enable_downvotes, - registration_mode: ls.registration_mode, - enable_nsfw: ls.enable_nsfw, - community_creation_admin_only: ls.community_creation_admin_only, - icon: site.icon, - banner: site.banner, - require_email_verification: ls.require_email_verification, - application_question: ls.application_question, - private_instance: ls.private_instance, - default_theme: ls.default_theme, - default_post_listing_type: ls.default_post_listing_type, - legal_information: ls.legal_information, - application_email_admins: ls.application_email_admins, - reports_email_admins: ls.reports_email_admins, - hide_modlog_mod_names: ls.hide_modlog_mod_names, - discussion_languages: this.props.siteRes.discussion_languages, - slur_filter_regex: ls.slur_filter_regex, - actor_name_max_length: ls.actor_name_max_length, - federation_enabled: ls.federation_enabled, - federation_debug: ls.federation_debug, - federation_worker_count: ls.federation_worker_count, - captcha_enabled: ls.captcha_enabled, - captcha_difficulty: ls.captcha_difficulty, - allowed_instances: - this.props.instancesRes?.federated_instances?.allowed.map( - i => i.domain - ), - blocked_instances: - this.props.instancesRes?.federated_instances?.blocked.map( - i => i.domain - ), - auth: "TODO", - }, - }; - } - - async componentDidMount() { - this.setState({ themeList: await fetchThemeList() }); + this.handleAddInstance = this.handleAddInstance.bind(this); + this.handleInstanceEnterPress = this.handleInstanceEnterPress.bind(this); + this.handleInstanceTextChange = this.handleInstanceTextChange.bind(this); } // Necessary to stop the loading @@ -129,29 +117,10 @@ export class SiteForm extends Component { this.setState({ loading: false }); } - componentDidUpdate() { - if ( - !this.state.loading && - !this.props.siteRes.site_view.local_site.site_setup && - (this.state.siteForm.name || - this.state.siteForm.sidebar || - this.state.siteForm.application_question || - this.state.siteForm.description) - ) { - window.onbeforeunload = () => true; - } else { - window.onbeforeunload = null; - } - } - - componentWillUnmount() { - window.onbeforeunload = null; - } - render() { const siteSetup = this.props.siteRes.site_view.local_site.site_setup; return ( - <> +
{ this.state.siteForm.sidebar || this.state.siteForm.application_question || this.state.siteForm.description - ) + ) && + !this.state.submitted } /> - -
{`${ - siteSetup - ? capitalizeFirstLetter(i18n.t("save")) - : capitalizeFirstLetter(i18n.t("name")) - } ${i18n.t("your_site")}`}
-
- -
- -
+
{`${ + siteSetup + ? capitalizeFirstLetter(i18n.t("save")) + : capitalizeFirstLetter(i18n.t("name")) + } ${i18n.t("your_site")}`}
+
+ +
+
-
- - +
+ + +
+
+ + +
+
+ +
+
-
- - +
+ +
+
-
- -
+
+
+ +
+ +
+
+
+
+
+
-
- -
- +
+
+
+ +
+
+
+
+ + +
+
+ {this.state.siteForm.registration_mode == "RequireApplication" && (
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
-
+ )} +
+
+
+ - +
-
- {this.state.siteForm.registration_mode == "RequireApplication" && ( -
- -
- -
-
- )} -
-
-
- - -
-
-
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
-
-
- - -
-
-
+
+
+
+
+ - -
-
- {this.props.showLocal && ( - - -
- -
- - )} -
-
-
- - -
-
-
-
- - -
+
+
+
+
+ +
-
+
+
+
-
+ +
+
+ {this.props.showLocal && ( +
+ +
+ +
+
+ )} +
+
+
+
- -
- -
+
+
+
+
+
-
-
-
- - -
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+
+
+
+ +
- {this.state.siteForm.federation_enabled && ( - <> -
- {this.federatedInstanceSelect("allowed_instances")} - {this.federatedInstanceSelect("blocked_instances")} -
-
-
-
- - -
-
-
-
- -
+
+ {this.state.siteForm.federation_enabled && ( + <> +
+ {this.federatedInstanceSelect("allowed_instances")} + {this.federatedInstanceSelect("blocked_instances")} +
+
+
+
+
- - )} -
-
-
- - -
-
- {this.state.siteForm.captcha_enabled && (
+
- - +
- )} + + )} +
+
+
+ + +
+
+
+ {this.state.siteForm.captcha_enabled && (
- + {i18n.t("captcha_difficulty")} + +
- - + )} +
+
+ +
+
+ ); } @@ -688,11 +648,15 @@ export class SiteForm extends Component { type="button" className="btn btn-sm bg-success ml-2" onClick={linkEvent(key, this.handleAddInstance)} + style={"width: 2rem; height: 2rem;"} tabIndex={ -1 /* Making this untabble because handling enter key in text input makes keyboard support for this button redundant */ } > - +
{selectedInstances && selectedInstances.length > 0 && ( @@ -708,13 +672,17 @@ export class SiteForm extends Component { ))} @@ -745,48 +713,69 @@ export class SiteForm extends Component { } } - handleCreateSiteSubmit(i: SiteForm, event: any) { + handleSaveSiteSubmit(i: SiteForm, event: any) { event.preventDefault(); - i.setState({ loading: true }); - const auth = myAuth() ?? "TODO"; + const auth = myAuthRequired(); i.setState(s => ((s.siteForm.auth = auth), s)); + i.setState({ loading: true, submitted: true }); + + const stateSiteForm = i.state.siteForm; + + let form: EditSite | CreateSite; + if (i.props.siteRes.site_view.local_site.site_setup) { - WebSocketService.Instance.send(wsClient.editSite(i.state.siteForm)); + form = stateSiteForm; } else { - const sForm = i.state.siteForm; - const form: CreateSite = { - name: sForm.name ?? "My site", - sidebar: sForm.sidebar, - description: sForm.description, - icon: sForm.icon, - banner: sForm.banner, - community_creation_admin_only: sForm.community_creation_admin_only, - enable_nsfw: sForm.enable_nsfw, - enable_downvotes: sForm.enable_downvotes, - application_question: sForm.application_question, - registration_mode: sForm.registration_mode, - require_email_verification: sForm.require_email_verification, - private_instance: sForm.private_instance, - default_theme: sForm.default_theme, - default_post_listing_type: sForm.default_post_listing_type, - application_email_admins: sForm.application_email_admins, - hide_modlog_mod_names: sForm.hide_modlog_mod_names, - legal_information: sForm.legal_information, - slur_filter_regex: sForm.slur_filter_regex, - actor_name_max_length: sForm.actor_name_max_length, - federation_enabled: sForm.federation_enabled, - federation_debug: sForm.federation_debug, - federation_worker_count: sForm.federation_worker_count, - captcha_enabled: sForm.captcha_enabled, - captcha_difficulty: sForm.captcha_difficulty, - allowed_instances: sForm.allowed_instances, - blocked_instances: sForm.blocked_instances, - discussion_languages: sForm.discussion_languages, + form = { + name: stateSiteForm.name ?? "My site", + sidebar: stateSiteForm.sidebar, + description: stateSiteForm.description, + icon: stateSiteForm.icon, + banner: stateSiteForm.banner, + community_creation_admin_only: + stateSiteForm.community_creation_admin_only, + enable_nsfw: stateSiteForm.enable_nsfw, + enable_downvotes: stateSiteForm.enable_downvotes, + application_question: stateSiteForm.application_question, + registration_mode: stateSiteForm.registration_mode, + require_email_verification: stateSiteForm.require_email_verification, + private_instance: stateSiteForm.private_instance, + default_theme: stateSiteForm.default_theme, + default_post_listing_type: stateSiteForm.default_post_listing_type, + application_email_admins: stateSiteForm.application_email_admins, + hide_modlog_mod_names: stateSiteForm.hide_modlog_mod_names, + legal_information: stateSiteForm.legal_information, + slur_filter_regex: stateSiteForm.slur_filter_regex, + actor_name_max_length: stateSiteForm.actor_name_max_length, + rate_limit_message: stateSiteForm.rate_limit_message, + rate_limit_message_per_second: + stateSiteForm.rate_limit_message_per_second, + rate_limit_comment: stateSiteForm.rate_limit_comment, + rate_limit_comment_per_second: + stateSiteForm.rate_limit_comment_per_second, + rate_limit_image: stateSiteForm.rate_limit_image, + rate_limit_image_per_second: stateSiteForm.rate_limit_image_per_second, + rate_limit_post: stateSiteForm.rate_limit_post, + rate_limit_post_per_second: stateSiteForm.rate_limit_post_per_second, + rate_limit_register: stateSiteForm.rate_limit_register, + rate_limit_register_per_second: + stateSiteForm.rate_limit_register_per_second, + rate_limit_search: stateSiteForm.rate_limit_search, + rate_limit_search_per_second: + stateSiteForm.rate_limit_search_per_second, + federation_enabled: stateSiteForm.federation_enabled, + federation_debug: stateSiteForm.federation_debug, + federation_worker_count: stateSiteForm.federation_worker_count, + captcha_enabled: stateSiteForm.captcha_enabled, + captcha_difficulty: stateSiteForm.captcha_difficulty, + allowed_instances: stateSiteForm.allowed_instances, + blocked_instances: stateSiteForm.blocked_instances, + discussion_languages: stateSiteForm.discussion_languages, auth, }; - WebSocketService.Instance.send(wsClient.createSite(form)); } - i.setState(i.state); + + i.props.onSaveSite(form); } handleAddInstance(key: InstanceKey) {