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" for="create-site-name">
78 value={this.state.siteForm.name}
79 onInput={linkEvent(this, this.handleSiteNameChange)}
86 <div class="form-group row">
87 <label class="col-12 col-form-label" for="create-site-sidebar">
93 value={this.state.siteForm.description}
94 id="create-site-sidebar"
95 onInput={linkEvent(this, this.handleSiteDescriptionChange)}
102 <div class="form-group row">
104 <div class="form-check">
106 class="form-check-input"
107 id="create-site-downvotes"
109 checked={this.state.siteForm.enable_downvotes}
110 onChange={linkEvent(this, this.handleSiteEnableDownvotesChange)}
112 <label class="form-check-label" for="create-site-downvotes">
113 { i18n.t('enable_downvotes') }
118 <div class="form-group row">
120 <div class="form-check">
122 class="form-check-input"
123 id="create-site-enable-nsfw"
125 checked={this.state.siteForm.enable_nsfw}
126 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
128 <label class="form-check-label" for="create-site-enable-nsfw">
129 { i18n.t('enable_nsfw') }
134 <div class="form-group row">
136 <div class="form-check">
138 class="form-check-input"
139 id="create-site-open-registration"
141 checked={this.state.siteForm.open_registration}
144 this.handleSiteOpenRegistrationChange
147 <label class="form-check-label" for="create-site-open-registration">
148 { i18n.t('open_registration') }
153 <div class="form-group row">
155 <button type="submit" class="btn btn-secondary mr-2">
156 {this.state.loading ? (
157 <svg class="icon icon-spinner spin">
158 <use xlinkHref="#icon-spinner"></use>
160 ) : this.props.site ? (
161 capitalizeFirstLetter(i18n.t('save'))
163 capitalizeFirstLetter(i18n.t('create'))
166 {this.props.site && (
169 class="btn btn-secondary"
170 onClick={linkEvent(this, this.handleCancel)}
181 handleCreateSiteSubmit(i: SiteForm, event: any) {
182 event.preventDefault();
183 i.state.loading = true;
185 WebSocketService.Instance.editSite(i.state.siteForm);
187 WebSocketService.Instance.createSite(i.state.siteForm);
192 handleSiteNameChange(i: SiteForm, event: any) {
193 i.state.siteForm.name = event.target.value;
197 handleSiteDescriptionChange(i: SiteForm, event: any) {
198 i.state.siteForm.description = event.target.value;
202 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
203 i.state.siteForm.enable_nsfw = event.target.checked;
207 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
208 i.state.siteForm.open_registration = event.target.checked;
212 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
213 i.state.siteForm.enable_downvotes = event.target.checked;
217 handleCancel(i: SiteForm) {