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">
178 <button type="submit" class="btn btn-secondary mr-2">
179 {this.state.loading ? (
180 <svg class="icon icon-spinner spin">
181 <use xlinkHref="#icon-spinner"></use>
183 ) : this.props.site ? (
184 capitalizeFirstLetter(i18n.t('save'))
186 capitalizeFirstLetter(i18n.t('create'))
189 {this.props.site && (
192 class="btn btn-secondary"
193 onClick={linkEvent(this, this.handleCancel)}
205 handleCreateSiteSubmit(i: SiteForm, event: any) {
206 event.preventDefault();
207 i.state.loading = true;
209 WebSocketService.Instance.editSite(i.state.siteForm);
211 WebSocketService.Instance.createSite(i.state.siteForm);
216 handleSiteNameChange(i: SiteForm, event: any) {
217 i.state.siteForm.name = event.target.value;
221 handleSiteDescriptionChange(i: SiteForm, event: any) {
222 i.state.siteForm.description = event.target.value;
226 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
227 i.state.siteForm.enable_nsfw = event.target.checked;
231 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
232 i.state.siteForm.open_registration = event.target.checked;
236 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
237 i.state.siteForm.enable_downvotes = event.target.checked;
241 handleCancel(i: SiteForm) {