2019-04-16 23:04:23 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Site, SiteForm as SiteFormI } from '../interfaces';
|
|
|
|
import { WebSocketService } from '../services';
|
2020-01-24 18:59:50 +00:00
|
|
|
import { capitalizeFirstLetter, randomStr, setupTribute } from '../utils';
|
2020-01-06 16:22:51 +00:00
|
|
|
import autosize from 'autosize';
|
2020-01-24 18:59:50 +00:00
|
|
|
import Tribute from 'tributejs/src/Tribute.js';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
interface SiteFormProps {
|
|
|
|
site?: Site; // If a site is given, that means this is an edit
|
|
|
|
onCancel?(): any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SiteFormState {
|
|
|
|
siteForm: SiteFormI;
|
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SiteForm extends Component<SiteFormProps, SiteFormState> {
|
2020-01-24 18:59:50 +00:00
|
|
|
private id = `site-form-${randomStr()}`;
|
|
|
|
private tribute: Tribute;
|
2019-10-19 00:20:27 +00:00
|
|
|
private emptyState: SiteFormState = {
|
2019-04-16 23:04:23 +00:00
|
|
|
siteForm: {
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: true,
|
|
|
|
open_registration: true,
|
|
|
|
enable_nsfw: true,
|
2019-10-19 00:20:27 +00:00
|
|
|
name: null,
|
2019-04-16 23:04:23 +00:00
|
|
|
},
|
2019-10-19 00:20:27 +00:00
|
|
|
loading: false,
|
|
|
|
};
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
2020-01-24 18:59:50 +00:00
|
|
|
|
|
|
|
this.tribute = setupTribute();
|
2019-04-16 23:04:23 +00:00
|
|
|
this.state = this.emptyState;
|
2020-01-24 18:59:50 +00:00
|
|
|
|
2019-04-21 21:38:57 +00:00
|
|
|
if (this.props.site) {
|
|
|
|
this.state.siteForm = {
|
|
|
|
name: this.props.site.name,
|
|
|
|
description: this.props.site.description,
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: this.props.site.enable_downvotes,
|
|
|
|
open_registration: this.props.site.open_registration,
|
|
|
|
enable_nsfw: this.props.site.enable_nsfw,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-21 21:38:57 +00:00
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-01-24 18:59:50 +00:00
|
|
|
var textarea: any = document.getElementById(this.id);
|
|
|
|
autosize(textarea);
|
|
|
|
this.tribute.attach(textarea);
|
|
|
|
textarea.addEventListener('tribute-replaced', () => {
|
|
|
|
this.state.siteForm.description = textarea.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
autosize.update(textarea);
|
|
|
|
});
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
|
2019-10-19 00:20:27 +00:00
|
|
|
<h5>{`${
|
|
|
|
this.props.site
|
|
|
|
? capitalizeFirstLetter(i18n.t('edit'))
|
|
|
|
: capitalizeFirstLetter(i18n.t('name'))
|
|
|
|
} ${i18n.t('your_site')}`}</h5>
|
2019-04-16 23:04:23 +00:00
|
|
|
<div class="form-group row">
|
2020-01-31 20:52:27 +00:00
|
|
|
<label class="col-12 col-form-label" for="create-site-name">
|
|
|
|
{ i18n.t('name') }
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
2019-04-16 23:04:23 +00:00
|
|
|
<div class="col-12">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="create-site-name"
|
2019-10-19 00:20:27 +00:00
|
|
|
class="form-control"
|
|
|
|
value={this.state.siteForm.name}
|
|
|
|
onInput={linkEvent(this, this.handleSiteNameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
maxLength={20}
|
|
|
|
/>
|
2019-04-16 23:04:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2020-01-31 20:52:27 +00:00
|
|
|
<label class="col-12 col-form-label" for="create-site-sidebar">
|
|
|
|
{ i18n.t('sidebar') }
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
2019-04-16 23:04:23 +00:00
|
|
|
<div class="col-12">
|
2019-10-19 00:20:27 +00:00
|
|
|
<textarea
|
2020-01-24 18:59:50 +00:00
|
|
|
id={this.id}
|
2019-10-19 00:20:27 +00:00
|
|
|
value={this.state.siteForm.description}
|
2020-01-31 20:52:27 +00:00
|
|
|
id="create-site-sidebar"
|
2019-10-19 00:20:27 +00:00
|
|
|
onInput={linkEvent(this, this.handleSiteDescriptionChange)}
|
|
|
|
class="form-control"
|
|
|
|
rows={3}
|
|
|
|
maxLength={10000}
|
|
|
|
/>
|
2019-04-16 23:04:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-12-11 20:21:47 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="create-site-downvotes"
|
2019-12-11 20:21:47 +00:00
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.enable_downvotes}
|
|
|
|
onChange={linkEvent(this, this.handleSiteEnableDownvotesChange)}
|
|
|
|
/>
|
2020-01-31 20:52:27 +00:00
|
|
|
<label class="form-check-label" for="create-site-downvotes">
|
|
|
|
{ i18n.t('enable_downvotes') }
|
2019-12-11 20:21:47 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="create-site-enable-nsfw"
|
2019-12-11 20:21:47 +00:00
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.enable_nsfw}
|
|
|
|
onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
|
|
|
|
/>
|
2020-01-31 20:52:27 +00:00
|
|
|
<label class="form-check-label" for="create-site-enable-nsfw">
|
|
|
|
{ i18n.t('enable_nsfw') }
|
2019-12-11 20:21:47 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="create-site-open-registration"
|
2019-12-11 20:21:47 +00:00
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.siteForm.open_registration}
|
|
|
|
onChange={linkEvent(
|
|
|
|
this,
|
|
|
|
this.handleSiteOpenRegistrationChange
|
|
|
|
)}
|
|
|
|
/>
|
2020-01-31 20:52:27 +00:00
|
|
|
<label class="form-check-label" for="create-site-open-registration">
|
|
|
|
{ i18n.t('open_registration') }
|
2019-12-11 20:21:47 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-04-16 23:04:23 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<button type="submit" class="btn btn-secondary mr-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : this.props.site ? (
|
|
|
|
capitalizeFirstLetter(i18n.t('save'))
|
|
|
|
) : (
|
|
|
|
capitalizeFirstLetter(i18n.t('create'))
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
{this.props.site && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
|
|
|
>
|
2020-01-31 20:52:27 +00:00
|
|
|
{ i18n.t('cancel') }
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2019-04-16 23:04:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateSiteSubmit(i: SiteForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
|
|
|
if (i.props.site) {
|
|
|
|
WebSocketService.Instance.editSite(i.state.siteForm);
|
|
|
|
} else {
|
|
|
|
WebSocketService.Instance.createSite(i.state.siteForm);
|
|
|
|
}
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteNameChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.name = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteDescriptionChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.description = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:21:47 +00:00
|
|
|
handleSiteEnableNsfwChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.enable_nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.open_registration = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
|
|
|
|
i.state.siteForm.enable_downvotes = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
handleCancel(i: SiteForm) {
|
|
|
|
i.props.onCancel();
|
|
|
|
}
|
|
|
|
}
|