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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
import { firestore, fireStorage } from '../firebase.js';
export const UPDATE_DIVISON = 'UPDATE_DIVISON';
export const UPDATE_HOURS = 'UPDATE_HOURS';
export const UPDATE_REGISTERED_COMPETITIONS = 'UPDATE_REGISTERED_COMPETITIONS';
export const UPDATE_FORUM_POSTS = 'UPDATE_FORUM_POSTS';
export const UPDATE_NOTIFICATION = 'UPDATE_NOTIFICATION';
export const updateDivison = (divison) => {
return {
type: UPDATE_DIVISON,
payload: divison
}
}
export const updateHours = (hours, reqHours) => {
return {
type: UPDATE_HOURS,
approvedHours: hours,
requestedHours: reqHours
}
}
export const updateRegisteredCompetitions = (registeredComps) => {
return {
type: UPDATE_REGISTERED_COMPETITIONS,
payload: registeredComps
}
}
export const updateForumPosts = (_forumPosts) => {
return {
type: UPDATE_FORUM_POSTS,
payload: _forumPosts
}
}
export const updateNotification = (status) => {
return {
type: UPDATE_NOTIFICATION,
payload: status
}
}
//Middleware to dispatches
export const fetchDivison = () => (dispatch, getState) => {
const uid = getState().firebaseAuth.uid;
var docRef = firestore.collection('users').doc(uid);
docRef.get().then((doc) => {
dispatch(updateDivison(doc.data().divison));
});
}
export const snapshotHours = () => (dispatch, getState) => {
const currentAuthState = getState().firebaseAuth;
if(currentAuthState.signedIn) {
var totalHours;
var docRefUsers = firestore.collection('users').doc(currentAuthState.uid);
var docRefReq = firestore.collection('requests').where('uid', '==', currentAuthState.uid);
docRefUsers.onSnapshot((doc) => {
totalHours = doc.data().hours;
docRefReq.onSnapshot((query) => {
if(getState().firebaseFirestore.isAdminUpdate) {
alert("One of your hours requests has been approved or denied.\nGo to dashboard to see changes.")
}
var requestedHours = 0;
query.forEach((docs) => {
requestedHours += docs.data().time;
});
dispatch(updateHours(totalHours, requestedHours));
dispatch(updateNotification(true));
});
});
}
}
export const snapshotRegisteredCompetitions = () => (dispatch, getState) => {
var docRef = firestore.collection('competitions');
docRef.onSnapshot((querySnapshot) => {
var registeredComps = [];
querySnapshot.forEach((doc) => {
if(doc.exists && doc.data().uids.includes(getState().firebaseAuth.uid)) {
registeredComps.push(doc.id);
}
});
dispatch(updateRegisteredCompetitions(registeredComps));
});
}
export const snapshotForums = () => (dispatch) => {
var docRef = firestore.collection('posts');
docRef.onSnapshot((query) => {
var forumPosts = [];
query.forEach((doc) => {
forumPosts.push({
...doc.data(),
postId: doc.id
});
});
dispatch(updateForumPosts(forumPosts));
});
}
//Do not dipatch to store, only update firebaseFirestore
export const setUserData = (_divison) => (dispatch, getState) => {
var _uid = getState().firebaseAuth.uid;
var username = getState().firebaseAuth.userEmail.replace('@communityschoolnaples.org', '');
firestore.collection('users').doc(_uid)
.set({
hours: 0,
divison: _divison,
username
}).catch((error) => {
console.log(error);
})
}
export const requestHours = (_time, _trainee, _location, _subject, _date, _pictureName) => (dispatch, getState) => {
if(getState().app.offline) {
alert("Failed to create hour request. Must have internet connection.")
} else {
dispatch(updateNotification(false));
var docRef = firestore.collection('requests');
const _uid = getState().firebaseAuth.uid;
const _email = getState().firebaseAuth.userEmail;
const _path = 'requests/' + _uid + '/' + _pictureName;
var fireStorageRef = fireStorage.ref().child(_path);
fireStorageRef.getDownloadURL().then((url) => {
docRef.add({
time: _time,
trainee: _trainee,
location: _location,
subject: _subject,
day: _date,
imgUrl: url,
path: _path,
uid: _uid,
email: _email
}).then(()=> {
alert("Successfuly sent hours request for " + _time*60 + " minutes.\n"
+ "If it is approved, it will update on your dashboard.");
}).catch((error) => {
alert(error);
});
});
}
}
export const registerComp = (compName) => (dispatch, getState) => {
if(getState().app.offline) {
alert("Failed to register. Please establish internet connection.")
} else {
var docRef = firestore.collection('competitions').doc(compName);
var uid = getState().firebaseAuth.uid;
var email = getState().firebaseAuth.userEmail;
docRef.get().then((doc) => {
if(doc.exists) {
var uidArr = doc.data().uids;
var emailArr = doc.data().emails;
uidArr.push(uid);
emailArr.push(email);
docRef.set({
uids: uidArr,
emails: emailArr
}).then(()=> {
alert("Successfuly registered for " + compName.replace(/[0-9]/g, '').replace('_', ' ') + ".");
}).catch((error) => {
alert(error);
});
} else {
docRef.set({
uids : [uid],
emails: [email]
}).then(()=> {
alert("Successfuly registered for " + compName.replace(/[0-9]/g, '').replace('_', ' ') + ".");
}).catch((error) => {
alert(error);
});
}
}).catch((error) => {
alert(error);
});
}
}
export const createForumPost = (_subject, _content) => (dispatch, getState) => {
if(getState().app.offline) {
alert("Failed to create forum post. Please establish internet connection.")
} else {
var docRef = firestore.collection('posts');
const userEmail = getState().firebaseAuth.userEmail;
docRef.add({
email: userEmail,
subject: _subject,
content: _content,
day: new Date()
}).then(()=> {
alert("Successfuly posted forum with subject " + _subject + ".");
}).catch((error) => {
alert(error);
});
}
}
export const createComment = (postId, content) => (dispatch, getState) => {
if(getState().app.offline) {
alert("Failed to create comment post. Please establish internet connection.")
} else {
var docRef = firestore.collection('posts').doc(postId);
docRef.get().then((doc) => {
var comments = doc.data().comments ? doc.data().comments : [];
comments.push({
content,
user : getState().firebaseAuth.userEmail.replace('@communityschoolnaples.org', ''),
date: new Date()
});
docRef.update({
comments
});
})
.catch((error) => {
alert(error);
});
}
}
//TODO: Make it per year, not hard coded...
export const archiveClass2019 = () => (dispatch, getState) => {
if(getState().app.offline) {
alert('Failed to archive class. Please establish internet connection.');
} else {
var docRef = firestore.collection('users');
docRef.get().then((oldUsers) => {
oldUsers.forEach((user) => {
var newPath = firestore.collection('archived').doc('2019').collection('users');
newPath.doc(user.id).set(user.data());
if(user.data().username && user.data().username.includes('2019')) {
console.log(user.data().username, user.id);
docRef.doc(user.id).delete();
}
docRef.doc(user.id).update({hours: 0});
});
});
}
}
export const archiveCompetitions2019 = () => (dispatch, getState) => {
if(getState().app.offline) {
alert('Failed to archive competitions. Please establish internet connection.');
} else {
var docRef = firestore.collection('competitions');
docRef.get().then((oldCompetitions) => {
oldCompetitions.forEach((comp) => {
console.log(comp.id, comp.data());
var newPath = firestore.collection('archived').doc('2019').collection('competitions');
newPath.doc(comp.id).set(comp.data());
//This will delete the document in its old place...
docRef.doc(comp.id).delete();
});
});
}
}
|