useNotifications Hook

This commit is contained in:
headlesdev 2025-05-24 23:03:10 +02:00
parent 913dd7dd63
commit ddc88796d2
4 changed files with 54 additions and 2 deletions

View File

@ -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 });
}
}

View File

@ -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;

42
hooks/useNotifications.ts Normal file
View File

@ -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> | 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 };
}

View File

@ -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', {