aboutsummaryrefslogtreecommitdiff
path: root/src/actions/firebaseAdmin.js
diff options
context:
space:
mode:
authorMichael Foiani <mfoiani2019@communiyschoolnaples.org>2018-08-04 23:59:12 -0400
committerMichael Foiani <mfoiani2019@communiyschoolnaples.org>2018-08-04 23:59:12 -0400
commitc518aedffdae943a84c53763629d0785043f6a3f (patch)
tree28596613dcbea1b2f0c3931ddd25f56723813dcc /src/actions/firebaseAdmin.js
parentdd18707b172db1cdc5a69a0a39b5fff3c5edc781 (diff)
Separated firebaseAdmin's actions and reducers into their respective files.
Diffstat (limited to 'src/actions/firebaseAdmin.js')
-rw-r--r--src/actions/firebaseAdmin.js110
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));
+}