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 this.parseMessage = this.parseMessage.bind(this);
63 this.subscription = wsSubscribe(this.parseMessage);
65 // Only fetch the data if coming from another route
66 if (this.isoData.path == this.context.router.route.match.url) {
69 listRegistrationApplicationsResponse: this.isoData
70 .routeData[0] as ListRegistrationApplicationsResponse,
82 componentWillUnmount() {
84 this.subscription?.unsubscribe();
88 get documentTitle(): string {
89 const mui = UserService.Instance.myUserInfo;
91 ? `@${mui.local_user_view.person.name} ${i18n.t(
92 "registration_applications"
93 )} - ${this.state.siteRes.site_view.site.name}`
99 <div className="container-lg">
100 {this.state.loading ? (
105 <div className="row">
106 <div className="col-12">
108 title={this.documentTitle}
109 path={this.context.router.route.match.url}
111 <h5 className="mb-2">{i18n.t("registration_applications")}</h5>
113 {this.applicationList()}
115 page={this.state.page}
116 onChange={this.handlePageChange}
125 unreadOrAllRadios() {
127 <div className="btn-group btn-group-toggle flex-wrap mb-2">
129 className={`btn btn-outline-secondary pointer
130 ${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
135 value={UnreadOrAll.Unread}
136 checked={this.state.unreadOrAll == UnreadOrAll.Unread}
137 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
142 className={`btn btn-outline-secondary pointer
143 ${this.state.unreadOrAll == UnreadOrAll.All && "active"}
148 value={UnreadOrAll.All}
149 checked={this.state.unreadOrAll == UnreadOrAll.All}
150 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
160 <div className="mb-2">
161 <span className="mr-3">{this.unreadOrAllRadios()}</span>
167 const res = this.state.listRegistrationApplicationsResponse;
171 {res.registration_applications.map(ra => (
174 <RegistrationApplication
175 key={ra.registration_application.id}
185 handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
186 i.setState({ unreadOrAll: Number(event.target.value), page: 1 });
190 handlePageChange(page: number) {
191 this.setState({ page });
195 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
196 const promises: Promise<any>[] = [];
198 const auth = req.auth;
200 const form: ListRegistrationApplications = {
206 promises.push(req.client.listRegistrationApplications(form));
213 const unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
214 const auth = myAuth();
216 const form: ListRegistrationApplications = {
217 unread_only: unread_only,
218 page: this.state.page,
222 WebSocketService.Instance.send(
223 wsClient.listRegistrationApplications(form)
228 parseMessage(msg: any) {
229 const op = wsUserOp(msg);
232 toast(i18n.t(msg.error), "danger");
234 } else if (msg.reconnect) {
236 } else if (op == UserOperation.ListRegistrationApplications) {
237 const data = wsJsonToRes<ListRegistrationApplicationsResponse>(msg);
239 listRegistrationApplicationsResponse: data,
242 window.scrollTo(0, 0);
243 } else if (op == UserOperation.ApproveRegistrationApplication) {
244 const data = wsJsonToRes<RegistrationApplicationResponse>(msg);
245 updateRegistrationApplicationRes(
246 data.registration_application,
247 this.state.listRegistrationApplicationsResponse
248 ?.registration_applications
250 const uacs = UserService.Instance.unreadApplicationCountSub;
251 // Minor bug, where if the application switches from deny to approve, the count will still go down
252 uacs.next(uacs.getValue() - 1);
253 this.setState(this.state);