2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Prompt } from "inferno-router";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { CreateSite, EditSite, Site } from "lemmy-js-client";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { WebSocketService } from "../../services";
|
2021-12-30 15:26:45 +00:00
|
|
|
import { authField, capitalizeFirstLetter, wsClient } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { ImageUploadForm } from "../common/image-upload-form";
|
|
|
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface SiteFormProps {
|
|
|
|
site?: Site; // If a site is given, that means this is an edit
|
|
|
|
onCancel?(): any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SiteFormState {
|
2020-12-24 01:58:27 +00:00
|
|
|
siteForm: EditSite;
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SiteForm extends Component<SiteFormProps, SiteFormState> {
|
|
|
|
private emptyState: SiteFormState = {
|
|
|
|
siteForm: {
|
|
|
|
enable_downvotes: true,
|
|
|
|
open_registration: true,
|
|
|
|
enable_nsfw: true,
|
|
|
|
name: null,
|
|
|
|
icon: null,
|
|
|
|
banner: null,
|
2021-12-30 15:26:45 +00:00
|
|
|
require_email_verification: null,
|
|
|
|
require_application: null,
|
|
|
|
application_question: null,
|
|
|
|
private_instance: null,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2021-04-23 22:26:36 +00:00
|
|
|
this.handleSiteSidebarChange = this.handleSiteSidebarChange.bind(this);
|
2021-12-30 15:26:45 +00:00
|
|
|
this.handleSiteApplicationQuestionChange =
|
|
|
|
this.handleSiteApplicationQuestionChange.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
this.handleIconUpload = this.handleIconUpload.bind(this);
|
|
|
|
this.handleIconRemove = this.handleIconRemove.bind(this);
|
|
|
|
|
|
|
|
this.handleBannerUpload = this.handleBannerUpload.bind(this);
|
|
|
|
this.handleBannerRemove = this.handleBannerRemove.bind(this);
|
|
|
|
|
|
|
|
if (this.props.site) {
|
2021-12-30 15:26:45 +00:00
|
|
|
let site = this.props.site;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.siteForm = {
|
2021-12-30 15:26:45 +00:00
|
|
|
name: site.name,
|
|
|
|
sidebar: site.sidebar,
|
|
|
|
description: site.description,
|
|
|
|
enable_downvotes: site.enable_downvotes,
|
|
|
|
open_registration: site.open_registration,
|
|
|
|
enable_nsfw: site.enable_nsfw,
|
|
|
|
community_creation_admin_only: site.community_creation_admin_only,
|
|
|
|
icon: site.icon,
|
|
|
|
banner: site.banner,
|
|
|
|
require_email_verification: site.require_email_verification,
|
|
|
|
require_application: site.require_application,
|
|
|
|
application_question: site.application_question,
|
|
|
|
private_instance: site.private_instance,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Necessary to stop the loading
|
|
|
|
componentWillReceiveProps() {
|
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if (
|
|
|
|
!this.state.loading &&
|
|
|
|
!this.props.site &&
|
2021-04-23 22:26:36 +00:00
|
|
|
(this.state.siteForm.name ||
|
|
|
|
this.state.siteForm.sidebar ||
|
2021-12-30 15:26:45 +00:00
|
|
|
this.state.siteForm.application_question ||
|
2021-04-23 22:26:36 +00:00
|
|
|
this.state.siteForm.description)
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
|
|
|
window.onbeforeunload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Prompt
|
|
|
|
when={
|
|
|
|
!this.state.loading &&
|
|
|
|
!this.props.site &&
|
2021-04-23 22:26:36 +00:00
|
|
|
(this.state.siteForm.name ||
|
|
|
|
this.state.siteForm.sidebar ||
|
2021-12-30 15:26:45 +00:00
|
|
|
this.state.siteForm.application_question ||
|
2021-04-23 22:26:36 +00:00
|
|
|
this.state.siteForm.description)
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
message={i18n.t("block_leaving")}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
|
|
|
|
<h5>{`${
|
|
|
|
this.props.site
|
2021-02-22 02:39:04 +00:00
|
|
|
? capitalizeFirstLetter(i18n.t("save"))
|
|
|
|
: capitalizeFirstLetter(i18n.t("name"))
|
|
|
|
} ${i18n.t("your_site")}`}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label" htmlFor="create-site-name">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("name")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="create-site-name"
|
|
|
|
class="form-control"
|
|
|
|
value={this.state.siteForm.name}
|
|
|
|
onInput={linkEvent(this, this.handleSiteNameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
maxLength={20}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group">
|
2021-02-22 02:39:04 +00:00
|
|
|
<label>{i18n.t("icon")}</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ImageUploadForm
|
2021-02-22 02:39:04 +00:00
|
|
|
uploadTitle={i18n.t("upload_icon")}
|
2020-09-06 16:15:25 +00:00
|
|
|
imageSrc={this.state.siteForm.icon}
|
|
|
|
onUpload={this.handleIconUpload}
|
|
|
|
onRemove={this.handleIconRemove}
|
|
|
|
rounded
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="form-group">
|
2021-02-22 02:39:04 +00:00
|
|
|
<label>{i18n.t("banner")}</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ImageUploadForm
|
2021-02-22 02:39:04 +00:00
|
|
|
uploadTitle={i18n.t("upload_banner")}
|
2020-09-06 16:15:25 +00:00
|
|
|
imageSrc={this.state.siteForm.banner}
|
|
|
|
onUpload={this.handleBannerUpload}
|
|
|
|
onRemove={this.handleBannerRemove}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-04-23 22:26:36 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label" htmlFor="site-desc">
|
|
|
|
{i18n.t("description")}
|
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
class="form-control"
|
|
|
|
id="site-desc"
|
|
|
|
value={this.state.siteForm.description}
|
|
|
|
onInput={linkEvent(this, this.handleSiteDescChange)}
|
|
|
|
maxLength={150}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="form-group row">
|
2021-12-30 15:26:45 +00:00
|
|
|
<label class="col-12 col-form-label">{i18n.t("sidebar")}</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="col-12">
|
|
|
|
<MarkdownTextArea
|
2021-04-23 22:26:36 +00:00
|
|
|
initialContent={this.state.siteForm.sidebar}
|
|
|
|
onContentChange={this.handleSiteSidebarChange}
|
2020-09-06 16:15:25 +00:00
|
|
|
hideNavigationWarnings
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-12-30 15:26:45 +00:00
|
|
|
{this.state.siteForm.require_application && (
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label">
|
|
|
|
{i18n.t("application_questionnaire")}
|
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.siteForm.application_question}
|
|
|
|
onContentChange={this.handleSiteApplicationQuestionChange}
|
|
|
|
hideNavigationWarnings
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-downvotes"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.enable_downvotes}
|
|
|
|
onChange={linkEvent(
|
|
|
|
this,
|
|
|
|
this.handleSiteEnableDownvotesChange
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<label class="form-check-label" htmlFor="create-site-downvotes">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("enable_downvotes")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-enable-nsfw"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.enable_nsfw}
|
|
|
|
onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-enable-nsfw"
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("enable_nsfw")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-open-registration"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.open_registration}
|
|
|
|
onChange={linkEvent(
|
|
|
|
this,
|
|
|
|
this.handleSiteOpenRegistrationChange
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-open-registration"
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("open_registration")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-24 03:55:51 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-community-creation-admin-only"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.community_creation_admin_only}
|
|
|
|
onChange={linkEvent(
|
|
|
|
this,
|
|
|
|
this.handleSiteCommunityCreationAdminOnly
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-community-creation-admin-only"
|
|
|
|
>
|
|
|
|
{i18n.t("community_creation_admin_only")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-12-30 15:26:45 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-require-email-verification"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.require_email_verification}
|
|
|
|
onChange={linkEvent(
|
|
|
|
this,
|
|
|
|
this.handleSiteRequireEmailVerification
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-require-email-verification"
|
|
|
|
>
|
|
|
|
{i18n.t("require_email_verification")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-require-application"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.require_application}
|
|
|
|
onChange={linkEvent(this, this.handleSiteRequireApplication)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-require-application"
|
|
|
|
>
|
|
|
|
{i18n.t("require_registration_application")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="create-site-private-instance"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.private_instance}
|
|
|
|
onChange={linkEvent(this, this.handleSitePrivateInstance)}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
class="form-check-label"
|
|
|
|
htmlFor="create-site-private-instance"
|
|
|
|
>
|
|
|
|
{i18n.t("private_instance")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
class="btn btn-secondary mr-2"
|
|
|
|
disabled={this.state.loading}
|
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : this.props.site ? (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("create"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
{this.props.site && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateSiteSubmit(i: SiteForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
|
|
|
if (i.props.site) {
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.editSite(i.state.siteForm));
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2021-04-23 21:48:31 +00:00
|
|
|
let form: CreateSite = {
|
|
|
|
name: i.state.siteForm.name || "My site",
|
|
|
|
...i.state.siteForm,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.createSite(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteNameChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.name = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-04-23 22:26:36 +00:00
|
|
|
handleSiteSidebarChange(val: string) {
|
|
|
|
this.state.siteForm.sidebar = val;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2021-12-30 15:26:45 +00:00
|
|
|
handleSiteApplicationQuestionChange(val: string) {
|
|
|
|
this.state.siteForm.application_question = val;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2021-04-23 22:26:36 +00:00
|
|
|
handleSiteDescChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.description = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleSiteEnableNsfwChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.enable_nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.open_registration = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-04-24 03:55:51 +00:00
|
|
|
handleSiteCommunityCreationAdminOnly(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.community_creation_admin_only = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.enable_downvotes = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-12-30 15:26:45 +00:00
|
|
|
handleSiteRequireApplication(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.require_application = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteRequireEmailVerification(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.require_email_verification = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSitePrivateInstance(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.private_instance = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleCancel(i: SiteForm) {
|
|
|
|
i.props.onCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIconUpload(url: string) {
|
|
|
|
this.state.siteForm.icon = url;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIconRemove() {
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.siteForm.icon = "";
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBannerUpload(url: string) {
|
|
|
|
this.state.siteForm.banner = url;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBannerRemove() {
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.siteForm.banner = "";
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|