1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { MarkdownTextArea } from './markdown-textarea';
4 import { Site, SiteForm as SiteFormI } from '../interfaces';
5 import { WebSocketService } from '../services';
6 import { capitalizeFirstLetter, randomStr } from '../utils';
7 import { i18n } from '../i18next';
9 interface SiteFormProps {
10 site?: Site; // If a site is given, that means this is an edit
14 interface SiteFormState {
19 export class SiteForm extends Component<SiteFormProps, SiteFormState> {
20 private id = `site-form-${randomStr()}`;
21 private emptyState: SiteFormState = {
23 enable_downvotes: true,
24 open_registration: true,
31 constructor(props: any, context: any) {
32 super(props, context);
34 this.state = this.emptyState;
35 this.handleSiteDescriptionChange = this.handleSiteDescriptionChange.bind(
39 if (this.props.site) {
40 this.state.siteForm = {
41 name: this.props.site.name,
42 description: this.props.site.description,
43 enable_downvotes: this.props.site.enable_downvotes,
44 open_registration: this.props.site.open_registration,
45 enable_nsfw: this.props.site.enable_nsfw,
50 // Necessary to stop the loading
51 componentWillReceiveProps() {
52 this.state.loading = false;
53 this.setState(this.state);
56 componentDidUpdate() {
58 !this.state.loading &&
60 (this.state.siteForm.name || this.state.siteForm.description)
62 window.onbeforeunload = () => true;
64 window.onbeforeunload = undefined;
68 componentWillUnmount() {
69 window.onbeforeunload = null;
77 !this.state.loading &&
79 (this.state.siteForm.name || this.state.siteForm.description)
81 message={i18n.t('block_leaving')}
83 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
86 ? capitalizeFirstLetter(i18n.t('save'))
87 : capitalizeFirstLetter(i18n.t('name'))
88 } ${i18n.t('your_site')}`}</h5>
89 <div class="form-group row">
90 <label class="col-12 col-form-label" htmlFor="create-site-name">
98 value={this.state.siteForm.name}
99 onInput={linkEvent(this, this.handleSiteNameChange)}
106 <div class="form-group row">
107 <label class="col-12 col-form-label" htmlFor={this.id}>
112 initialContent={this.state.siteForm.description}
113 onContentChange={this.handleSiteDescriptionChange}
117 <div class="form-group row">
119 <div class="form-check">
121 class="form-check-input"
122 id="create-site-downvotes"
124 checked={this.state.siteForm.enable_downvotes}
127 this.handleSiteEnableDownvotesChange
130 <label class="form-check-label" htmlFor="create-site-downvotes">
131 {i18n.t('enable_downvotes')}
136 <div class="form-group row">
138 <div class="form-check">
140 class="form-check-input"
141 id="create-site-enable-nsfw"
143 checked={this.state.siteForm.enable_nsfw}
144 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
147 class="form-check-label"
148 htmlFor="create-site-enable-nsfw"
150 {i18n.t('enable_nsfw')}
155 <div class="form-group row">
157 <div class="form-check">
159 class="form-check-input"
160 id="create-site-open-registration"
162 checked={this.state.siteForm.open_registration}
165 this.handleSiteOpenRegistrationChange
169 class="form-check-label"
170 htmlFor="create-site-open-registration"
172 {i18n.t('open_registration')}
177 <div class="form-group row">
181 class="btn btn-secondary mr-2"
182 disabled={this.state.loading}
184 {this.state.loading ? (
185 <svg class="icon icon-spinner spin">
186 <use xlinkHref="#icon-spinner"></use>
188 ) : this.props.site ? (
189 capitalizeFirstLetter(i18n.t('save'))
191 capitalizeFirstLetter(i18n.t('create'))
194 {this.props.site && (
197 class="btn btn-secondary"
198 onClick={linkEvent(this, this.handleCancel)}
210 handleCreateSiteSubmit(i: SiteForm, event: any) {
211 event.preventDefault();
212 i.state.loading = true;
214 WebSocketService.Instance.editSite(i.state.siteForm);
216 WebSocketService.Instance.createSite(i.state.siteForm);
221 handleSiteNameChange(i: SiteForm, event: any) {
222 i.state.siteForm.name = event.target.value;
226 handleSiteDescriptionChange(val: string) {
227 this.state.siteForm.description = val;
228 this.setState(this.state);
231 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
232 i.state.siteForm.enable_nsfw = event.target.checked;
236 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
237 i.state.siteForm.open_registration = event.target.checked;
241 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
242 i.state.siteForm.enable_downvotes = event.target.checked;
246 handleCancel(i: SiteForm) {