-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppContext.js
More file actions
35 lines (29 loc) · 1.04 KB
/
AppContext.js
File metadata and controls
35 lines (29 loc) · 1.04 KB
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
import React, { createContext, useEffect, useState } from 'react';
import { ref as databaseRef, update } from 'firebase/database';
import { database } from './config';
export const AppContext = createContext();
export const AppProvider = ({ children, uid }) => {
const [friendList, setFriendList] = useState([]);
const [user, setUser] = useState({});
const userUid = uid;
const updateStatus = async () => {
const time = Date.now();
try {
const userRef = databaseRef(database, `Users/${uid}`);
await update(userRef, { LastSeen: time });
} catch (error) {
console.error("Error updating database: ", error);
}
};
useEffect(() => {
if (uid) {
const interval = setInterval(updateStatus, 12000);
return () => clearInterval(interval);
}
}, [uid]);
return (
<AppContext.Provider value={{ friendList, setFriendList, user, setUser, userUid }}>
{children}
</AppContext.Provider>
);
};