-
Notifications
You must be signed in to change notification settings - Fork 1
/
Firestore.jsx
124 lines (109 loc) · 2.93 KB
/
Firestore.jsx
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
import app from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
export const config = {
apiKey: "AIzaSyBz86p_Wdi_WBkLpcxhMaph4geiv69bcNc",
authDomain: "fadderuke-matnat.firebaseapp.com",
databaseURL: "https://fadderuke-matnat.firebaseio.com",
projectId: "fadderuke-matnat",
storageBucket: "fadderuke-matnat.appspot.com",
messagingSenderId: "909444728352",
appId: "1:909444728352:web:09ee2da490aafc00",
};
class Firebase {
constructor() {
app.initializeApp(config);
this.db = app.firestore();
this.auth = app.auth();
}
fetchEvents(setEventsCallback) {
this.db.collection("events2020").onSnapshot((snapshot) => {
const events = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
subEvents: [],
}));
setEventsCallback(events);
console.log("Events fetched successfully!");
});
}
fetchSubEvents(setSubeventsCallback) {
this.db.collection("subevents2020").onSnapshot((subSnapshot) => {
setSubeventsCallback(
subSnapshot.docs.map((d) => ({ id: d.id, ...d.data() }))
);
console.log("Sub-events fetched successfully!");
});
}
fetchGroupNames(setGroupNamesCallback) {
this.db.collection("groups2020").onSnapshot((snapshot) => {
const groupNames = snapshot.docs.map((doc) => {
return doc.data();
});
setGroupNamesCallback(groupNames);
}, console.log);
}
login(email, password, errorCallback) {
this.auth.signInWithEmailAndPassword(email, password).catch((error) => {
errorCallback(error);
});
}
logout() {
this.auth.signOut();
}
registerForAuthUpdates(callback, loadingCallback) {
this.auth.onAuthStateChanged((user) => {
if (user) {
callback(user);
loadingCallback(false);
} else {
callback(undefined);
loadingCallback(false);
}
});
}
fetchGroupsThatUserAdministrates(userId, callback) {
this.db
.collection("writePermissions")
.doc(userId)
.get()
.then((doc) => {
if (doc.exists) {
const groups = doc.data().groups;
if (groups === "all") {
callback(groups);
} else {
callback(groups);
}
}
});
}
addEvent(event, uid) {
if (event) {
return this.db.collection("events2020").add({ ...event, createdBy: uid });
}
}
removeEvent(event_id) {
if (event_id) {
return this.db.collection("events2020").doc(event_id).delete();
}
}
addSubEvent(event, uid) {
if (event) {
return this.db
.collection("subevents2020")
.add({ ...event, createdBy: uid });
}
}
removeSubEvent(event_id) {
if (event_id) {
return this.db.collection("subevents2020").doc(event_id).delete();
}
}
updateEvent(event) {
if (event) {
return this.db.collection("events2020").doc(event.id).set(event);
}
}
}
export default Firebase;