diff options
Diffstat (limited to 'src/actions/firebaseAdmin.js')
-rw-r--r-- | src/actions/firebaseAdmin.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/actions/firebaseAdmin.js b/src/actions/firebaseAdmin.js new file mode 100644 index 0000000..8fdc8fa --- /dev/null +++ b/src/actions/firebaseAdmin.js @@ -0,0 +1,110 @@ +import { firestore } from '../firebase.js'; + +export const UPDATE_ADMIN = 'UPDATE_ADMIN'; +export const UPDATE_ADMIN_REQUESTS = 'UPDATE_ADMIN_REQUESTS'; +export const UPDATE_ADMIN_COMP_LIST = 'UPDATE_ADMIN_COMP_LIST'; + +export const updateAdmin = () => { + return { + type: UPDATE_ADMIN, + payload: true + } +} + +export const updateAdminRequests = (requests) => { + return { + type: UPDATE_ADMIN_REQUESTS, + payload: requests + } +} + +export const updateAdminCompList = (compList) => { + return { + type: UPDATE_ADMIN_COMP_LIST, + payload: compList + } +} + +//Middleware to update dispatches + +export const adminControls = () => (dispatch) => { + dispatch(updateAdmin()); + dispatch(snapshotAdminRequests()); + dispatch(snapshotAdminCompList()); +} + +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 snapshotAdminCompList = () => (dispatch) => { + var docRef = firestore.collection('competitions'); + docRef.onSnapshot((querySnapshot) => { + var compList = []; + querySnapshot.forEach((doc) => { + var divisonData = []; + for(var i = 0; i < doc.data().uids.length; i++) { + var docRefDivison = firestore.collection('users').doc(doc.data().uids[i]); + docRefDivison.get().then((docUser) => { + divisonData.push(docUser.data().divison); + }); + } + compList.push({ + ...doc.data(), + name: doc.id, + divisons: divisonData + }); + }); + dispatch(updateAdminCompList(compList)); + }); +} + +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); + }); +} + +//User triggered events dispatching to middleware + +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 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)); +} |