diff --git a/app/api/notifications/test/get/route.ts b/app/api/notifications/test/get/route.ts new file mode 100644 index 0000000..07f24d0 --- /dev/null +++ b/app/api/notifications/test/get/route.ts @@ -0,0 +1,33 @@ +import { NextRequest, NextResponse } from "next/server"; +import prisma from "@/app/prisma"; +import { z } from "zod/v4"; + +const schema = z.object({ + notificationTestId: z.string() +}) + +export async function GET(request: NextRequest) { + try { + const searchParams = request.nextUrl.searchParams; + const notificationTestId = schema.parse({ notificationTestId: searchParams.get('notificationTestId') }); + + if(!notificationTestId) { + return NextResponse.json({ error: "Notification test ID is required" }, { status: 400 }); + } + + const notificationTest = await prisma.notificationTest.findUnique({ + where: { id: parseInt(notificationTestId.notificationTestId) } + }) + + if(!notificationTest) { + return NextResponse.json({ error: "Notification test not found" }, { status: 404 }); + } + + return NextResponse.json({ notificationTest }, { status: 200 }); + } catch (error) { + if(error instanceof z.ZodError) { + return NextResponse.json({ error: error.issues[0].message }, { status: 400 }); + } + return NextResponse.json({ error: "Internal server error" }, { status: 500 }); + } +} \ No newline at end of file diff --git a/components/cards/settings/NotificationSettings.tsx b/components/cards/settings/NotificationSettings.tsx index 954d574..07ad330 100644 --- a/components/cards/settings/NotificationSettings.tsx +++ b/components/cards/settings/NotificationSettings.tsx @@ -11,11 +11,13 @@ import Loading from '@/components/Loading'; export const NotificationSettings = ({ onError, onSuccess }: { onError: (message: string) => void, onSuccess: (message: string) => void }) => { const { loadNotifications, notifications, loading, testNotification } = useNotifications(); const [deleteNotificationId, setDeleteNotificationId] = useState(null); + const [notificationTestId, setNotificationTestId] = useState(null); const testNotificationHandler = async (notificationProviderId: number) => { (document.getElementById('test_notification') as HTMLDialogElement)?.showModal(); - await testNotification(notificationProviderId); + const notificationTest = await testNotification(notificationProviderId); + setNotificationTestId(notificationTest.id); } return ( @@ -79,7 +81,9 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message {deleteNotificationId && ( )} - + {notificationTestId && ( + + )} ); diff --git a/components/dialogues/TestNotification.tsx b/components/dialogues/TestNotification.tsx index c00df93..3612434 100644 --- a/components/dialogues/TestNotification.tsx +++ b/components/dialogues/TestNotification.tsx @@ -1,6 +1,22 @@ -import { Bell, CheckCircle2, Clock } from "lucide-react" +import { Bell, CheckCircle2, Clock, X } from "lucide-react" +import useNotifications from "@/hooks/useNotifications"; +import { useState } from "react"; +import { useEffect } from "react"; + +interface TestNotificationProps { + notificationTestId: number; +} + +export default function TestNotification({ notificationTestId }: TestNotificationProps) { + + const { getNotificationTest, notificationTest } = useNotifications(); + + + const checkNotificationTest = async () => { + console.log(notificationTestId); + await getNotificationTest(notificationTestId); + } -export default function TestNotification() { return (
@@ -25,21 +41,40 @@ export default function TestNotification() {

-
Status: + {!notificationTest?.sent && !notificationTest?.success && (
- Pending -
+ Pending +
+ )} + {notificationTest?.sent && !notificationTest?.success && ( +
+ + Failed +
+ )} + {notificationTest?.sent && notificationTest?.success && ( +
+ + Success +
+ )}
+
+
+ +
+
diff --git a/hooks/useNotifications.ts b/hooks/useNotifications.ts index 012fad0..a60ff89 100644 --- a/hooks/useNotifications.ts +++ b/hooks/useNotifications.ts @@ -1,9 +1,17 @@ import { useState, useEffect, useCallback } from "react"; import axios from "axios"; +interface NotificationTest { + id: number; + notificationProviderId: number; + sent: boolean; + success: boolean; +} + const useNotifications = () => { const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(false); + const [notificationTest, setNotificationTest] = useState(null); const loadNotifications = useCallback(() => { setLoading(true); @@ -51,13 +59,26 @@ const useNotifications = () => { }); } + const getNotificationTest = (notificationTestId: number): Promise | string => { + return axios.get('/api/notifications/test/get', { params: { notificationTestId } }) + .then((response) => { + setNotificationTest(response.data.notificationTest); + return response.data.notificationTest; + }) + .catch(err => { + throw err.response?.data?.error || 'An error occurred'; + }); + } + return { notifications, loading, addNotification, deleteNotification, loadNotifications, - testNotification + testNotification, + getNotificationTest, + notificationTest }; }