mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
useNotifications Hook
This commit is contained in:
parent
913dd7dd63
commit
ddc88796d2
11
app/api/notifications/get/route.ts
Normal file
11
app/api/notifications/get/route.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -69,7 +69,7 @@ export async function GET(request: NextRequest) {
|
|||||||
id: String(network.id),
|
id: String(network.id),
|
||||||
siteId: String(network.siteId)
|
siteId: String(network.siteId)
|
||||||
})) || []
|
})) || []
|
||||||
} as Site;
|
} as unknown as Site;
|
||||||
});
|
});
|
||||||
const total = filteredSites.length;
|
const total = filteredSites.length;
|
||||||
|
|
||||||
|
|||||||
42
hooks/useNotifications.ts
Normal file
42
hooks/useNotifications.ts
Normal 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 };
|
||||||
|
}
|
||||||
@ -22,7 +22,6 @@ const useSite = () => {
|
|||||||
const [sites, setSites] = useState([]);
|
const [sites, setSites] = useState([]);
|
||||||
|
|
||||||
const loadSite = useCallback(() => {
|
const loadSite = useCallback(() => {
|
||||||
console.log(siteId)
|
|
||||||
if (!siteId) return;
|
if (!siteId) return;
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
axios.get('/api/sites/get', {
|
axios.get('/api/sites/get', {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user