From b9d73266c06e8a0348fa8dbf6ee0686f7b6f8f2f Mon Sep 17 00:00:00 2001
From: Namit Bhutani
Date: Mon, 22 Jul 2024 22:15:00 +0530
Subject: [PATCH 1/6] feat: popup toast for unread notifications
---
frontend/src/About.tsx | 2 +-
frontend/src/components/announcements.tsx | 71 ++++++++++++++++++++---
2 files changed, 65 insertions(+), 8 deletions(-)
diff --git a/frontend/src/About.tsx b/frontend/src/About.tsx
index e91ffd72..6d9ebb75 100644
--- a/frontend/src/About.tsx
+++ b/frontend/src/About.tsx
@@ -58,7 +58,7 @@ function About() {
. We thank Arunachala AM, Jason Goveas, Anurav Garg, Dharanikanth
Reddy, Karthik Prakash, Shreyash Dash, Kovid Lakhera, Palaniappan R,
- Meghraj Goswami, Soumitra Shewale, Kishan Abijay, Namit Bhutani,
+ Meghraj Goswami, Soumitra Shewale, Kishan Abijay, Namit Bhutani,
Chaitanya Keyal and Samir Khairati for this rewrite of ChronoFactorem.
diff --git a/frontend/src/components/announcements.tsx b/frontend/src/components/announcements.tsx
index f2e26a8c..617adbb3 100644
--- a/frontend/src/components/announcements.tsx
+++ b/frontend/src/components/announcements.tsx
@@ -1,3 +1,4 @@
+import { useState, useEffect } from "react";
import {
Dialog,
DialogContent,
@@ -5,9 +6,9 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
+import { useToast } from "@/components/ui/use-toast";
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
-
import { DialogTrigger } from "@radix-ui/react-dialog";
import { Megaphone } from "lucide-react";
import type { z } from "zod";
@@ -29,6 +30,37 @@ function Announcements() {
queryFn: fetchAnnouncements,
});
+ const { toast } = useToast();
+
+ const [readAnnouncements, setReadAnnouncements] = useState(() => {
+ const storedReadAnnouncements = localStorage.getItem("readAnnouncements");
+ return storedReadAnnouncements ? JSON.parse(storedReadAnnouncements) : [];
+ });
+
+ useEffect(() => {
+ localStorage.setItem(
+ "readAnnouncements",
+ JSON.stringify(readAnnouncements),
+ );
+ }, [readAnnouncements]);
+
+ useEffect(() => {
+ const unreadAnnouncements = announcements?.filter(
+ (announcement) => !readAnnouncements.includes(announcement.id),
+ );
+
+ if (unreadAnnouncements && unreadAnnouncements.length > 0) {
+ toast({
+ title: "New Announcements",
+ description: `You have ${unreadAnnouncements.length} unread announcement(s).`,
+ });
+ }
+ }, [announcements, readAnnouncements, toast]);
+
+ const markAsRead = (id: string) => {
+ setReadAnnouncements((prev) => [...prev, id]);
+ };
+
return (