diff --git a/components/Navbar/parts/NotificationButton.js b/components/Navbar/parts/NotificationButton.js index daa9d470..3b4936d0 100644 --- a/components/Navbar/parts/NotificationButton.js +++ b/components/Navbar/parts/NotificationButton.js @@ -3,10 +3,15 @@ import ActiveIconButtonLink from "./ActiveIconButtonLink"; import { useEffect, useState } from "react"; import { getUserNotificationCount } from "@/lib/api"; +// import { useRouter } from "next/navigation"; +import { useSearchParams } from "next/navigation"; +import axios from "axios"; const NotificationButton = ({ user }) => { const [notificationCount, setNotificationCount] = useState(0); + const searchParams = useSearchParams(); + useEffect(() => { const fetchNotificationCount = async () => { const nc = await getUserNotificationCount({ @@ -15,15 +20,51 @@ const NotificationButton = ({ user }) => { offset: 0, }); - if (nc?.userNotifications?.count || nc?.userNotifications?.count===0) { + if (nc?.userNotifications?.count || nc?.userNotifications?.count === 0) { setNotificationCount(nc?.userNotifications?.count); } }; + const clearNotifications = async id => { + var data = JSON.stringify({ + id: id ? id : "*", + }); + var config = { + method: "post", + url: `${process.env.NEXT_PUBLIC_STRAPI_API_URL}/api/notificationsClear`, + headers: { + Authorization: `Bearer ${user.jwt}`, + "Content-Type": "application/json", + }, + data: data, + }; + // const loadingToastId = toast.loading("Sending verification email"); + + axios(config) + .then(function (response) { + setTimeout(() => { + fetchNotificationCount() + }, 10); + }) + .catch(function (error) { + console.log(error); + }); + + // location.reload() + }; + if (user?.isLoggedIn) { fetchNotificationCount(); } - }, [user?.isLoggedIn]); + if (user?.isLoggedIn && searchParams) { + // Check if there is a clearNotification query parameter in the router path + const clearNotification = searchParams?.get("clearNotification"); + if (clearNotification) { + clearNotifications(clearNotification); + } + } + + }, [user?.isLoggedIn, searchParams]); return (
{notification.post.title}:
@@ -52,7 +52,7 @@ const ClaimNotification = ({ notification }) => {
{notification.read == false || notification.read == "false" ? (
diff --git a/components/Notifications/NotificationCard/CreatorBadgeNotification.js b/components/Notifications/NotificationCard/CreatorBadgeNotification.js
index 0ce2e9df..6aae720c 100644
--- a/components/Notifications/NotificationCard/CreatorBadgeNotification.js
+++ b/components/Notifications/NotificationCard/CreatorBadgeNotification.js
@@ -20,7 +20,7 @@ const CreatorBadgeNotification = ({ notification }) => {
<>
You've been added as a creator of {" "}
{notification.post.title}
@@ -34,7 +34,7 @@ const CreatorBadgeNotification = ({ notification }) => {
{notification.post.title}:
@@ -52,7 +52,7 @@ const CreatorBadgeNotification = ({ notification }) => {
{notification.read == false || notification.read == "false" ? (
diff --git a/components/Notifications/NotificationCard/InterviewInviteBadge.js b/components/Notifications/NotificationCard/InterviewInviteBadge.js
index 7aed742e..a3d2130c 100644
--- a/components/Notifications/NotificationCard/InterviewInviteBadge.js
+++ b/components/Notifications/NotificationCard/InterviewInviteBadge.js
@@ -20,8 +20,8 @@ const InterviewInviteBadge = ({ notification }) => {
<>
You're invited to answer an interview article about {" "}
{notification.post.title}
@@ -34,7 +34,8 @@ const InterviewInviteBadge = ({ notification }) => {
Get featured in the newsletter for telling your story about
@@ -50,7 +51,7 @@ const InterviewInviteBadge = ({ notification }) => {