1 import { Component, linkEvent } from "inferno";
3 ListRegistrationApplications,
4 ListRegistrationApplicationsResponse,
5 RegistrationApplicationResponse,
6 RegistrationApplicationView,
9 } from "lemmy-js-client";
10 import { Subscription } from "rxjs";
11 import { i18n } from "../../i18next";
12 import { InitialFetchRequest } from "../../interfaces";
13 import { UserService, WebSocketService } from "../../services";
21 updateRegistrationApplicationRes,
27 import { HtmlTags } from "../common/html-tags";
28 import { Spinner } from "../common/icon";
29 import { Paginator } from "../common/paginator";
30 import { RegistrationApplication } from "../common/registration-application";
37 interface RegistrationApplicationsState {
38 applications: RegistrationApplicationView[];
41 unreadOrAll: UnreadOrAll;
45 export class RegistrationApplications extends Component<
47 RegistrationApplicationsState
49 private isoData = setIsoData(this.context);
50 private subscription: Subscription;
51 private emptyState: RegistrationApplicationsState = {
52 unreadOrAll: UnreadOrAll.Unread,
55 site_view: this.isoData.site_res.site_view,
59 constructor(props: any, context: any) {
60 super(props, context);
62 this.state = this.emptyState;
63 this.handlePageChange = this.handlePageChange.bind(this);
65 if (!UserService.Instance.myUserInfo && isBrowser()) {
66 toast(i18n.t("not_logged_in"), "danger");
67 this.context.router.history.push(`/login`);
70 this.parseMessage = this.parseMessage.bind(this);
71 this.subscription = wsSubscribe(this.parseMessage);
73 // Only fetch the data if coming from another route
74 if (this.isoData.path == this.context.router.route.match.url) {
75 this.state.applications =
76 this.isoData.routeData[0].registration_applications || []; // TODO test
77 this.state.loading = false;
87 componentWillUnmount() {
89 this.subscription.unsubscribe();
93 get documentTitle(): string {
95 UserService.Instance.myUserInfo.local_user_view.person.name
96 } ${i18n.t("registration_applications")} - ${
97 this.state.site_view.site.name
103 <div class="container">
104 {this.state.loading ? (
112 title={this.documentTitle}
113 path={this.context.router.route.match.url}
115 <h5 class="mb-2">{i18n.t("registration_applications")}</h5>
117 {this.applicationList()}
119 page={this.state.page}
120 onChange={this.handlePageChange}
129 unreadOrAllRadios() {
131 <div class="btn-group btn-group-toggle flex-wrap mb-2">
133 className={`btn btn-outline-secondary pointer
134 ${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
139 value={UnreadOrAll.Unread}
140 checked={this.state.unreadOrAll == UnreadOrAll.Unread}
141 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
146 className={`btn btn-outline-secondary pointer
147 ${this.state.unreadOrAll == UnreadOrAll.All && "active"}
152 value={UnreadOrAll.All}
153 checked={this.state.unreadOrAll == UnreadOrAll.All}
154 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
164 <div className="mb-2">
165 <span class="mr-3">{this.unreadOrAllRadios()}</span>
173 {this.state.applications.map(ra => (
176 <RegistrationApplication
177 key={ra.registration_application.id}
186 handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
187 i.state.unreadOrAll = Number(event.target.value);
193 handlePageChange(page: number) {
194 this.setState({ page });
198 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
199 let promises: Promise<any>[] = [];
201 let form: ListRegistrationApplications = {
207 promises.push(req.client.listRegistrationApplications(form));
213 let unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
214 let form: ListRegistrationApplications = {
215 unread_only: unread_only,
216 page: this.state.page,
220 WebSocketService.Instance.send(wsClient.listRegistrationApplications(form));
223 parseMessage(msg: any) {
224 let op = wsUserOp(msg);
227 toast(i18n.t(msg.error), "danger");
229 } else if (msg.reconnect) {
231 } else if (op == UserOperation.ListRegistrationApplications) {
232 let data = wsJsonToRes<ListRegistrationApplicationsResponse>(msg).data;
233 this.state.applications = data.registration_applications;
234 this.state.loading = false;
235 window.scrollTo(0, 0);
236 this.setState(this.state);
237 } else if (op == UserOperation.ApproveRegistrationApplication) {
238 let data = wsJsonToRes<RegistrationApplicationResponse>(msg).data;
239 updateRegistrationApplicationRes(
240 data.registration_application,
241 this.state.applications
243 let uacs = UserService.Instance.unreadApplicationCountSub;
244 // Minor bug, where if the application switches from deny to approve, the count will still go down
245 uacs.next(uacs.getValue() - 1);
246 this.setState(this.state);