1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
5 ListRegistrationApplications,
6 ListRegistrationApplicationsResponse,
7 RegistrationApplicationResponse,
11 } from "lemmy-js-client";
12 import { Subscription } from "rxjs";
13 import { i18n } from "../../i18next";
14 import { InitialFetchRequest } from "../../interfaces";
15 import { UserService, WebSocketService } from "../../services";
23 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 listRegistrationApplicationsResponse: Option<ListRegistrationApplicationsResponse>;
39 siteRes: GetSiteResponse;
40 unreadOrAll: UnreadOrAll;
45 export class RegistrationApplications extends Component<
47 RegistrationApplicationsState
49 private isoData = setIsoData(
51 ListRegistrationApplicationsResponse
53 private subscription: Subscription;
54 private emptyState: RegistrationApplicationsState = {
55 listRegistrationApplicationsResponse: None,
56 siteRes: this.isoData.site_res,
57 unreadOrAll: UnreadOrAll.Unread,
62 constructor(props: any, context: any) {
63 super(props, context);
65 this.state = this.emptyState;
66 this.handlePageChange = this.handlePageChange.bind(this);
68 if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
69 toast(i18n.t("not_logged_in"), "danger");
70 this.context.router.history.push(`/login`);
73 this.parseMessage = this.parseMessage.bind(this);
74 this.subscription = wsSubscribe(this.parseMessage);
76 // Only fetch the data if coming from another route
77 if (this.isoData.path == this.context.router.route.match.url) {
80 listRegistrationApplicationsResponse: Some(
81 this.isoData.routeData[0] as ListRegistrationApplicationsResponse
94 componentWillUnmount() {
96 this.subscription.unsubscribe();
100 get documentTitle(): string {
101 return UserService.Instance.myUserInfo.match({
103 `@${mui.local_user_view.person.name} ${i18n.t(
104 "registration_applications"
105 )} - ${this.state.siteRes.site_view.site.name}`,
112 <div className="container-lg">
113 {this.state.loading ? (
118 <div className="row">
119 <div className="col-12">
121 title={this.documentTitle}
122 path={this.context.router.route.match.url}
126 <h5 className="mb-2">{i18n.t("registration_applications")}</h5>
128 {this.applicationList()}
130 page={this.state.page}
131 onChange={this.handlePageChange}
140 unreadOrAllRadios() {
142 <div className="btn-group btn-group-toggle flex-wrap mb-2">
144 className={`btn btn-outline-secondary pointer
145 ${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
150 value={UnreadOrAll.Unread}
151 checked={this.state.unreadOrAll == UnreadOrAll.Unread}
152 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
157 className={`btn btn-outline-secondary pointer
158 ${this.state.unreadOrAll == UnreadOrAll.All && "active"}
163 value={UnreadOrAll.All}
164 checked={this.state.unreadOrAll == UnreadOrAll.All}
165 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
175 <div className="mb-2">
176 <span className="mr-3">{this.unreadOrAllRadios()}</span>
182 return this.state.listRegistrationApplicationsResponse.match({
185 {res.registration_applications.map(ra => (
188 <RegistrationApplication
189 key={ra.registration_application.id}
200 handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
201 i.setState({ unreadOrAll: Number(event.target.value), page: 1 });
205 handlePageChange(page: number) {
206 this.setState({ page });
210 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
211 let promises: Promise<any>[] = [];
213 let form = new ListRegistrationApplications({
214 unread_only: Some(true),
216 limit: Some(fetchLimit),
217 auth: req.auth.unwrap(),
219 promises.push(req.client.listRegistrationApplications(form));
225 let unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
226 let form = new ListRegistrationApplications({
227 unread_only: Some(unread_only),
228 page: Some(this.state.page),
229 limit: Some(fetchLimit),
230 auth: auth().unwrap(),
232 WebSocketService.Instance.send(wsClient.listRegistrationApplications(form));
235 parseMessage(msg: any) {
236 let op = wsUserOp(msg);
239 toast(i18n.t(msg.error), "danger");
241 } else if (msg.reconnect) {
243 } else if (op == UserOperation.ListRegistrationApplications) {
244 let data = wsJsonToRes<ListRegistrationApplicationsResponse>(
246 ListRegistrationApplicationsResponse
249 listRegistrationApplicationsResponse: Some(data),
252 window.scrollTo(0, 0);
253 } else if (op == UserOperation.ApproveRegistrationApplication) {
254 let data = wsJsonToRes<RegistrationApplicationResponse>(
256 RegistrationApplicationResponse
258 updateRegistrationApplicationRes(
259 data.registration_application,
260 this.state.listRegistrationApplicationsResponse
261 .map(r => r.registration_applications)
264 let uacs = UserService.Instance.unreadApplicationCountSub;
265 // Minor bug, where if the application switches from deny to approve, the count will still go down
266 uacs.next(uacs.getValue() - 1);
267 this.setState(this.state);