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);
66 !this.state.loading &&
67 (this.state.siteForm.name || this.state.siteForm.description)
69 message={i18n.t('block_leaving')}
71 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
74 ? capitalizeFirstLetter(i18n.t('edit'))
75 : capitalizeFirstLetter(i18n.t('name'))
76 } ${i18n.t('your_site')}`}</h5>
77 <div class="form-group row">
78 <label class="col-12 col-form-label" htmlFor="create-site-name">
86 value={this.state.siteForm.name}
87 onInput={linkEvent(this, this.handleSiteNameChange)}
94 <div class="form-group row">
95 <label class="col-12 col-form-label" htmlFor={this.id}>
101 value={this.state.siteForm.description}
102 onInput={linkEvent(this, this.handleSiteDescriptionChange)}
109 <div class="form-group row">
111 <div class="form-check">
113 class="form-check-input"
114 id="create-site-downvotes"
116 checked={this.state.siteForm.enable_downvotes}
119 this.handleSiteEnableDownvotesChange
122 <label class="form-check-label" htmlFor="create-site-downvotes">
123 {i18n.t('enable_downvotes')}
128 <div class="form-group row">
130 <div class="form-check">
132 class="form-check-input"
133 id="create-site-enable-nsfw"
135 checked={this.state.siteForm.enable_nsfw}
136 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
139 class="form-check-label"
140 htmlFor="create-site-enable-nsfw"
142 {i18n.t('enable_nsfw')}
147 <div class="form-group row">
149 <div class="form-check">
151 class="form-check-input"
152 id="create-site-open-registration"
154 checked={this.state.siteForm.open_registration}
157 this.handleSiteOpenRegistrationChange
161 class="form-check-label"
162 htmlFor="create-site-open-registration"
164 {i18n.t('open_registration')}
169 <div class="form-group row">
171 <button type="submit" class="btn btn-secondary mr-2">
172 {this.state.loading ? (
173 <svg class="icon icon-spinner spin">
174 <use xlinkHref="#icon-spinner"></use>
176 ) : this.props.site ? (
177 capitalizeFirstLetter(i18n.t('save'))
179 capitalizeFirstLetter(i18n.t('create'))
182 {this.props.site && (
185 class="btn btn-secondary"
186 onClick={linkEvent(this, this.handleCancel)}
198 handleCreateSiteSubmit(i: SiteForm, event: any) {
199 event.preventDefault();
200 i.state.loading = true;
202 WebSocketService.Instance.editSite(i.state.siteForm);
204 WebSocketService.Instance.createSite(i.state.siteForm);
209 handleSiteNameChange(i: SiteForm, event: any) {
210 i.state.siteForm.name = event.target.value;
214 handleSiteDescriptionChange(i: SiteForm, event: any) {
215 i.state.siteForm.description = event.target.value;
219 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
220 i.state.siteForm.enable_nsfw = event.target.checked;
224 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
225 i.state.siteForm.open_registration = event.target.checked;
229 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
230 i.state.siteForm.enable_downvotes = event.target.checked;
234 handleCancel(i: SiteForm) {