1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { Site, SiteForm as SiteFormI } from '../interfaces';
4 import { WebSocketService } from '../services';
5 import { capitalizeFirstLetter, randomStr, setupTribute } from '../utils';
6 import autosize from 'autosize';
7 import Tribute from 'tributejs/src/Tribute.js';
8 import { i18n } from '../i18next';
10 interface SiteFormProps {
11 site?: Site; // If a site is given, that means this is an edit
15 interface SiteFormState {
20 export class SiteForm extends Component<SiteFormProps, SiteFormState> {
21 private id = `site-form-${randomStr()}`;
22 private tribute: Tribute;
23 private emptyState: SiteFormState = {
25 enable_downvotes: true,
26 open_registration: true,
33 constructor(props: any, context: any) {
34 super(props, context);
36 this.tribute = setupTribute();
37 this.state = this.emptyState;
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,
51 var textarea: any = document.getElementById(this.id);
53 this.tribute.attach(textarea);
54 textarea.addEventListener('tribute-replaced', () => {
55 this.state.siteForm.description = textarea.value;
56 this.setState(this.state);
57 autosize.update(textarea);
61 // Necessary to stop the loading
62 componentWillReceiveProps() {
63 this.state.loading = false;
64 this.setState(this.state);
72 !this.state.loading &&
74 (this.state.siteForm.name || this.state.siteForm.description)
76 message={i18n.t('block_leaving')}
78 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
81 ? capitalizeFirstLetter(i18n.t('save'))
82 : capitalizeFirstLetter(i18n.t('name'))
83 } ${i18n.t('your_site')}`}</h5>
84 <div class="form-group row">
85 <label class="col-12 col-form-label" htmlFor="create-site-name">
93 value={this.state.siteForm.name}
94 onInput={linkEvent(this, this.handleSiteNameChange)}
101 <div class="form-group row">
102 <label class="col-12 col-form-label" htmlFor={this.id}>
108 value={this.state.siteForm.description}
109 onInput={linkEvent(this, this.handleSiteDescriptionChange)}
116 <div class="form-group row">
118 <div class="form-check">
120 class="form-check-input"
121 id="create-site-downvotes"
123 checked={this.state.siteForm.enable_downvotes}
126 this.handleSiteEnableDownvotesChange
129 <label class="form-check-label" htmlFor="create-site-downvotes">
130 {i18n.t('enable_downvotes')}
135 <div class="form-group row">
137 <div class="form-check">
139 class="form-check-input"
140 id="create-site-enable-nsfw"
142 checked={this.state.siteForm.enable_nsfw}
143 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
146 class="form-check-label"
147 htmlFor="create-site-enable-nsfw"
149 {i18n.t('enable_nsfw')}
154 <div class="form-group row">
156 <div class="form-check">
158 class="form-check-input"
159 id="create-site-open-registration"
161 checked={this.state.siteForm.open_registration}
164 this.handleSiteOpenRegistrationChange
168 class="form-check-label"
169 htmlFor="create-site-open-registration"
171 {i18n.t('open_registration')}
176 <div class="form-group row">
180 class="btn btn-secondary mr-2"
181 disabled={this.state.loading}
183 {this.state.loading ? (
184 <svg class="icon icon-spinner spin">
185 <use xlinkHref="#icon-spinner"></use>
187 ) : this.props.site ? (
188 capitalizeFirstLetter(i18n.t('save'))
190 capitalizeFirstLetter(i18n.t('create'))
193 {this.props.site && (
196 class="btn btn-secondary"
197 onClick={linkEvent(this, this.handleCancel)}
209 handleCreateSiteSubmit(i: SiteForm, event: any) {
210 event.preventDefault();
211 i.state.loading = true;
213 WebSocketService.Instance.editSite(i.state.siteForm);
215 WebSocketService.Instance.createSite(i.state.siteForm);
220 handleSiteNameChange(i: SiteForm, event: any) {
221 i.state.siteForm.name = event.target.value;
225 handleSiteDescriptionChange(i: SiteForm, event: any) {
226 i.state.siteForm.description = event.target.value;
230 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
231 i.state.siteForm.enable_nsfw = event.target.checked;
235 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
236 i.state.siteForm.open_registration = event.target.checked;
240 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
241 i.state.siteForm.enable_downvotes = event.target.checked;
245 handleCancel(i: SiteForm) {