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}
215 initialLanguageId={None}
219 onContentChange={this.handleSiteSidebarChange}
220 hideNavigationWarnings
225 <div className="form-group row">
226 <label className="col-12 col-form-label">
227 {i18n.t("legal_information")}
229 <div className="col-12">
231 initialContent={this.state.siteForm.legal_information}
232 initialLanguageId={None}
236 onContentChange={this.handleSiteLegalInfoChange}
237 hideNavigationWarnings
242 {this.state.siteForm.require_application.unwrapOr(false) && (
243 <div className="form-group row">
244 <label className="col-12 col-form-label">
245 {i18n.t("application_questionnaire")}
247 <div className="col-12">
249 initialContent={this.state.siteForm.application_question}
250 initialLanguageId={None}
254 onContentChange={this.handleSiteApplicationQuestionChange}
255 hideNavigationWarnings
261 <div className="form-group row">
262 <div className="col-12">
263 <div className="form-check">
265 className="form-check-input"
266 id="create-site-downvotes"
268 checked={toUndefined(this.state.siteForm.enable_downvotes)}
271 this.handleSiteEnableDownvotesChange
275 className="form-check-label"
276 htmlFor="create-site-downvotes"
278 {i18n.t("enable_downvotes")}
283 <div className="form-group row">
284 <div className="col-12">
285 <div className="form-check">
287 className="form-check-input"
288 id="create-site-enable-nsfw"
290 checked={toUndefined(this.state.siteForm.enable_nsfw)}
291 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
294 className="form-check-label"
295 htmlFor="create-site-enable-nsfw"
297 {i18n.t("enable_nsfw")}
302 <div className="form-group row">
303 <div className="col-12">
304 <div className="form-check">
306 className="form-check-input"
307 id="create-site-open-registration"
309 checked={toUndefined(this.state.siteForm.open_registration)}
312 this.handleSiteOpenRegistrationChange
316 className="form-check-label"
317 htmlFor="create-site-open-registration"
319 {i18n.t("open_registration")}
324 <div className="form-group row">
325 <div className="col-12">
326 <div className="form-check">
328 className="form-check-input"
329 id="create-site-community-creation-admin-only"
331 checked={toUndefined(
332 this.state.siteForm.community_creation_admin_only
336 this.handleSiteCommunityCreationAdminOnly
340 className="form-check-label"
341 htmlFor="create-site-community-creation-admin-only"
343 {i18n.t("community_creation_admin_only")}
348 <div className="form-group row">
349 <div className="col-12">
350 <div className="form-check">
352 className="form-check-input"
353 id="create-site-require-email-verification"
355 checked={toUndefined(
356 this.state.siteForm.require_email_verification
360 this.handleSiteRequireEmailVerification
364 className="form-check-label"
365 htmlFor="create-site-require-email-verification"
367 {i18n.t("require_email_verification")}
372 <div className="form-group row">
373 <div className="col-12">
374 <div className="form-check">
376 className="form-check-input"
377 id="create-site-require-application"
379 checked={toUndefined(this.state.siteForm.require_application)}
380 onChange={linkEvent(this, this.handleSiteRequireApplication)}
383 className="form-check-label"
384 htmlFor="create-site-require-application"
386 {i18n.t("require_registration_application")}
391 <div className="form-group row">
392 <div className="col-12">
394 className="form-check-label mr-2"
395 htmlFor="create-site-default-theme"
400 id="create-site-default-theme"
401 value={toUndefined(this.state.siteForm.default_theme)}
402 onChange={linkEvent(this, this.handleSiteDefaultTheme)}
403 className="custom-select w-auto"
405 <option value="browser">{i18n.t("browser_default")}</option>
406 {this.state.themeList.unwrapOr([]).map(theme => (
407 <option key={theme} value={theme}>
414 {this.props.showLocal && (
415 <form className="form-group row">
416 <label className="col-sm-3">{i18n.t("listing_type")}</label>
417 <div className="col-sm-9">
421 this.state.siteForm.default_post_listing_type.unwrapOr(
427 showSubscribed={false}
428 onChange={this.handleDefaultPostListingTypeChange}
433 <div className="form-group row">
434 <div className="col-12">
435 <div className="form-check">
437 className="form-check-input"
438 id="create-site-private-instance"
440 checked={toUndefined(this.state.siteForm.private_instance)}
441 onChange={linkEvent(this, this.handleSitePrivateInstance)}
444 className="form-check-label"
445 htmlFor="create-site-private-instance"
447 {i18n.t("private_instance")}
452 <div className="form-group row">
453 <div className="col-12">
454 <div className="form-check">
456 className="form-check-input"
457 id="create-site-hide-modlog-mod-names"
459 checked={toUndefined(
460 this.state.siteForm.hide_modlog_mod_names
462 onChange={linkEvent(this, this.handleSiteHideModlogModNames)}
465 className="form-check-label"
466 htmlFor="create-site-hide-modlog-mod-names"
468 {i18n.t("hide_modlog_mod_names")}
473 <div className="form-group row">
474 <div className="col-12">
477 className="btn btn-secondary mr-2"
478 disabled={this.state.loading}
480 {this.state.loading ? (
482 ) : this.props.site.isSome() ? (
483 capitalizeFirstLetter(i18n.t("save"))
485 capitalizeFirstLetter(i18n.t("create"))
488 {this.props.site.isSome() && (
491 className="btn btn-secondary"
492 onClick={linkEvent(this, this.handleCancel)}
504 handleCreateSiteSubmit(i: SiteForm, event: any) {
505 event.preventDefault();
506 i.setState({ loading: true });
507 i.setState(s => ((s.siteForm.auth = auth().unwrap()), s));
509 if (i.props.site.isSome()) {
510 WebSocketService.Instance.send(wsClient.editSite(i.state.siteForm));
513 let sForm = i.state.siteForm;
514 let form = new CreateSite({
515 name: sForm.name.unwrapOr("My site"),
516 sidebar: sForm.sidebar,
517 description: sForm.description,
519 banner: sForm.banner,
520 community_creation_admin_only: sForm.community_creation_admin_only,
521 enable_nsfw: sForm.enable_nsfw,
522 enable_downvotes: sForm.enable_downvotes,
523 require_application: sForm.require_application,
524 application_question: sForm.application_question,
525 open_registration: sForm.open_registration,
526 require_email_verification: sForm.require_email_verification,
527 private_instance: sForm.private_instance,
528 default_theme: sForm.default_theme,
529 default_post_listing_type: sForm.default_post_listing_type,
530 auth: auth().unwrap(),
531 hide_modlog_mod_names: sForm.hide_modlog_mod_names,
533 WebSocketService.Instance.send(wsClient.createSite(form));
538 handleSiteNameChange(i: SiteForm, event: any) {
539 i.state.siteForm.name = Some(event.target.value);
543 handleSiteSidebarChange(val: string) {
544 this.setState(s => ((s.siteForm.sidebar = Some(val)), s));
547 handleSiteLegalInfoChange(val: string) {
548 this.setState(s => ((s.siteForm.legal_information = Some(val)), s));
551 handleSiteApplicationQuestionChange(val: string) {
552 this.setState(s => ((s.siteForm.application_question = Some(val)), s));
555 handleSiteDescChange(i: SiteForm, event: any) {
556 i.state.siteForm.description = Some(event.target.value);
560 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
561 i.state.siteForm.enable_nsfw = Some(event.target.checked);
565 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
566 i.state.siteForm.open_registration = Some(event.target.checked);
570 handleSiteCommunityCreationAdminOnly(i: SiteForm, event: any) {
571 i.state.siteForm.community_creation_admin_only = Some(event.target.checked);
575 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
576 i.state.siteForm.enable_downvotes = Some(event.target.checked);
580 handleSiteRequireApplication(i: SiteForm, event: any) {
581 i.state.siteForm.require_application = Some(event.target.checked);
585 handleSiteRequireEmailVerification(i: SiteForm, event: any) {
586 i.state.siteForm.require_email_verification = Some(event.target.checked);
590 handleSitePrivateInstance(i: SiteForm, event: any) {
591 i.state.siteForm.private_instance = Some(event.target.checked);
595 handleSiteHideModlogModNames(i: SiteForm, event: any) {
596 i.state.siteForm.hide_modlog_mod_names = Some(event.target.checked);
600 handleSiteDefaultTheme(i: SiteForm, event: any) {
601 i.state.siteForm.default_theme = Some(event.target.value);
605 handleCancel(i: SiteForm) {
609 handleIconUpload(url: string) {
610 this.setState(s => ((s.siteForm.icon = Some(url)), s));
614 this.setState(s => ((s.siteForm.icon = Some("")), s));
617 handleBannerUpload(url: string) {
618 this.setState(s => ((s.siteForm.banner = Some(url)), s));
621 handleBannerRemove() {
622 this.setState(s => ((s.siteForm.banner = Some("")), s));
625 handleDefaultPostListingTypeChange(val: ListingType) {
628 (s.siteForm.default_post_listing_type = Some(
629 ListingType[ListingType[val]]