1 import autosize from "autosize";
2 import { Component, linkEvent } from "inferno";
12 } from "lemmy-js-client";
13 import { Subscription } from "rxjs";
14 import { i18n } from "../../i18next";
15 import { InitialFetchRequest } from "../../interfaces";
16 import { WebSocketService } from "../../services";
18 capitalizeFirstLetter,
28 import { HtmlTags } from "../common/html-tags";
29 import { Spinner } from "../common/icon";
30 import { PersonListing } from "../person/person-listing";
31 import { SiteForm } from "./site-form";
33 interface AdminSettingsState {
34 siteRes: GetSiteResponse;
35 banned: PersonViewSafe[];
37 leaveAdminTeamLoading: boolean;
40 export class AdminSettings extends Component<any, AdminSettingsState> {
41 private siteConfigTextAreaId = `site-config-${randomStr()}`;
42 private isoData = setIsoData(this.context);
43 private subscription?: Subscription;
44 state: AdminSettingsState = {
45 siteRes: this.isoData.site_res,
48 leaveAdminTeamLoading: false,
51 constructor(props: any, context: any) {
52 super(props, context);
54 this.parseMessage = this.parseMessage.bind(this);
55 this.subscription = wsSubscribe(this.parseMessage);
57 // Only fetch the data if coming from another route
58 if (this.isoData.path == this.context.router.route.match.url) {
61 banned: (this.isoData.routeData[0] as BannedPersonsResponse).banned,
67 WebSocketService.Instance.send(
68 wsClient.getBannedPersons({
76 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
77 let promises: Promise<any>[] = [];
81 let bannedPersonsForm: GetBannedPersons = { auth };
82 promises.push(req.client.getBannedPersons(bannedPersonsForm));
90 var textarea: any = document.getElementById(this.siteConfigTextAreaId);
95 componentWillUnmount() {
97 this.subscription?.unsubscribe();
101 get documentTitle(): string {
102 return `${i18n.t("admin_settings")} - ${
103 this.state.siteRes.site_view.site.name
109 <div className="container-lg">
110 {this.state.loading ? (
115 <div className="row">
116 <div className="col-12 col-md-6">
118 title={this.documentTitle}
119 path={this.context.router.route.match.url}
122 siteRes={this.state.siteRes}
123 showLocal={showLocal(this.isoData)}
126 <div className="col-12 col-md-6">
139 <h5>{capitalizeFirstLetter(i18n.t("admins"))}</h5>
140 <ul className="list-unstyled">
141 {this.state.siteRes.admins.map(admin => (
142 <li key={admin.person.id} className="list-inline-item">
143 <PersonListing person={admin.person} />
155 onClick={linkEvent(this, this.handleLeaveAdminTeam)}
156 className="btn btn-danger mb-2"
158 {this.state.leaveAdminTeamLoading ? (
161 i18n.t("leave_admin_team")
170 <h5>{i18n.t("banned_users")}</h5>
171 <ul className="list-unstyled">
172 {this.state.banned.map(banned => (
173 <li key={banned.person.id} className="list-inline-item">
174 <PersonListing person={banned.person} />
182 handleLeaveAdminTeam(i: AdminSettings) {
185 i.setState({ leaveAdminTeamLoading: true });
186 WebSocketService.Instance.send(wsClient.leaveAdmin({ auth }));
190 parseMessage(msg: any) {
191 let op = wsUserOp(msg);
194 toast(i18n.t(msg.error), "danger");
195 this.context.router.history.push("/");
196 this.setState({ loading: false });
198 } else if (op == UserOperation.EditSite) {
199 let data = wsJsonToRes<SiteResponse>(msg);
200 this.setState(s => ((s.siteRes.site_view = data.site_view), s));
201 toast(i18n.t("site_saved"));
202 } else if (op == UserOperation.GetBannedPersons) {
203 let data = wsJsonToRes<BannedPersonsResponse>(msg);
204 this.setState({ banned: data.banned, loading: false });
205 } else if (op == UserOperation.LeaveAdmin) {
206 let data = wsJsonToRes<GetSiteResponse>(msg);
207 this.setState(s => ((s.siteRes.site_view = data.site_view), s));
208 this.setState({ leaveAdminTeamLoading: false });
210 toast(i18n.t("left_admin_team"));
211 this.context.router.history.push("/");