aboutsummaryrefslogtreecommitdiff
path: root/src/actions/firebaseAdmin.js
blob: 6009d469ee78e2d198ac88f45918d694e6760404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { firestore, fireStorage } 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 UPDATE_ADMIN_USER_LIST = 'UPDATE_ADMIN_USER_LIST';

export const updateAdmin = (status) => {
  return {
    type:     UPDATE_ADMIN,
    payload:  status
  }
}

export const updateAdminRequests = (requests) => {
  return {
    type:     UPDATE_ADMIN_REQUESTS,
    payload:  requests
  }
}

export const updateAdminCompList = (compList) => {
  return {
    type:     UPDATE_ADMIN_COMP_LIST,
    payload:  compList
  }
}

export const updateAdminUserList = (userList) => {
  return {
    type:     UPDATE_ADMIN_USER_LIST,
    payload:  userList
  }
}

//Middleware to update dispatches

export const adminControls = () => (dispatch) => {
  dispatch(updateAdmin(true));
  dispatch(snapshotAdminRequests());
  dispatch(snapshotAdminCompList());
  dispatch(snapshotAdminUserList());
}

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 snapshotAdminUserList = () => (dispatch) => {
  var docRef  = firestore.collection('users');
  docRef.onSnapshot((querySnapshot) => {
    var userList = [];
    querySnapshot.forEach((doc) => {
      userList.push(doc);
    });
    dispatch(updateAdminUserList(userList));
  });
}

export const adminClose = () => (dispatch) => {
  dispatch(updateAdmin(false));
  dispatch(updateAdminRequests([]));
  dispatch(updateAdminCompList([]));
  dispatch(updateAdminUserList([]));
}

export const adminRejectHours = (_id, _path) => (dispatch) => {
  dispatch(adminDeleteRequest(_id, _path));
}

export const adminDeleteRequest = (_id, _path) => (dispatch) => {
  var docRef  = firestore.collection('requests').doc(_id);
  docRef.delete().then(() => {
    var fireStorageRef  = fireStorage.ref().child(_path);
    fireStorageRef.delete().then(() => {
    });
  });
}

//User triggered events dispatching to middleware

export const adminListener = () => (dispatch, getState) => {
  document.onkeyup = function(e) {
    if((e.altKey && e.which == 65) && !getState().firebaseAdmin.isAdmin) {
      var docRef  = firestore.collection('admin');
      docRef.doc('adminKey').get().then((doc) => {
        if(prompt('Enter admin password') == doc.data().password) {
          dispatch(adminControls());
        } else {
          alert("Incorrect admin password");
          docRef.doc('loggedData').collection('failedTries').add({
            uid: getState().firebaseAuth.uid,
            email: getState().firebaseAuth.userEmail,
            day: new Date()
          });
        }
      }); 
    }
  }
}

export const adminApproveHours = (_uid, _time, _id, _path) => (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, _path));
}