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