1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { MarkdownTextArea } from './markdown-textarea';
4 import { ImageUploadForm } from './image-upload-form';
5 import { Site, EditSite } from 'lemmy-js-client';
6 import { UserService, WebSocketService } from '../services';
7 import { capitalizeFirstLetter, randomStr } from '../utils';
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 emptyState: SiteFormState = {
24 enable_downvotes: true,
25 open_registration: true,
30 auth: UserService.Instance.authField(),
35 constructor(props: any, context: any) {
36 super(props, context);
38 this.state = this.emptyState;
39 this.handleSiteDescriptionChange = this.handleSiteDescriptionChange.bind(
43 this.handleIconUpload = this.handleIconUpload.bind(this);
44 this.handleIconRemove = this.handleIconRemove.bind(this);
46 this.handleBannerUpload = this.handleBannerUpload.bind(this);
47 this.handleBannerRemove = this.handleBannerRemove.bind(this);
49 if (this.props.site) {
50 this.state.siteForm = {
51 name: this.props.site.name,
52 description: this.props.site.description,
53 enable_downvotes: this.props.site.enable_downvotes,
54 open_registration: this.props.site.open_registration,
55 enable_nsfw: this.props.site.enable_nsfw,
56 icon: this.props.site.icon,
57 banner: this.props.site.banner,
58 auth: UserService.Instance.authField(),
63 // Necessary to stop the loading
64 componentWillReceiveProps() {
65 this.state.loading = false;
66 this.setState(this.state);
69 componentDidUpdate() {
71 !this.state.loading &&
73 (this.state.siteForm.name || this.state.siteForm.description)
75 window.onbeforeunload = () => true;
77 window.onbeforeunload = undefined;
81 componentWillUnmount() {
82 window.onbeforeunload = null;
90 !this.state.loading &&
92 (this.state.siteForm.name || this.state.siteForm.description)
94 message={i18n.t('block_leaving')}
96 <form onSubmit={linkEvent(this, this.handleCreateSiteSubmit)}>
99 ? capitalizeFirstLetter(i18n.t('save'))
100 : capitalizeFirstLetter(i18n.t('name'))
101 } ${i18n.t('your_site')}`}</h5>
102 <div class="form-group row">
103 <label class="col-12 col-form-label" htmlFor="create-site-name">
109 id="create-site-name"
111 value={this.state.siteForm.name}
112 onInput={linkEvent(this, this.handleSiteNameChange)}
119 <div class="form-group">
120 <label>{i18n.t('icon')}</label>
122 uploadTitle={i18n.t('upload_icon')}
123 imageSrc={this.state.siteForm.icon}
124 onUpload={this.handleIconUpload}
125 onRemove={this.handleIconRemove}
129 <div class="form-group">
130 <label>{i18n.t('banner')}</label>
132 uploadTitle={i18n.t('upload_banner')}
133 imageSrc={this.state.siteForm.banner}
134 onUpload={this.handleBannerUpload}
135 onRemove={this.handleBannerRemove}
138 <div class="form-group row">
139 <label class="col-12 col-form-label" htmlFor={this.id}>
144 initialContent={this.state.siteForm.description}
145 onContentChange={this.handleSiteDescriptionChange}
146 hideNavigationWarnings
150 <div class="form-group row">
152 <div class="form-check">
154 class="form-check-input"
155 id="create-site-downvotes"
157 checked={this.state.siteForm.enable_downvotes}
160 this.handleSiteEnableDownvotesChange
163 <label class="form-check-label" htmlFor="create-site-downvotes">
164 {i18n.t('enable_downvotes')}
169 <div class="form-group row">
171 <div class="form-check">
173 class="form-check-input"
174 id="create-site-enable-nsfw"
176 checked={this.state.siteForm.enable_nsfw}
177 onChange={linkEvent(this, this.handleSiteEnableNsfwChange)}
180 class="form-check-label"
181 htmlFor="create-site-enable-nsfw"
183 {i18n.t('enable_nsfw')}
188 <div class="form-group row">
190 <div class="form-check">
192 class="form-check-input"
193 id="create-site-open-registration"
195 checked={this.state.siteForm.open_registration}
198 this.handleSiteOpenRegistrationChange
202 class="form-check-label"
203 htmlFor="create-site-open-registration"
205 {i18n.t('open_registration')}
210 <div class="form-group row">
214 class="btn btn-secondary mr-2"
215 disabled={this.state.loading}
217 {this.state.loading ? (
218 <svg class="icon icon-spinner spin">
219 <use xlinkHref="#icon-spinner"></use>
221 ) : this.props.site ? (
222 capitalizeFirstLetter(i18n.t('save'))
224 capitalizeFirstLetter(i18n.t('create'))
227 {this.props.site && (
230 class="btn btn-secondary"
231 onClick={linkEvent(this, this.handleCancel)}
243 handleCreateSiteSubmit(i: SiteForm, event: any) {
244 event.preventDefault();
245 i.state.loading = true;
247 WebSocketService.Instance.client.editSite(i.state.siteForm);
249 WebSocketService.Instance.client.createSite(i.state.siteForm);
254 handleSiteNameChange(i: SiteForm, event: any) {
255 i.state.siteForm.name = event.target.value;
259 handleSiteDescriptionChange(val: string) {
260 this.state.siteForm.description = val;
261 this.setState(this.state);
264 handleSiteEnableNsfwChange(i: SiteForm, event: any) {
265 i.state.siteForm.enable_nsfw = event.target.checked;
269 handleSiteOpenRegistrationChange(i: SiteForm, event: any) {
270 i.state.siteForm.open_registration = event.target.checked;
274 handleSiteEnableDownvotesChange(i: SiteForm, event: any) {
275 i.state.siteForm.enable_downvotes = event.target.checked;
279 handleCancel(i: SiteForm) {
283 handleIconUpload(url: string) {
284 this.state.siteForm.icon = url;
285 this.setState(this.state);
289 this.state.siteForm.icon = '';
290 this.setState(this.state);
293 handleBannerUpload(url: string) {
294 this.state.siteForm.banner = url;
295 this.setState(this.state);
298 handleBannerRemove() {
299 this.state.siteForm.banner = '';
300 this.setState(this.state);