mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
useNotifications Hook
This commit is contained in:
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 loadSite = useCallback(() => {
|
||||
console.log(siteId)
|
||||
if (!siteId) return;
|
||||
setLoading(true);
|
||||
axios.get('/api/sites/get', {
|
||||
|
||||
Reference in New Issue
Block a user