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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
import { firebase, firestore } from '../firebase.js';
//Start Firbase Auth
export const AUTH_FAIL = 'AUTH_FAIL';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_SIGN_OUT = 'AUTH_SIGN_OUT';
export const CREATE_ACCOUNT = 'CREATE_ACCOUNT';
export const ADMIN_LISTENER = 'ADMIN_LISTENER';
export const IS_ADMIN = 'IS_ADMIN';
export const ADMIN_CONTROLS = 'ADMIN_CONTROLS';
export const UPDATE_ADMIN = 'UPDATE_ADMIN';
export const SET_USER_DATA = 'SET_USER_DATA';
const auth = firebase.auth();
export const createAccount = (_email, _password, divison) => (dispatch) => {
firebase.auth().createUserWithEmailAndPassword(_email, _password).then(() => {
dispatch(signIn(_email, _password, divison));
})
.catch((error) => {
// Handle Errors here.
alert(error.code + ": " + error.message);
});
}
export const signIn = (_email, _password, divison) => (dispatch) => {
auth.signInWithEmailAndPassword(_email, _password).then(() => {
var user = auth.currentUser;
/* User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
*/
dispatch(authSuccess(user));
if(divison) {
console.log(divison);
dispatch(setUserData(divison));
}
dispatch(snapshotHours())
dispatch(snapshotRegisteredCompetitions());
//Admin controls
if( user.uid === 'rxKROQAukzchWuueDLwA9c0YmsT2' || //Lucy Wood
user.uid === 'sAVjlnSAETaP5VtTKGhfBKHKeQF2' //Michael Foiani
)
{
dispatch(adminListener());
}
})
.catch((error) => {
dispatch(authFail(error.code));
});
}
export const setUserData = (_divison) => (dispatch, getState) => {
const uid = getState().firebase.uid;
var docRef = firestore.collection('users').doc(uid);
docRef.set({
hours: 0,
divison: _divison
}).catch((error) => {
console.log(error);
})
}
export const adminListener = () => (dispatch, getState) => {
document.onkeyup = function(e) {
if(e.altKey && e.which == 65) {
var docRef = firestore.collection('keys').doc('adminKey');
docRef.get().then((doc) => {
if(prompt('Enter admin password') == doc.data().password) {
dispatch(adminControls());
}
});
}
}
}
export const authFail = (errorCode) => {
alert(errorCode);
return {
type: AUTH_FAIL,
payload: false,
code: errorCode,
uid: null
}
}
export const authSuccess = (_user) => {
alert('Sign In Success');
return {
type: AUTH_SUCCESS,
payload: true,
code: "Success",
uid: _user.uid,
userEmail: _user.email
}
}
export const adminControls = () => (dispatch) => {
dispatch(updateAdmin());
dispatch(snapshotAdminRequests());
dispatch(snapshotAdminCompList());
}
export const updateAdmin = () => {
return {
type: UPDATE_ADMIN,
payload: true
}
}
export const signOut = () => (dispatch) => {
auth.signOut().then(() => {
dispatch(authSignOut());
});
}
export const authSignOut = () => {
return {
type: AUTH_SIGN_OUT,
payload: false,
code: "Signed Out User",
uid: "",
userEmail: "",
isAdmin: false,
requests: [],
compList: []
}
}
//End Firebase Auth
//Start Firebase Firestore
export const SNAPSHOT_HOURS = 'SNAPSHOT_HOURS';
export const UPDATE_HOURS = 'UPDATE_HOURS';
export const snapshotHours = () => (dispatch, getState) => {
const currentState = getState().firebase;
if(currentState.initialized) {
var totalHours;
var docRefUsers = firestore.collection('users').doc(currentState.uid);
var docRefReq = firestore.collection('requests').where('uid', '==', currentState.uid);
docRefUsers.onSnapshot((doc) => {
totalHours = doc.data().hours;
docRefReq.onSnapshot((query) => {
var requestedHours = 0;
query.forEach((docs) => {
requestedHours += docs.data().time;
});
dispatch(updateHours(totalHours, requestedHours));
});
});
}
}
export const updateHours = (hours, reqHours) => {
return {
type: 'UPDATE_HOURS',
approvedHours: hours,
requestedHours: reqHours
}
}
export const REGISTER_COMP = 'REGISTER_COMP';
export const SNAPSHOT_REGISTERED_COMPETITIONS = 'SNAPSHOT_REGISTERED_COMPETITIONS';
export const UPDATE_REGISTERED_COMPETITIONS = 'UPDATE_REGISTERED_COMPETITIONS';
export const SNAPSHOT_ADMIN_COMP_LIST = 'SNAPSHOT_ADMIN_COMP_LIST';
export const UPDATE_ADMIN_COMP_LIST = 'UPDATE_ADMIN_COMP_LIST';
export const registerComp = (compName) => (dispatch, getState) => {
var docRef = firestore.collection('competitions').doc(compName);
var uid = getState().firebase.uid;
var email = getState().firebase.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
});
} else {
docRef.set({
uids : [uid],
emails: [email]
});
}
});
}
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().firebase.uid)) {
registeredComps.push(doc.id);
}
});
dispatch(updateRegisteredCompetitions(registeredComps));
});
}
export const updateRegisteredCompetitions = (registeredComps) => {
return {
type: UPDATE_REGISTERED_COMPETITIONS,
payload: registeredComps
}
}
export const snapshotAdminCompList = () => (dispatch) => {
var docRef = firestore.collection('competitions');
docRef.onSnapshot((querySnapshot) => {
var compList = [];
querySnapshot.forEach((doc) => {
compList.push({
...doc.data(),
name: doc.id
});
});
dispatch(updateAdminCompList(compList));
});
}
export const updateAdminCompList = (compList) => {
return {
type: UPDATE_ADMIN_COMP_LIST,
payload: compList
}
}
export const REQUEST_HOURS = 'REQUEST_HOURS';
export const ADMIN_APPROVE_HOURS = 'ADMIN_APPROVE_HOURS';
export const ADMIN_REJECT_HOURS = 'ADMIN_REJECT_HOURS';
export const ADMIN_DELETE_REQUEST = 'ADMIN_DELETE_REQUEST';
export const SNAPSHOT_ADMIN_REQUESTS = 'SNAPSHOT_ADMIN_REQUESTS';
export const UPDATE_ADMIN_REQUESTS = 'UPDATE_ADMIN_REQUESTS';
export const requestHours = (_time, _trainee, _date) => (dispatch, getState) => {
var docRef = firestore.collection('requests');
const _uid = getState().firebase.uid;
const _email = getState().firebase.userEmail;
docRef.add({
time: _time,
trainee: _trainee,
day: _date,
uid: _uid,
email: _email
});
docRef = firestore.collection('users').doc(_uid);
docRef.get().then((doc) => {
if(doc.exists) {
docRef.set({
hours: doc.data().hours
});
}
});
}
export const snapshotAdminRequests = () => (dispatch) => {
var docRef = firestore.collection('requests');
docRef.onSnapshot((querySnapshot) => {
var requestList = [];
querySnapshot.forEach((doc) => {
requestList.push({
...doc.data(),
docId: doc.id
});
});
dispatch(updateAdminRequests(requestList));
});
}
export const updateAdminRequests = (requests) => {
return {
type: UPDATE_ADMIN_REQUESTS,
payload: requests
}
}
export const adminApproveHours = (_uid, _time, _id) => (dispatch) => {
var docRef = firestore.collection('users').doc(_uid);
docRef.get().then((doc) => {
if(doc.exists) {
docRef.update({
hours: (doc.data().hours + _time)
}).catch((error) => {
alert(error);
});
}
});
dispatch(adminDeleteRequest(_id));
}
export const adminRejectHours = (_id) => (dispatch) => {
dispatch(adminDeleteRequest(_id));
}
export const adminDeleteRequest = (_id) => (dispatch) => {
var docRef = firestore.collection('requests').doc(_id);
docRef.delete().then(() => {
//console.log('Deleted request with id ' + _id);
});
}
export const CREATE_FOURM_POST = 'CREATE_FOURM_POST';
export const SNAPSHOT_FOURM = 'SNAPSHOT_FOURM';
export const UPDATE_FOURM_POSTS = 'UPDATE_FOURM_POSTS';
export const createFourmPost = (_subject, _content) => (dispatch, getState) => {
var docRef = firestore.collection('posts');
const userEmail = getState().firebase.userEmail;
docRef.add({
email: userEmail,
subject: _subject,
content: _content
});
}
export const snapshotFourms = () => (dispatch) => {
var docRef = firestore.collection('posts');
docRef.onSnapshot((query) => {
var fourmPosts = [];
query.forEach((doc) => {
fourmPosts.push(doc.data());
});
dispatch(updateFourmPosts(fourmPosts));
});
}
export const updateFourmPosts = (_fourmPosts) => {
return {
type: UPDATE_FOURM_POSTS,
payload: _fourmPosts
}
}
//End Firebase Firestore
|