1 import { Component, linkEvent } from "inferno";
4 ListRegistrationApplications,
5 ListRegistrationApplicationsResponse,
6 RegistrationApplicationResponse,
10 } from "lemmy-js-client";
11 import { Subscription } from "rxjs";
12 import { i18n } from "../../i18next";
13 import { InitialFetchRequest } from "../../interfaces";
14 import { UserService, WebSocketService } from "../../services";
22 updateRegistrationApplicationRes,
26 import { HtmlTags } from "../common/html-tags";
27 import { Spinner } from "../common/icon";
28 import { Paginator } from "../common/paginator";
29 import { RegistrationApplication } from "../common/registration-application";
36 interface RegistrationApplicationsState {
37 listRegistrationApplicationsResponse?: ListRegistrationApplicationsResponse;
38 siteRes: GetSiteResponse;
39 unreadOrAll: UnreadOrAll;
44 export class RegistrationApplications extends Component<
46 RegistrationApplicationsState
48 private isoData = setIsoData(this.context);
49 private subscription?: Subscription;
50 state: RegistrationApplicationsState = {
51 siteRes: this.isoData.site_res,
52 unreadOrAll: UnreadOrAll.Unread,
57 constructor(props: any, context: any) {
58 super(props, context);
60 this.handlePageChange = this.handlePageChange.bind(this);
62 if (!UserService.Instance.myUserInfo && isBrowser()) {
63 toast(i18n.t("not_logged_in"), "danger");
64 this.context.router.history.push(`/login`);
67 this.parseMessage = this.parseMessage.bind(this);
68 this.subscription = wsSubscribe(this.parseMessage);
70 // Only fetch the data if coming from another route
71 if (this.isoData.path == this.context.router.route.match.url) {
74 listRegistrationApplicationsResponse: this.isoData
75 .routeData[0] as ListRegistrationApplicationsResponse,
87 componentWillUnmount() {
89 this.subscription?.unsubscribe();
93 get documentTitle(): string {
94 let mui = UserService.Instance.myUserInfo;
96 ? `@${mui.local_user_view.person.name} ${i18n.t(
97 "registration_applications"
98 )} - ${this.state.siteRes.site_view.site.name}`
104 <div className="container-lg">
105 {this.state.loading ? (
110 <div className="row">
111 <div className="col-12">
113 title={this.documentTitle}
114 path={this.context.router.route.match.url}
116 <h5 className="mb-2">{i18n.t("registration_applications")}</h5>
118 {this.applicationList()}
120 page={this.state.page}
121 onChange={this.handlePageChange}
130 unreadOrAllRadios() {
132 <div className="btn-group btn-group-toggle flex-wrap mb-2">
134 className={`btn btn-outline-secondary pointer
135 ${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
140 value={UnreadOrAll.Unread}
141 checked={this.state.unreadOrAll == UnreadOrAll.Unread}
142 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
147 className={`btn btn-outline-secondary pointer
148 ${this.state.unreadOrAll == UnreadOrAll.All && "active"}
153 value={UnreadOrAll.All}
154 checked={this.state.unreadOrAll == UnreadOrAll.All}
155 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
165 <div className="mb-2">
166 <span className="mr-3">{this.unreadOrAllRadios()}</span>
172 let res = this.state.listRegistrationApplicationsResponse;
176 {res.registration_applications.map(ra => (
179 <RegistrationApplication
180 key={ra.registration_application.id}
190 handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
191 i.setState({ unreadOrAll: Number(event.target.value), page: 1 });
195 handlePageChange(page: number) {
196 this.setState({ page });
200 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
201 let promises: Promise<any>[] = [];
205 let form: ListRegistrationApplications = {
211 promises.push(req.client.listRegistrationApplications(form));
218 let unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
221 let form: ListRegistrationApplications = {
222 unread_only: unread_only,
223 page: this.state.page,
227 WebSocketService.Instance.send(
228 wsClient.listRegistrationApplications(form)
233 parseMessage(msg: any) {
234 let op = wsUserOp(msg);
237 toast(i18n.t(msg.error), "danger");
239 } else if (msg.reconnect) {
241 } else if (op == UserOperation.ListRegistrationApplications) {
242 let data = wsJsonToRes<ListRegistrationApplicationsResponse>(msg);
244 listRegistrationApplicationsResponse: data,
247 window.scrollTo(0, 0);
248 } else if (op == UserOperation.ApproveRegistrationApplication) {
249 let data = wsJsonToRes<RegistrationApplicationResponse>(msg);
250 updateRegistrationApplicationRes(
251 data.registration_application,
252 this.state.listRegistrationApplicationsResponse
253 ?.registration_applications
255 let uacs = UserService.Instance.unreadApplicationCountSub;
256 // Minor bug, where if the application switches from deny to approve, the count will still go down
257 uacs.next(uacs.getValue() - 1);
258 this.setState(this.state);