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(snapshotHours()) dispatch(snapshotRegisteredCompetitions()); }) .catch((error) => { dispatch(authFail(error.code)); }); } 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 signOut = () => (dispatch) => { auth.signOut().then(() => { dispatch(authSignOut()); }); } export const authSignOut = () => { return { type: AUTH_SIGN_OUT, payload: false, code: "Signed Out User", uid: "", userEmail: "" } } //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 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 }); } else { docRef.set({ uids : [uid] }); } }); } 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 REQUEST_HOURS = 'REQUEST_HOURS'; export const requestHours = (_time, _trainee, _date) => (dispatch, getState) => { var docRef = firestore.collection('requests'); var _uid = getState().firebase.uid; docRef.add({ time: _time, trainee: _trainee, date: _date, uid: _uid }); docRef = firestore.collection('users').doc(_uid); docRef.get().then((doc) => { if(doc.exists) { docRef.set({ hours: doc.data().hours, requestedHours: doc.data().requestedHours + _time }); } }); } //End Firebase Firestore