From ddc88796d23dce331976d3276b29b78203815b92 Mon Sep 17 00:00:00 2001 From: headlesdev Date: Sat, 24 May 2025 23:03:10 +0200 Subject: [PATCH] useNotifications Hook --- app/api/notifications/get/route.ts | 11 ++++++++ app/api/sites/get_all/route.ts | 2 +- hooks/useNotifications.ts | 42 ++++++++++++++++++++++++++++++ hooks/useSites.ts | 1 - 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/api/notifications/get/route.ts create mode 100644 hooks/useNotifications.ts diff --git a/app/api/notifications/get/route.ts b/app/api/notifications/get/route.ts new file mode 100644 index 0000000..46454ac --- /dev/null +++ b/app/api/notifications/get/route.ts @@ -0,0 +1,11 @@ +import { NextRequest, NextResponse } from "next/server"; +import prisma from "@/app/prisma"; + +export async function GET(request: NextRequest) { + try { + const notifications = await prisma.notificationProvider.findMany(); + return NextResponse.json({ notifications }); + } catch (error: any) { + return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); + } +} \ No newline at end of file diff --git a/app/api/sites/get_all/route.ts b/app/api/sites/get_all/route.ts index 839a0e9..e3fda42 100644 --- a/app/api/sites/get_all/route.ts +++ b/app/api/sites/get_all/route.ts @@ -69,7 +69,7 @@ export async function GET(request: NextRequest) { id: String(network.id), siteId: String(network.siteId) })) || [] - } as Site; + } as unknown as Site; }); const total = filteredSites.length; diff --git a/hooks/useNotifications.ts b/hooks/useNotifications.ts new file mode 100644 index 0000000..27f112c --- /dev/null +++ b/hooks/useNotifications.ts @@ -0,0 +1,42 @@ +import { useState, useEffect, useCallback } from "react"; +import axios from "axios"; + +const useNotifications = () => { + const [notifications, setNotifications] = useState([]); + const [loading, setLoading] = useState(false); + + const loadNotifications = useCallback(() => { + setLoading(true); + axios.get('/api/notifications/get').then((response) => { + setNotifications(response.data.notifications); + setLoading(false); + }); + }, []); + + useEffect(() => { + loadNotifications(); + }, [loadNotifications]); + + const addNotification = (name: string, type: string, config: string): Promise | string => { + if(name.length < 3) { + return 'Notification name must be at least 3 characters long'; + } + + return axios.post('/api/notifications/add', { name, type, config }) + .then((response) => { + return response.data.notification; + }); + }; + + const deleteNotification = (notificationId: number) => { + axios.delete('/api/notifications/delete', { params: { notificationId } }) + .then(() => { + return "Notification deleted successfully"; + }) + .catch(err => { + throw err.response?.data?.error || 'An error occurred'; + }); + } + + return { notifications, loading, addNotification, deleteNotification }; +} \ No newline at end of file diff --git a/hooks/useSites.ts b/hooks/useSites.ts index 0e5cae2..f61b7d3 100644 --- a/hooks/useSites.ts +++ b/hooks/useSites.ts @@ -22,7 +22,6 @@ const useSite = () => { const [sites, setSites] = useState([]); const loadSite = useCallback(() => { - console.log(siteId) if (!siteId) return; setLoading(true); axios.get('/api/sites/get', {