aboutsummaryrefslogtreecommitdiff
path: root/src/actions/firebaseFirestore.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/actions/firebaseFirestore.js')
-rw-r--r--src/actions/firebaseFirestore.js129
1 files changed, 83 insertions, 46 deletions
diff --git a/src/actions/firebaseFirestore.js b/src/actions/firebaseFirestore.js
index 7e65eec..701a0c3 100644
--- a/src/actions/firebaseFirestore.js
+++ b/src/actions/firebaseFirestore.js
@@ -44,6 +44,7 @@ export const fetchDivison = () => (dispatch, getState) => {
});
}
+var isAdminUpdate = false;
export const snapshotHours = () => (dispatch, getState) => {
const currentAuthState = getState().firebaseAuth;
if(currentAuthState.signedIn) {
@@ -53,11 +54,15 @@ export const snapshotHours = () => (dispatch, getState) => {
docRefUsers.onSnapshot((doc) => {
totalHours = doc.data().hours;
docRefReq.onSnapshot((query) => {
+ if(isAdminUpdate) {
+ alert("One of your hours requests has been approved or denied.\nGo to dashboard to see changes.")
+ }
var requestedHours = 0;
query.forEach((docs) => {
requestedHours += docs.data().time;
});
dispatch(updateHours(totalHours, requestedHours));
+ isAdminUpdate = true;
});
});
}
@@ -101,58 +106,90 @@ export const setUserData = (_divison) => (dispatch, getState) => {
}
export const requestHours = (_time, _trainee, _location, _subject, _date, _pictureName) => (dispatch, getState) => {
- var docRef = firestore.collection('requests');
- const _uid = getState().firebaseAuth.uid;
- const _email = getState().firebaseAuth.userEmail;
- const _path = 'requests/' + _uid + '/' + _pictureName;
-
- var fireStorageRef = fireStorage.ref().child(_path);
- fireStorageRef.getDownloadURL().then((url) => {
- docRef.add({
- time: _time,
- trainee: _trainee,
- location: _location,
- subject: _subject,
- day: _date,
- imgUrl: url,
- path: _path,
- uid: _uid,
- email: _email
+ if(getState().app.offline) {
+ alert("Failed to create forum post. Must have internet connection.")
+ } else {
+ isAdminUpdate = false;
+ var docRef = firestore.collection('requests');
+ const _uid = getState().firebaseAuth.uid;
+ const _email = getState().firebaseAuth.userEmail;
+ const _path = 'requests/' + _uid + '/' + _pictureName;
+
+ var fireStorageRef = fireStorage.ref().child(_path);
+ fireStorageRef.getDownloadURL().then((url) => {
+ docRef.add({
+ time: _time,
+ trainee: _trainee,
+ location: _location,
+ subject: _subject,
+ day: _date,
+ imgUrl: url,
+ path: _path,
+ uid: _uid,
+ email: _email
+ }).then(()=> {
+ alert("Successfuly sent hours request for " + _time*60 + " minutes.\n"
+ + "If it is approved, it will update on your dashboard.");
+ }).catch((error) => {
+ alert(error);
+ });
});
- });
-
+ }
}
export const registerComp = (compName) => (dispatch, getState) => {
- var docRef = firestore.collection('competitions').doc(compName);
- var uid = getState().firebaseAuth.uid;
- var email = getState().firebaseAuth.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]
- });
- }
- });
+ if(getState().app.offline) {
+ alert("Failed to register. Please establish internet connection.")
+ } else {
+ var docRef = firestore.collection('competitions').doc(compName);
+ var uid = getState().firebaseAuth.uid;
+ var email = getState().firebaseAuth.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
+ }).then(()=> {
+ alert("Successfuly registered for " + compName + ".");
+ }).catch((error) => {
+ alert(error);
+ });
+ } else {
+ docRef.set({
+ uids : [uid],
+ emails: [email]
+ }).then(()=> {
+ alert("Successfuly registered for " + compName + ".");
+ }).catch((error) => {
+ alert(error);
+ });
+ }
+ }).catch((error) => {
+ alert(error);
+ });
+ }
}
export const createForumPost = (_subject, _content) => (dispatch, getState) => {
- var docRef = firestore.collection('posts');
- const userEmail = getState().firebaseAuth.userEmail;
- docRef.add({
- email: userEmail,
- subject: _subject,
- content: _content
- });
+ if(getState().app.offline) {
+ alert("Failed to create forum post. Please establish internet connection.")
+ } else {
+ var docRef = firestore.collection('posts');
+ const userEmail = getState().firebaseAuth.userEmail;
+ docRef.add({
+ email: userEmail,
+ subject: _subject,
+ content: _content
+ }).then(()=> {
+ alert("Successfuly posted forum with subject " + _subject + ".");
+ }).catch((error) => {
+ alert(error);
+ });
+ }
+
}