-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.js
149 lines (130 loc) · 3.82 KB
/
Context.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useState, useEffect } from "react";
import { auth, db } from "./firebase";
const Context = React.createContext();
function ContextProvider({ children }) {
const [user, setUser] = useState(null);
const [users, setUsers] = useState([]);
const [userGuest, setUserGuest] = useState(true);
/*SAVE rapidAPI CALLS for profile page. Once a user has loaded it, the data is saved in state -> */
const [profileWzData, setProfileWzData] = useState(null);
const [searchOk, setSearchOk] = useState(false);
/* for list page */
const [_profiles, _setProfiles] = useState([]);
const [listOfProfiles, setListOfProfiles] = useState([
{
userName: "schmetir",
img: "https://user-images.githubusercontent.com/17027312/134349999-06919dce-11f2-42b9-9c0c-2b27d8dcce51.jpeg",
},
{
userName: "nurrminator",
img: "https://photos.smugmug.com/photos/i-HGQDK9V/0/XL/i-HGQDK9V-XL.jpg",
},
{
userName: "BigMme930",
img: "https://photos.smugmug.com/photos/i-BS3QMBH/0/O/i-BS3QMBH-O.jpg",
},
{
userName: "Bengtbenny",
img: "https://user-images.githubusercontent.com/17027312/143914999-4b3362b9-259e-4fe0-9258-fff161b1c67a.jpeg",
},
]);
/* ...-> ok. but save firebase reads for now */
/* useEffect(() => {
const unsubscribe = db.collection("users").onSnapshot((snapshot) =>
setListOfProfiles(
snapshot.docs.map((doc) => ({
userName: doc.data().displayName,
img: doc.data().photoURL,
}))
)
);
return unsubscribe;
}, []); */
useEffect(() => {
const unsubscribe = db
.collection("users")
.onSnapshot((snapshot) =>
setUsers(snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
);
return unsubscribe;
}, []);
const [posts, setPosts] = useState([]);
useEffect(() => {
const unsubscribe = db
.collection("posts")
.orderBy("timestamp", "desc")
/* .limit(3) */
.onSnapshot((snapshot) =>
setPosts(
snapshot.docs.map((doc) => ({ ...doc.data(), postId: doc.id }))
)
);
return unsubscribe;
}, []);
const [lists, setLists] = useState([]);
useEffect(() => {
const unsubscribe = db
.collection("lists")
.orderBy("timestamp", "desc")
.onSnapshot((snapshot) =>
setLists(
snapshot.docs.map((doc) => ({ ...doc.data(), listId: doc.id }))
)
);
return unsubscribe;
}, []);
//user listener
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((authUser) => {
if (authUser) {
//user has logged in
//console.log(authUser);
setUser(authUser);
} else {
//user has logged out
setUser(null);
}
return () => {
unsubscribe();
};
});
}, [user]);
const [elementIdToScrollTo, setElementIdToScrollTo] = useState("");
const [loadingNotific, setLoadingNotific] = useState(false);
/* console.log(profile); */
return (
<Context.Provider
value={{
elementIdToScrollTo,
setElementIdToScrollTo,
loadingNotific,
setLoadingNotific,
/* for list page */
_profiles,
_setProfiles,
listOfProfiles,
setListOfProfiles,
userGuest,
setUserGuest,
//NOTE:user is the authUser(only:displayName, email, image), users is from the firestore with the complete info
user,
setUser,
users,
setUsers,
posts,
setPosts,
lists,
setLists,
/* for profile page */
/* save api calls👇 after 1 render the profile is always there, no need to call api again */
searchOk,
setSearchOk,
profileWzData,
setProfileWzData,
}}
>
{children}
</Context.Provider>
);
}
export { ContextProvider, Context };