diff --git a/frontend/src/ts/elements/alerts.ts b/frontend/src/ts/elements/alerts.ts index 6c0cf7522..f202b8f2b 100644 --- a/frontend/src/ts/elements/alerts.ts +++ b/frontend/src/ts/elements/alerts.ts @@ -1,5 +1,10 @@ +import formatDistanceToNowStrict from "date-fns/formatDistanceToNowStrict"; +import Ape from "../ape"; +import { Auth } from "../firebase"; + export function hide(): void { if (!$("#alertsPopupWrapper").hasClass("hidden")) { + setBellButtonColored(false); $("#alertsPopup").animate( { marginRight: "-10rem", @@ -31,8 +36,15 @@ export async function show(): Promise { 100, "easeOutCubic" ); - $("#alertsPopup .accountAlerts .list").html(` -
`); + + if (Auth.currentUser) { + $("#alertsPopup .accountAlerts").removeClass("hidden"); + $("#alertsPopup .accountAlerts .list").html(` +
`); + } else { + $("#alertsPopup .accountAlerts").addClass("hidden"); + } + $("#alertsPopupWrapper") .stop(true, true) .css("opacity", 0) @@ -43,14 +55,93 @@ export async function show(): Promise { }, 100, () => { - getAccountAlerts(); + if (Auth.currentUser) { + getAccountAlerts(); + } } ); } } async function getAccountAlerts(): Promise { - // + const inboxResponse = await Ape.users.getInbox(); + + $("#alertsPopup .accountAlerts .list").empty(); + + if (inboxResponse.status !== 200) { + // addNotification(, -1); + $("#alertsPopup .accountAlerts .list").html( + ` +
+ Error getting inbox: ${inboxResponse.message} +
+ ` + ); + return; + } + const inboxData = inboxResponse.data as { + inbox: MonkeyTypes.MonkeyMail[]; + maxMail: number; + }; + + if (inboxData.inbox.length === 0) { + $("#alertsPopup .accountAlerts .list").html(` +
+ Nothing to show +
+ `); + return; + } + + $("#alertsPopup .accountAlerts .title .right").text( + `${inboxData.inbox.length}/${inboxData.maxMail}` + ); + + const markAsRead = []; + + for (const ie of inboxData.inbox) { + if (!ie.read && ie.rewards.length == 0) { + markAsRead.push(ie.id); + } + + let rewardsString = ""; + + if (ie.rewards.length > 0) { + rewardsString = `
+ + ${ie.rewards.length} +
`; + } + + $("#alertsPopup .accountAlerts .list").append(` + +
+
+
${formatDistanceToNowStrict( + new Date(ie.timestamp) + )} ago
+
${ie.subject}
+
+ ${ie.body}\n\n${rewardsString} +
+
+ ${ + ie.rewards.length > 0 + ? `
` + : `` + } +
+
+
+ + `); + } + + if (markAsRead.length > 0) { + Ape.users.updateInbox({ + mailIdsToMarkRead: markAsRead, + }); + } } export function addPSA(message: string, level: number): void { @@ -110,6 +201,14 @@ export function addNotification( `); } +export function setBellButtonColored(tf: boolean): void { + if (tf) { + $("#top #menu .showAlerts").addClass("active"); + } else { + $("#top #menu .showAlerts").removeClass("active"); + } +} + $("#top #menu .showAlerts").on("click", () => { show(); }); @@ -119,3 +218,9 @@ $("#alertsPopupWrapper").on("mousedown", (e) => { hide(); } }); + +$(document).on("keydown", (e) => { + if (e.key === "Escape" && !$("#alertsPopupWrapper").hasClass("hidden")) { + hide(); + } +});