2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Subscription } from "rxjs";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
SiteResponse,
|
|
|
|
GetSiteResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
SaveSiteConfig,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetSiteConfigResponse,
|
2020-09-09 02:40:36 +00:00
|
|
|
GetSiteConfig,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { WebSocketService } from "../services";
|
2020-09-09 02:40:36 +00:00
|
|
|
import {
|
|
|
|
wsJsonToRes,
|
|
|
|
capitalizeFirstLetter,
|
|
|
|
toast,
|
|
|
|
randomStr,
|
|
|
|
setIsoData,
|
|
|
|
wsSubscribe,
|
|
|
|
isBrowser,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsUserOp,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
|
|
|
authField,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../utils";
|
|
|
|
import autosize from "autosize";
|
|
|
|
import { SiteForm } from "./site-form";
|
2021-03-15 18:09:31 +00:00
|
|
|
import { PersonListing } from "./person-listing";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { HtmlTags } from "./html-tags";
|
|
|
|
import { Spinner } from "./icon";
|
|
|
|
import { i18n } from "../i18next";
|
|
|
|
import { InitialFetchRequest } from "shared/interfaces";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface AdminSettingsState {
|
|
|
|
siteRes: GetSiteResponse;
|
|
|
|
siteConfigRes: GetSiteConfigResponse;
|
2020-12-24 01:58:27 +00:00
|
|
|
siteConfigForm: SaveSiteConfig;
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
|
|
|
siteConfigLoading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AdminSettings extends Component<any, AdminSettingsState> {
|
|
|
|
private siteConfigTextAreaId = `site-config-${randomStr()}`;
|
2020-09-09 02:40:36 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: AdminSettingsState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2020-09-06 16:15:25 +00:00
|
|
|
siteConfigForm: {
|
|
|
|
config_hjson: null,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
},
|
|
|
|
siteConfigRes: {
|
|
|
|
config_hjson: null,
|
|
|
|
},
|
|
|
|
loading: true,
|
|
|
|
siteConfigLoading: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.siteConfigRes = this.isoData.routeData[0];
|
|
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
|
|
this.state.siteConfigLoading = false;
|
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getSiteConfig({
|
|
|
|
auth: authField(),
|
|
|
|
})
|
|
|
|
);
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: GetSiteConfig = { auth: req.auth };
|
2020-11-12 21:56:46 +00:00
|
|
|
return [req.client.getSiteConfig(form)];
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (isBrowser()) {
|
|
|
|
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
|
|
|
|
autosize(textarea);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-09 02:40:36 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2021-02-22 02:39:04 +00:00
|
|
|
return `${i18n.t("admin_settings")} - ${
|
2020-12-24 01:58:27 +00:00
|
|
|
this.state.siteRes.site_view.site.name
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-md-6">
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.state.siteRes.site_view.site.id && (
|
|
|
|
<SiteForm site={this.state.siteRes.site_view.site} />
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
{this.admins()}
|
|
|
|
{this.bannedUsers()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-md-6">{this.adminSettings()}</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
admins() {
|
|
|
|
return (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ul class="list-unstyled">
|
|
|
|
{this.state.siteRes.admins.map(admin => (
|
|
|
|
<li class="list-inline-item">
|
2021-03-15 18:09:31 +00:00
|
|
|
<PersonListing person={admin.person} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bannedUsers() {
|
|
|
|
return (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("banned_users")}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ul class="list-unstyled">
|
|
|
|
{this.state.siteRes.banned.map(banned => (
|
|
|
|
<li class="list-inline-item">
|
2021-03-15 18:09:31 +00:00
|
|
|
<PersonListing person={banned.person} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
adminSettings() {
|
|
|
|
return (
|
|
|
|
<div>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("admin_settings")}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
<form onSubmit={linkEvent(this, this.handleSiteConfigSubmit)}>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label
|
|
|
|
class="col-12 col-form-label"
|
|
|
|
htmlFor={this.siteConfigTextAreaId}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("site_config")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<textarea
|
|
|
|
id={this.siteConfigTextAreaId}
|
|
|
|
value={this.state.siteConfigForm.config_hjson}
|
|
|
|
onInput={linkEvent(this, this.handleSiteConfigHjsonChange)}
|
|
|
|
class="form-control text-monospace"
|
|
|
|
rows={3}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<button type="submit" class="btn btn-secondary mr-2">
|
|
|
|
{this.state.siteConfigLoading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteConfigSubmit(i: AdminSettings, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.siteConfigLoading = true;
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.saveSiteConfig(i.state.siteConfigForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSiteConfigHjsonChange(i: AdminSettings, event: any) {
|
|
|
|
i.state.siteConfigForm.config_hjson = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
this.context.router.history.push("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditSite) {
|
|
|
|
let data = wsJsonToRes<SiteResponse>(msg).data;
|
|
|
|
this.state.siteRes.site_view = data.site_view;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("site_saved"));
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.GetSiteConfig) {
|
|
|
|
let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.siteConfigRes = data;
|
|
|
|
this.state.loading = false;
|
|
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
|
|
this.setState(this.state);
|
|
|
|
var textarea: any = document.getElementById(this.siteConfigTextAreaId);
|
|
|
|
autosize(textarea);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.SaveSiteConfig) {
|
|
|
|
let data = wsJsonToRes<GetSiteConfigResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.siteConfigRes = data;
|
|
|
|
this.state.siteConfigForm.config_hjson = this.state.siteConfigRes.config_hjson;
|
|
|
|
this.state.siteConfigLoading = false;
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("site_saved"));
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|