-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationsContext.tsx
97 lines (82 loc) · 2.81 KB
/
NotificationsContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { createContext, useContext, useEffect, useState } from "react";
import { DeleteANotification, GetNotifications, UpdateNotificationReadState } from "../firebase/utils";
import {useAuth} from './AuthContext'
// import { FieldValues, SubmitHandler, useForm } from "react-hook-form";
interface NotificationsStore {
notifications: [];
noNotifs: boolean;
unreadNotifsLen: number;
fetchNotifications: () => Promise<void>;
deleteNotification: (notifID: string) => Promise<void>;
updateNotificationReadState: (notifID: string) => Promise<void>;
}
// @ts-ignore
const NotificationsContext = createContext<NotificationsStore>()
const NotificationsContextProvider = ({ children }) => {
const [notifications, setNotifications] = useState([]);
const [noNotifs, setNoNotifs] = useState(false);
const [unreadNotifsLen,setunreadNotifsLen] = useState(notifications.length)
const [updateLoading, setUpdateLoading] = useState(false);
const [deleteLoading, setDeleteLoading] = useState(false);
const {user} = useAuth()
const fetchNotifications = async (uid) => {
try {
const data = await GetNotifications(uid);
if (data.length <= 0) {
setNoNotifs(true);
return;
}
setNotifications(data.sort((a,b) => b.timeStamp - a.timeStamp));
setNoNotifs(false);
} catch (error) {
console.error('Error fetching notifications:', error);
}
}
const deleteNotification = async (notifID) => {
try {
setDeleteLoading(true)
await DeleteANotification(notifID);
} catch (error) {
console.log(error)
}finally {
setDeleteLoading(false)
}
}
const updateNotificationReadState = async (notifID) => {
try {
setUpdateLoading(true);
// console.log("call for update notification")
await UpdateNotificationReadState(notifID);
} catch (error) {
}finally{
setUpdateLoading(false);
}
}
console.log(updateLoading)
// console.log("notification context")
useEffect(() => {
if(user){
fetchNotifications(user.uid);
}
}, [user,updateLoading,deleteLoading])
// calculate length of unread notifications
useEffect(() => {
const unreadNotifs = notifications.filter((item) => !item.read);
setunreadNotifsLen(unreadNotifs.length);
}, [notifications]);
console.log(notifications)
return (
// @ts-ignore
<NotificationsContext.Provider value={{ notifications, noNotifs, unreadNotifsLen,updateNotificationReadState, deleteNotification }}>
{children}
</NotificationsContext.Provider>
)
}
export default NotificationsContextProvider
export const useNotifications = () => {
const notifications = useContext(NotificationsContext)
if (notifications === undefined) {
throw new Error('useNotifications must be used within a NotificationsContextProvider');
}
return notifications;
}