fix error

This commit is contained in:
Alec Armbruster 2023-06-26 14:09:16 -04:00
parent 1830c3c1ef
commit fef4af1fcf
No known key found for this signature in database
GPG key ID: 52BC7C84E960FD1B

View file

@ -1,4 +1,5 @@
import { myAuth } from "@utils/app"; import { myAuth } from "@utils/app";
import { isBrowser } from "@utils/browser";
import { poll } from "@utils/helpers"; import { poll } from "@utils/helpers";
import { amAdmin } from "@utils/roles"; import { amAdmin } from "@utils/roles";
import { import {
@ -11,27 +12,27 @@ import { HttpService, UserService } from "../services";
import { RequestState } from "../services/HttpService"; import { RequestState } from "../services/HttpService";
export class InboxService { export class InboxService {
#unreadInboxCountRes: RequestState<GetUnreadCountResponse>; static #_instance: InboxService;
#unreadReportCountRes: RequestState<GetReportCountResponse>; unreadInboxCountRes: RequestState<GetUnreadCountResponse>;
#unreadApplicationCountRes: RequestState<GetUnreadRegistrationApplicationCountResponse>; unreadReportCountRes: RequestState<GetReportCountResponse>;
unreadApplicationCountRes: RequestState<GetUnreadRegistrationApplicationCountResponse>;
static #instance: InboxService;
private constructor() { private constructor() {
this.#unreadInboxCountRes = { state: "empty" }; this.unreadInboxCountRes = { state: "empty" };
this.#unreadReportCountRes = { state: "empty" }; this.unreadReportCountRes = { state: "empty" };
this.#unreadApplicationCountRes = { state: "empty" }; this.unreadApplicationCountRes = { state: "empty" };
this.startPoll(); this.startPoll();
this.fetchUnreadCounts();
} }
static get #Instance() { static get #Instance() {
return this.#instance ?? (this.#instance = new this()); return this.#_instance ?? (this.#_instance = new this());
} }
unreadInboxCount(): number { unreadInboxCount(): number {
if (this.#unreadInboxCountRes.state === "success") { if (this.unreadInboxCountRes.state === "success") {
const { data } = this.#unreadInboxCountRes; const { data } = this.unreadInboxCountRes;
return data.replies + data.mentions + data.private_messages; return data.replies + data.mentions + data.private_messages;
} else { } else {
return 0; return 0;
@ -39,12 +40,12 @@ export class InboxService {
} }
public static get unreadInboxCount(): number { public static get unreadInboxCount(): number {
return this.#instance.unreadInboxCount(); return this.#Instance.unreadInboxCount();
} }
unreadReportCount(): number { unreadReportCount(): number {
if (this.#unreadReportCountRes.state === "success") { if (this.unreadReportCountRes.state === "success") {
const { data } = this.#unreadReportCountRes; const { data } = this.unreadReportCountRes;
return ( return (
data.post_reports + data.post_reports +
data.comment_reports + data.comment_reports +
@ -56,19 +57,19 @@ export class InboxService {
} }
public static get unreadReportCount(): number { public static get unreadReportCount(): number {
return this.#instance.unreadReportCount(); return this.#Instance.unreadReportCount();
} }
unreadApplicationCount(): number { unreadApplicationCount(): number {
if (this.#unreadApplicationCountRes.state === "success") { if (this.unreadApplicationCountRes.state === "success") {
return this.#unreadApplicationCountRes.data.registration_applications; return this.unreadApplicationCountRes.data.registration_applications;
} else { } else {
return 0; return 0;
} }
} }
public static get unreadApplicationCount(): number { public static get unreadApplicationCount(): number {
return this.#instance.unreadApplicationCount(); return this.#Instance.unreadApplicationCount();
} }
get isModerator(): boolean { get isModerator(): boolean {
@ -78,6 +79,10 @@ export class InboxService {
} }
startPoll() { startPoll() {
if (!isBrowser()) {
return;
}
poll(async () => { poll(async () => {
if (window.document.visibilityState === "hidden") { if (window.document.visibilityState === "hidden") {
return; return;
@ -91,18 +96,18 @@ export class InboxService {
const auth = myAuth(); const auth = myAuth();
if (auth) { if (auth) {
this.#unreadInboxCountRes = await HttpService.client.getUnreadCount({ this.unreadInboxCountRes = await HttpService.client.getUnreadCount({
auth, auth,
}); });
if (this.isModerator) { if (this.isModerator) {
this.#unreadReportCountRes = await HttpService.client.getReportCount({ this.unreadReportCountRes = await HttpService.client.getReportCount({
auth, auth,
}); });
} }
if (amAdmin()) { if (amAdmin()) {
this.#unreadApplicationCountRes = this.unreadApplicationCountRes =
await HttpService.client.getUnreadRegistrationApplicationCount({ await HttpService.client.getUnreadRegistrationApplicationCount({
auth, auth,
}); });