1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
3 import { Prompt } from "inferno-router";
10 } from "lemmy-js-client";
11 import { i18n } from "../../i18next";
12 import { WebSocketService } from "../../services";
15 capitalizeFirstLetter,
19 import { Spinner } from "../common/icon";
20 import { ImageUploadForm } from "../common/image-upload-form";
21 import { ListingTypeSelect } from "../common/listing-type-select";
22 import { MarkdownTextArea } from "../common/markdown-textarea";
24 interface SiteFormProps {
25 site: Option<Site>; // If a site is given, that means this is an edit
31 interface SiteFormState {
34 themeList: Option<string[]>;
37 export class SiteForm extends Component<SiteFormProps, SiteFormState> {
38 private emptyState: SiteFormState = {
39 siteForm: new EditSite({
40 enable_downvotes: Some(true),
41 open_registration: Some(true),
42 enable_nsfw: Some(true),
46 require_email_verification: None,
47 require_application: None,
48 application_question: None,
49 private_instance: None,
52 default_post_listing_type: None,
53 legal_information: None,
55 community_creation_admin_only: None,
57 hide_modlog_mod_names: Some(true),
63 constructor(props: any, context: any) {
64 super(props, context);
66 this.state = this.emptyState;
67 this.handleSiteSidebarChange = this.handleSiteSidebarChange.bind(this);
68 this.handleSiteLegalInfoChange = this.handleSiteLegalInfoChange.bind(this);
69 this.handleSiteApplicationQuestionChange =
70 this.handleSiteApplicationQuestionChange.bind(this);
72 this.handleIconUpload = this.handleIconUpload.bind(this);
73 this.handleIconRemove = this.handleIconRemove.bind(this);
75 this.handleBannerUpload = this.handleBannerUpload.bind(this);
76 this.handleBannerRemove = this.handleBannerRemove.bind(this);
78 this.handleDefaultPostListingTypeChange =
79 this.handleDefaultPostListingTypeChange.bind(this);
81 if (this.props.site.isSome()) {
82 let site = this.props.site.unwrap();
85 siteForm: new EditSite({
86 name: Some(site.name),
87 sidebar: site.sidebar,
88 description: site.description,
89 enable_downvotes: Some(site.enable_downvotes),
90 open_registration: Some(site.open_registration),
91 enable_nsfw: Some(site.enable_nsfw),
92 community_creation_admin_only: Some(
93 site.community_creation_admin_only
97 require_email_verification: Some(site.require_email_verification),
98 require_application: Some(site.require_application),
99 application_question: site.application_question,
100 private_instance: Some(site.private_instance),
101 default_theme: Some(site.default_theme),
102 default_post_listing_type: Some(site.default_post_listing_type),
103 legal_information: site.legal_information,
104 hide_modlog_mod_names: site.hide_modlog_mod_names,
111 async componentDidMount() {
112 this.setState({ themeList: Some(await fetchThemeList()) });
115 // Necessary to stop the loading
116 componentWillReceiveProps() {
117 this.setState({ loading: false });
120 componentDidUpdate() {
122 !this.state.loading &&
123 this.props.site.isNone() &&
124 (this.state.siteForm.name ||
125 this.state.siteForm.sidebar ||
126 this.state.siteForm.application_question ||
127 this.state.siteForm.description)
129 window.onbeforeunload = () => true;
131 window.onbeforeunload = undefined;
135 componentWillUnmount() {
136 window.onbeforeunload = null;
144 !this.state.loading &&
145 this.props.site.isNone() &&
146 (this.state.siteForm.name ||
147 this.state.siteForm.sidebar ||
148 this.state.siteForm.application_question ||
149 this.state.siteForm.description)
151 message={i18n.t("block_leaving")}
153 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
155 this.props.site.isSome()
156 ? capitalizeFirstLetter(i18n.t("save"))
157 : capitalizeFirstLetter(i18n.t("name"))
158 } ${i18n.t("your_site")}`}</h5>
159 <div className="form-group row">
160 <label className="col-12 col-form-label" htmlFor="create-site-name">
163 <div className="col-12">
166 id="create-site-name"
167 className="form-control"
168 value={toUndefined(this.state.siteForm.name)}
169 onInput={linkEvent(this, this.handleSiteNameChange)}
176 <div className="form-group">
177 <label>{i18n.t("icon")}</label>
179 uploadTitle={i18n.t("upload_icon")}
180 imageSrc={this.state.siteForm.icon}
181 onUpload={this.handleIconUpload}
182 onRemove={this.handleIconRemove}
186 <div className="form-group">
187 <label>{i18n.t("banner")}</label>
189 uploadTitle={i18n.t("upload_banner")}
190 imageSrc={this.state.siteForm.banner}
191 onUpload={this.handleBannerUpload}
192 onRemove={this.handleBannerRemove}
195 <div className="form-group row">
196 <label className="col-12 col-form-label" htmlFor="site-desc">
197 {i18n.t("description")}
199 <div className="col-12">
202 className="form-control"
204 value={toUndefined(this.state.siteForm.description)}
205 onInput={linkEvent(this, this.handleSiteDescChange)}
210 <div className="form-group row">
211 <label className="col-12 col-form-label">{i18n.t("sidebar")}</label>
212 <div className="col-12">
214 initialContent={this.state.siteForm.sidebar}
218 onContentChange={this.handleSiteSidebarChange}
219 hideNavigationWarnings
223 <div className="form-group row">
224 <label className="col-12 col-form-label">
225 {i18n.t("legal_information")}
227 <div className="col-12">
229 initialContent={this.state.siteForm.legal_information}
233 onContentChange={this.handleSiteLegalInfoChange}
234 hideNavigationWarnings
238 {this.state.siteForm.require_application.unwrapOr(false) && (
239 <div className="form-group row">
240 <label className="col-12 col-form-label">
241 {i18n.t("application_questionnaire")}
243 <div className="col-12">
245 initialContent={this.state.siteForm.application_question}
249 onContentChange={this.handleSiteApplicationQuestionChange}
250 hideNavigationWarnings
255 <div className="form-group row">
256 <div className="col-12">
257 <div className="form-check">
259 className="form-check-input"
260 id="create-site-downvotes"
262 checked={toUndefined(this.state.siteForm.enable_downvotes)}
265 this.handleSiteEnableDownvotesChange
269 className="form-check-label"
270 htmlFor="create-site-downvotes"
272 {i18n.t("enable_downvotes")}
277 <div className="form-group row">
278 <div className="col-12">
279 <div className="form-check">
281 className="form-check-input"
282 id="create-site-enable-nsfw"
284 checked={toUndefined(this.state.siteForm.enable_nsfw)}
285 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
288 className="form-check-label"
289 htmlFor="create-site-enable-nsfw"
291 {i18n.t("enable_nsfw")}
296 <div className="form-group row">
297 <div className="col-12">
298 <div className="form-check">
300 className="form-check-input"
301 id="create-site-open-registration"
303 checked={toUndefined(this.state.siteForm.open_registration)}
306 this.handleSiteOpenRegistrationChange
310 className="form-check-label"
311 htmlFor="create-site-open-registration"
313 {i18n.t("open_registration")}
318 <div className="form-group row">
319 <div className="col-12">
320 <div className="form-check">
322 className="form-check-input"
323 id="create-site-community-creation-admin-only"
325 checked={toUndefined(
326 this.state.siteForm.community_creation_admin_only
330 this.handleSiteCommunityCreationAdminOnly
334 className="form-check-label"
335 htmlFor="create-site-community-creation-admin-only"
337 {i18n.t("community_creation_admin_only")}
342 <div className="form-group row">
343 <div className="col-12">
344 <div className="form-check">
346 className="form-check-input"
347 id="create-site-require-email-verification"
349 checked={toUndefined(
350 this.state.siteForm.require_email_verification
354 this.handleSiteRequireEmailVerification
358 className="form-check-label"
359 htmlFor="create-site-require-email-verification"
361 {i18n.t("require_email_verification")}
366 <div className="form-group row">
367 <div className="col-12">
368 <div className="form-check">
370 className="form-check-input"
371 id="create-site-require-application"
373 checked={toUndefined(this.state.siteForm.require_application)}
374 onChange={linkEvent(this, this.handleSiteRequireApplication)}
377 className="form-check-label"
378 htmlFor="create-site-require-application"
380 {i18n.t("require_registration_application")}
385 <div className="form-group row">
386 <div className="col-12">
388 className="form-check-label mr-2"
389 htmlFor="create-site-default-theme"
394 id="create-site-default-theme"
395 value={toUndefined(this.state.siteForm.default_theme)}
396 onChange={linkEvent(this, this.handleSiteDefaultTheme)}
397 className="custom-select w-auto"
399 <option value="browser">{i18n.t("browser_default")}</option>
400 {this.state.themeList.unwrapOr([]).map(theme => (
401 <option key={theme} value={theme}>
408 {this.props.showLocal && (
409 <form className="form-group row">
410 <label className="col-sm-3">{i18n.t("listing_type")}</label>
411 <div className="col-sm-9">
415 this.state.siteForm.default_post_listing_type.unwrapOr(
421 showSubscribed={false}
422 onChange={this.handleDefaultPostListingTypeChange}
427 <div className="form-group row">
428 <div className="col-12">
429 <div className="form-check">
431 className="form-check-input"
432 id="create-site-private-instance"
434 checked={toUndefined(this.state.siteForm.private_instance)}
435 onChange={linkEvent(this, this.handleSitePrivateInstance)}
438 className="form-check-label"
439 htmlFor="create-site-private-instance"
441 {i18n.t("private_instance")}
446 <div className="form-group row">
447 <div className="col-12">
448 <div className="form-check">
450 className="form-check-input"
451 id="create-site-hide-modlog-mod-names"
453 checked={toUndefined(
454 this.state.siteForm.hide_modlog_mod_names
456 onChange={linkEvent(this, this.handleSiteHideModlogModNames)}
459 className="form-check-label"
460 htmlFor="create-site-hide-modlog-mod-names"
462 {i18n.t("hide_modlog_mod_names")}
467 <div className="form-group row">
468 <div className="col-12">
471 className="btn btn-secondary mr-2"
472 disabled={this.state.loading}
474 {this.state.loading ? (
476 ) : this.props.site.isSome() ? (
477 capitalizeFirstLetter(i18n.t("save"))
479 capitalizeFirstLetter(i18n.t("create"))
482 {this.props.site.isSome() && (
485 className="btn btn-secondary"
486 onClick={linkEvent(this, this.handleCancel)}
498 handleCreateSiteSubmit(i: SiteForm, event: any) {
499 event.preventDefault();
500 i.setState({ loading: true });
501 i.setState(s => ((s.siteForm.auth = auth().unwrap()), s));
503 if (i.props.site.isSome()) {
504 WebSocketService.Instance.send(wsClient.editSite(i.state.siteForm));
507 let sForm = i.state.siteForm;
508 let form = new CreateSite({
509 name: sForm.name.unwrapOr("My site"),
510 sidebar: sForm.sidebar,
511 description: sForm.description,
513 banner: sForm.banner,
514 community_creation_admin_only: sForm.community_creation_admin_only,
515 enable_nsfw: sForm.enable_nsfw,
516 enable_downvotes: sForm.enable_downvotes,
517 require_application: sForm.require_application,
518 application_question: sForm.application_question,
519 open_registration: sForm.open_registration,
520 require_email_verification: sForm.require_email_verification,
521 private_instance: sForm.private_instance,
522 default_theme: sForm.default_theme,
523 default_post_listing_type: sForm.default_post_listing_type,
524 auth: auth().unwrap(),
525 hide_modlog_mod_names: sForm.hide_modlog_mod_names,
527 WebSocketService.Instance.send(wsClient.createSite(form));
532 handleSiteNameChange(i: SiteForm, event: any) {
533 i.state.siteForm.name = Some(event.target.value);
537 handleSiteSidebarChange(val: string) {
538 this.setState(s => ((s.siteForm.sidebar = Some(val)), s));
541 handleSiteLegalInfoChange(val: string) {
542 this.setState(s => ((s.siteForm.legal_information = Some(val)), s));
545 handleSiteApplicationQuestionChange(val: string) {
546 this.setState(s => ((s.siteForm.application_question = Some(val)), s));
549 handleSiteDescChange(i: SiteForm, event: any) {
550 i.state.siteForm.description = Some(event.target.value);
554 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
555 i.state.siteForm.enable_nsfw = Some(event.target.checked);
559 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
560 i.state.siteForm.open_registration = Some(event.target.checked);
564 handleSiteCommunityCreationAdminOnly(i: SiteForm, event: any) {
565 i.state.siteForm.community_creation_admin_only = Some(event.target.checked);
569 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
570 i.state.siteForm.enable_downvotes = Some(event.target.checked);
574 handleSiteRequireApplication(i: SiteForm, event: any) {
575 i.state.siteForm.require_application = Some(event.target.checked);
579 handleSiteRequireEmailVerification(i: SiteForm, event: any) {
580 i.state.siteForm.require_email_verification = Some(event.target.checked);
584 handleSitePrivateInstance(i: SiteForm, event: any) {
585 i.state.siteForm.private_instance = Some(event.target.checked);
589 handleSiteHideModlogModNames(i: SiteForm, event: any) {
590 i.state.siteForm.hide_modlog_mod_names = Some(event.target.checked);
594 handleSiteDefaultTheme(i: SiteForm, event: any) {
595 i.state.siteForm.default_theme = Some(event.target.value);
599 handleCancel(i: SiteForm) {
603 handleIconUpload(url: string) {
604 this.setState(s => ((s.siteForm.icon = Some(url)), s));
608 this.setState(s => ((s.siteForm.icon = Some("")), s));
611 handleBannerUpload(url: string) {
612 this.setState(s => ((s.siteForm.banner = Some(url)), s));
615 handleBannerRemove() {
616 this.setState(s => ((s.siteForm.banner = Some("")), s));
619 handleDefaultPostListingTypeChange(val: ListingType) {
622 (s.siteForm.default_post_listing_type = Some(
623 ListingType[ListingType[val]]