diff options
-rw-r--r-- | src/actions/firebaseAdmin.js | 110 | ||||
-rw-r--r-- | src/reducers/firebaseAdmin.js | 46 |
2 files changed, 156 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)); +} diff --git a/src/reducers/firebaseAdmin.js b/src/reducers/firebaseAdmin.js new file mode 100644 index 0000000..205d321 --- /dev/null +++ b/src/reducers/firebaseAdmin.js @@ -0,0 +1,46 @@ +/** +@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_ADMIN, + UPDATE_ADMIN_REQUESTS, + UPDATE_ADMIN_COMP_LIST +} +from '../actions/firebaseAdmin.js'; + +const firebaseAdmin = (state = {isAdmin: false, requests: [], compList: []}, action) => { + switch (action.type) { + case UPDATE_ADMIN: + return { + ...state, + isAdmin : action.payload + } + break; + + case UPDATE_ADMIN_REQUESTS: + return { + ...state, + requests : action.payload + } + break; + + case UPDATE_ADMIN_COMP_LIST: + return { + ...state, + compList : action.payload + } + break; + + default: + return state; + } +}; + +export default firebaseAdmin; |