diff options
-rw-r--r-- | src/actions/firebase.js | 38 | ||||
-rw-r--r-- | src/actions/firebaseAuth.js | 83 | ||||
-rw-r--r-- | src/actions/firebaseFirestore.js | 137 | ||||
-rw-r--r-- | src/reducers/firebaseFirestore.js | 56 |
4 files changed, 228 insertions, 86 deletions
diff --git a/src/actions/firebase.js b/src/actions/firebase.js index 6dca3d2..d93e227 100644 --- a/src/actions/firebase.js +++ b/src/actions/firebase.js @@ -60,44 +60,6 @@ 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) => { diff --git a/src/actions/firebaseAuth.js b/src/actions/firebaseAuth.js index 9783d8a..c1bcd8a 100644 --- a/src/actions/firebaseAuth.js +++ b/src/actions/firebaseAuth.js @@ -48,27 +48,22 @@ export const signIn = (_email, _password, divison) => (dispatch) => { } -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 signOut = () => (dispatch) => { + auth.signOut().then(() => { + dispatch(authSignOut()); + }); } -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 authSignOut = () => { + return { + type: AUTH_SIGN_OUT, + payload: false, + code: "Signed Out User", + uid: "", + userEmail: "", + isAdmin: false, + requests: [], + compList: [] } } @@ -82,7 +77,7 @@ export const authFail = (errorCode) => { } export const authSuccess = (_user) => { - alert('Sign In Success'); + alert('Success. Checkout the other pages to see what you can do!'); return { type: AUTH_SUCCESS, payload: true, @@ -91,19 +86,30 @@ export const authSuccess = (_user) => { } } -export const fetchDivison = () => (dispatch, getState) => { +//End firebase auth + + +export const setUserData = (_divison) => (dispatch, getState) => { const uid = getState().firebase.uid; var docRef = firestore.collection('users').doc(uid); - - docRef.get().then((doc) => { - dispatch(updateDivison(doc.data().divison)); - }); + docRef.set({ + hours: 0, + divison: _divison + }).catch((error) => { + console.log(error); + }) } -export const updateDivison = (divison) => { - return { - type: UPDATE_DIVISON, - payload: divison +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()); + } + }); + } } } @@ -119,22 +125,3 @@ export const updateAdmin = () => { 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: [] - } -} diff --git a/src/actions/firebaseFirestore.js b/src/actions/firebaseFirestore.js new file mode 100644 index 0000000..8cfb0da --- /dev/null +++ b/src/actions/firebaseFirestore.js @@ -0,0 +1,137 @@ +import { firestore } 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 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 + } +} + +//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 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 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 snapshotForums = () => (dispatch) => { + var docRef = firestore.collection('posts'); + docRef.onSnapshot((query) => { + var forumPosts = []; + query.forEach((doc) => { + forumPosts.push(doc.data()); + }); + dispatch(updateForumPosts(forumPosts)); + }); +} + +//Do not dipatch to store, only update firebaseFirestore + +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 + }); +} + +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 createForumPost = (_subject, _content) => (dispatch, getState) => { + var docRef = firestore.collection('posts'); + const userEmail = getState().firebase.userEmail; + docRef.add({ + email: userEmail, + subject: _subject, + content: _content + }); +} diff --git a/src/reducers/firebaseFirestore.js b/src/reducers/firebaseFirestore.js new file mode 100644 index 0000000..43b5f87 --- /dev/null +++ b/src/reducers/firebaseFirestore.js @@ -0,0 +1,56 @@ +/** +@license +Copyright (c) 2018 The Polymer Project Authors. All rights reserved. +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt +Code distributed by Google as part of the polymer project is also +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt +*/ + +import { + UPDATE_DIVISON, + UPDATE_HOURS, + UPDATE_REGISTERED_COMPETITIONS, + UPDATE_FORUM_POSTS +} +from '../actions/firebaseFirestore.js'; + +const firebaseFirestore = (state = {hours: -1, requestedHours: -1, registeredComps: [], forumPosts: []}, action) => { + switch (action.type) { + + case UPDATE_DIVISON: + return { + ...state, + divison: action.payload + }; + break; + + case UPDATE_HOURS: + return { + ...state, + hours: action.approvedHours, + requestedHours: action.requestedHours + } + break; + + case UPDATE_REGISTERED_COMPETITIONS: + return { + ...state, + registeredComps : action.payload + } + break; + + case UPDATE_FORUM_POSTS: + return { + ...state, + forumPosts : action.payload + } + break; + + default: + return state; + } +}; + +export default firebaseFirestore; |