1 import { Component, linkEvent } from 'inferno';
2 import { Site, SiteForm as SiteFormI } from '../interfaces';
3 import { WebSocketService } from '../services';
4 import { capitalizeFirstLetter, randomStr, setupTribute } from '../utils';
5 import autosize from 'autosize';
6 import Tribute from 'tributejs/src/Tribute.js';
7 import { i18n } from '../i18next';
8 import { T } from 'inferno-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);
63 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
66 ? capitalizeFirstLetter(i18n.t('edit'))
67 : capitalizeFirstLetter(i18n.t('name'))
68 } ${i18n.t('your_site')}`}</h5>
69 <div class="form-group row">
70 <label class="col-12 col-form-label">
71 <T i18nKey="name">#</T>
77 value={this.state.siteForm.name}
78 onInput={linkEvent(this, this.handleSiteNameChange)}
85 <div class="form-group row">
86 <label class="col-12 col-form-label">
87 <T i18nKey="sidebar">#</T>
92 value={this.state.siteForm.description}
93 onInput={linkEvent(this, this.handleSiteDescriptionChange)}
100 <div class="form-group row">
102 <div class="form-check">
104 class="form-check-input"
106 checked={this.state.siteForm.enable_downvotes}
107 onChange={linkEvent(this, this.handleSiteEnableDownvotesChange)}
109 <label class="form-check-label">
110 <T i18nKey="enable_downvotes">#</T>
115 <div class="form-group row">
117 <div class="form-check">
119 class="form-check-input"
121 checked={this.state.siteForm.enable_nsfw}
122 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
124 <label class="form-check-label">
125 <T i18nKey="enable_nsfw">#</T>
130 <div class="form-group row">
132 <div class="form-check">
134 class="form-check-input"
136 checked={this.state.siteForm.open_registration}
139 this.handleSiteOpenRegistrationChange
142 <label class="form-check-label">
143 <T i18nKey="open_registration">#</T>
148 <div class="form-group row">
150 <button type="submit" class="btn btn-secondary mr-2">
151 {this.state.loading ? (
152 <svg class="icon icon-spinner spin">
153 <use xlinkHref="#icon-spinner"></use>
155 ) : this.props.site ? (
156 capitalizeFirstLetter(i18n.t('save'))
158 capitalizeFirstLetter(i18n.t('create'))
161 {this.props.site && (
164 class="btn btn-secondary"
165 onClick={linkEvent(this, this.handleCancel)}
167 <T i18nKey="cancel">#</T>
176 handleCreateSiteSubmit(i: SiteForm, event: any) {
177 event.preventDefault();
178 i.state.loading = true;
180 WebSocketService.Instance.editSite(i.state.siteForm);
182 WebSocketService.Instance.createSite(i.state.siteForm);
187 handleSiteNameChange(i: SiteForm, event: any) {
188 i.state.siteForm.name = event.target.value;
192 handleSiteDescriptionChange(i: SiteForm, event: any) {
193 i.state.siteForm.description = event.target.value;
197 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
198 i.state.siteForm.enable_nsfw = event.target.checked;
202 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
203 i.state.siteForm.open_registration = event.target.checked;
207 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
208 i.state.siteForm.enable_downvotes = event.target.checked;
212 handleCancel(i: SiteForm) {