1 import { Component, linkEvent } from 'inferno';
2 import { Site, SiteForm as SiteFormI } from '../interfaces';
3 import { WebSocketService } from '../services';
4 import { capitalizeFirstLetter } from '../utils';
5 import * as autosize from 'autosize';
6 import { i18n } from '../i18next';
7 import { T } from 'inferno-i18next';
9 interface SiteFormProps {
10 site?: Site; // If a site is given, that means this is an edit
14 interface SiteFormState {
19 export class SiteForm extends Component<SiteFormProps, SiteFormState> {
20 private emptyState: SiteFormState = {
27 constructor(props: any, context: any) {
28 super(props, context);
29 this.state = this.emptyState;
30 if (this.props.site) {
31 this.state.siteForm = {
32 name: this.props.site.name,
33 description: this.props.site.description,
39 autosize(document.querySelectorAll('textarea'));
44 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
47 ? capitalizeFirstLetter(i18n.t('edit'))
48 : capitalizeFirstLetter(i18n.t('name'))
49 } ${i18n.t('your_site')}`}</h5>
50 <div class="form-group row">
51 <label class="col-12 col-form-label">
52 <T i18nKey="name">#</T>
58 value={this.state.siteForm.name}
59 onInput={linkEvent(this, this.handleSiteNameChange)}
66 <div class="form-group row">
67 <label class="col-12 col-form-label">
68 <T i18nKey="sidebar">#</T>
72 value={this.state.siteForm.description}
73 onInput={linkEvent(this, this.handleSiteDescriptionChange)}
80 <div class="form-group row">
82 <button type="submit" class="btn btn-secondary mr-2">
83 {this.state.loading ? (
84 <svg class="icon icon-spinner spin">
85 <use xlinkHref="#icon-spinner"></use>
87 ) : this.props.site ? (
88 capitalizeFirstLetter(i18n.t('save'))
90 capitalizeFirstLetter(i18n.t('create'))
96 class="btn btn-secondary"
97 onClick={linkEvent(this, this.handleCancel)}
99 <T i18nKey="cancel">#</T>
108 handleCreateSiteSubmit(i: SiteForm, event: any) {
109 event.preventDefault();
110 i.state.loading = true;
112 WebSocketService.Instance.editSite(i.state.siteForm);
114 WebSocketService.Instance.createSite(i.state.siteForm);
119 handleSiteNameChange(i: SiteForm, event: any) {
120 i.state.siteForm.name = event.target.value;
124 handleSiteDescriptionChange(i: SiteForm, event: any) {
125 i.state.siteForm.description = event.target.value;
129 handleCancel(i: SiteForm) {