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
|
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';
const auth = firebase.auth();
export const signIn = (_email, _password) => (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));
dispatch(getHours())
dispatch(pullRegisteredCompetitions());
})
.catch((error) => {
dispatch(authFail(error.code));
});
}
export const authFail = (errorCode) => {
return {
type: AUTH_FAIL,
payload: false,
code: errorCode,
uid: null
}
}
export const authSuccess = (_user) => {
alert('authSuccess');
return {
type: AUTH_SUCCESS,
payload: true,
code: "Success",
uid: _user.uid
}
}
export const signOut = () => (dispatch) => {
auth.signOut().then(() => {
dispatch(authSignOut());
});
}
export const authSignOut = () => {
return {
type: AUTH_SIGN_OUT,
payload: false,
code: "Signed Out User",
uid: null
}
}
//End Firebase Auth
//Start Firebase Firestore
export const GET_HOURS = 'GET_HOURS';
export const UPDATE_HOURS = 'UPDATE_HOURS';
export const getHours = () => (dispatch, getState) => {
const currentState = getState().firebase;
alert('ran');
if(currentState.initialized) {
var docRef = firestore.collection('users').doc(currentState.uid);
docRef.onSnapshot((doc) => {
dispatch(updateHours(doc.data().hours));
});
}
}
export const updateHours = (hours) => {
return {
type: 'UPDATE_HOURS',
payload: hours
}
}
export const REGISTER_COMP = 'REGISTER_COMP';
export const PULL_REGISTERED_COMPETITIONS = 'PULL_REGISTERED_COMPETITIONS';
export const UPDATE_REGISTERED_COMPETITIONS = 'UPDATE_REGISTERED_COMPETITIONS';
export const registerComp = (compName) => (dispatch, getState) => {
var docRef = firestore.collection('competitions').doc(compName);
var uid = getState().firebase.uid;
docRef.get().then((doc) => {
if(doc.exists) {
var uidArr = doc.data().uids;
uidArr.push(uid);
docRef.set({
uids: uidArr
});
dispatch(pullRegisteredCompetitions());
} else {
docRef.set({
uids : [uid]
}).then(() => {
dispatch(pullRegisteredCompetitions());
});
}
});
}
export const pullRegisteredCompetitions = () => (dispatch, getState) =>{
var registeredComps = [];
var docRef = firestore.collection('competitions');
docRef.get().then((query) => {
query.forEach((doc) => {
if(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
}
}
//End Firebase Firestore
|