2021-12-30 15:26:45 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2021-12-30 15:26:45 +00:00
|
|
|
ListRegistrationApplications,
|
|
|
|
ListRegistrationApplicationsResponse,
|
|
|
|
RegistrationApplicationResponse,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-12-30 15:26:45 +00:00
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { InitialFetchRequest } from "../../interfaces";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
|
|
|
import {
|
|
|
|
fetchLimit,
|
|
|
|
isBrowser,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2021-12-30 15:26:45 +00:00
|
|
|
setIsoData,
|
|
|
|
setupTippy,
|
|
|
|
toast,
|
|
|
|
updateRegistrationApplicationRes,
|
|
|
|
wsClient,
|
|
|
|
wsSubscribe,
|
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
|
|
|
import { Paginator } from "../common/paginator";
|
|
|
|
import { RegistrationApplication } from "../common/registration-application";
|
|
|
|
|
|
|
|
enum UnreadOrAll {
|
|
|
|
Unread,
|
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RegistrationApplicationsState {
|
2023-01-04 16:56:24 +00:00
|
|
|
listRegistrationApplicationsResponse?: ListRegistrationApplicationsResponse;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-12-30 15:26:45 +00:00
|
|
|
unreadOrAll: UnreadOrAll;
|
2023-05-15 19:53:29 +00:00
|
|
|
page: number;
|
2021-12-30 15:26:45 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RegistrationApplications extends Component<
|
|
|
|
any,
|
|
|
|
RegistrationApplicationsState
|
|
|
|
> {
|
2023-01-04 16:56:24 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
private subscription?: Subscription;
|
|
|
|
state: RegistrationApplicationsState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-12-30 15:26:45 +00:00
|
|
|
unreadOrAll: UnreadOrAll.Unread,
|
2023-05-15 19:53:29 +00:00
|
|
|
page: 1,
|
2021-12-30 15:26:45 +00:00
|
|
|
loading: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
|
|
|
|
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
listRegistrationApplicationsResponse: this.isoData
|
|
|
|
.routeData[0] as ListRegistrationApplicationsResponse,
|
2022-09-22 15:03:35 +00:00
|
|
|
loading: false,
|
|
|
|
};
|
2021-12-30 15:26:45 +00:00
|
|
|
} else {
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (isBrowser()) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2021-12-30 15:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2023-06-05 21:31:12 +00:00
|
|
|
const mui = UserService.Instance.myUserInfo;
|
2023-01-04 16:56:24 +00:00
|
|
|
return mui
|
|
|
|
? `@${mui.local_user_view.person.name} ${i18n.t(
|
2022-11-09 19:53:07 +00:00
|
|
|
"registration_applications"
|
2023-01-04 16:56:24 +00:00
|
|
|
)} - ${this.state.siteRes.site_view.site.name}`
|
|
|
|
: "";
|
2021-12-30 15:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2021-12-30 15:26:45 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<Spinner large />
|
|
|
|
</h5>
|
|
|
|
) : (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12">
|
2021-12-30 15:26:45 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<h5 className="mb-2">{i18n.t("registration_applications")}</h5>
|
2021-12-30 15:26:45 +00:00
|
|
|
{this.selects()}
|
|
|
|
{this.applicationList()}
|
|
|
|
<Paginator
|
|
|
|
page={this.state.page}
|
|
|
|
onChange={this.handlePageChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
unreadOrAllRadios() {
|
|
|
|
return (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="btn-group btn-group-toggle flex-wrap mb-2">
|
2021-12-30 15:26:45 +00:00
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer
|
|
|
|
${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={UnreadOrAll.Unread}
|
|
|
|
checked={this.state.unreadOrAll == UnreadOrAll.Unread}
|
|
|
|
onChange={linkEvent(this, this.handleUnreadOrAllChange)}
|
|
|
|
/>
|
|
|
|
{i18n.t("unread")}
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={`btn btn-outline-secondary pointer
|
|
|
|
${this.state.unreadOrAll == UnreadOrAll.All && "active"}
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
value={UnreadOrAll.All}
|
|
|
|
checked={this.state.unreadOrAll == UnreadOrAll.All}
|
|
|
|
onChange={linkEvent(this, this.handleUnreadOrAllChange)}
|
|
|
|
/>
|
|
|
|
{i18n.t("all")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
selects() {
|
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="mr-3">{this.unreadOrAllRadios()}</span>
|
2021-12-30 15:26:45 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
applicationList() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const res = this.state.listRegistrationApplicationsResponse;
|
2023-01-04 16:56:24 +00:00
|
|
|
return (
|
|
|
|
res && (
|
2022-06-21 21:42:29 +00:00
|
|
|
<div>
|
|
|
|
{res.registration_applications.map(ra => (
|
|
|
|
<>
|
|
|
|
<hr />
|
|
|
|
<RegistrationApplication
|
|
|
|
key={ra.registration_application.id}
|
|
|
|
application={ra}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
)
|
|
|
|
);
|
2021-12-30 15:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
|
2023-05-15 19:53:29 +00:00
|
|
|
i.setState({ unreadOrAll: Number(event.target.value), page: 1 });
|
2021-12-30 15:26:45 +00:00
|
|
|
i.refetch();
|
|
|
|
}
|
|
|
|
|
2023-05-15 19:53:29 +00:00
|
|
|
handlePageChange(page: number) {
|
2021-12-30 15:26:45 +00:00
|
|
|
this.setState({ page });
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2023-06-05 21:31:12 +00:00
|
|
|
const promises: Promise<any>[] = [];
|
2021-12-30 15:26:45 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const auth = req.auth;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (auth) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const form: ListRegistrationApplications = {
|
2023-01-04 16:56:24 +00:00
|
|
|
unread_only: true,
|
2023-05-15 19:53:29 +00:00
|
|
|
page: 1,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
promises.push(req.client.listRegistrationApplications(form));
|
|
|
|
}
|
2021-12-30 15:26:45 +00:00
|
|
|
|
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
|
|
|
|
refetch() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
|
|
|
|
const auth = myAuth();
|
2023-01-04 16:56:24 +00:00
|
|
|
if (auth) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const form: ListRegistrationApplications = {
|
2023-01-04 16:56:24 +00:00
|
|
|
unread_only: unread_only,
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listRegistrationApplications(form)
|
|
|
|
);
|
|
|
|
}
|
2021-12-30 15:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: any) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const op = wsUserOp(msg);
|
2021-12-30 15:26:45 +00:00
|
|
|
console.log(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
return;
|
|
|
|
} else if (msg.reconnect) {
|
|
|
|
this.refetch();
|
|
|
|
} else if (op == UserOperation.ListRegistrationApplications) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const data = wsJsonToRes<ListRegistrationApplicationsResponse>(msg);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
listRegistrationApplicationsResponse: data,
|
2022-09-22 15:03:35 +00:00
|
|
|
loading: false,
|
|
|
|
});
|
2021-12-30 15:26:45 +00:00
|
|
|
window.scrollTo(0, 0);
|
|
|
|
} else if (op == UserOperation.ApproveRegistrationApplication) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const data = wsJsonToRes<RegistrationApplicationResponse>(msg);
|
2021-12-30 15:26:45 +00:00
|
|
|
updateRegistrationApplicationRes(
|
|
|
|
data.registration_application,
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.listRegistrationApplicationsResponse
|
2023-01-04 16:56:24 +00:00
|
|
|
?.registration_applications
|
2021-12-30 15:26:45 +00:00
|
|
|
);
|
2023-06-05 21:31:12 +00:00
|
|
|
const uacs = UserService.Instance.unreadApplicationCountSub;
|
2021-12-30 15:26:45 +00:00
|
|
|
// Minor bug, where if the application switches from deny to approve, the count will still go down
|
2023-05-15 19:53:29 +00:00
|
|
|
uacs.next(uacs.getValue() - 1);
|
2021-12-30 15:26:45 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|