diff options
| author | Michael Foiani <mfoiani2019@communiyschoolnaples.org> | 2018-08-04 23:41:50 -0400 |
|---|---|---|
| committer | Michael Foiani <mfoiani2019@communiyschoolnaples.org> | 2018-08-04 23:41:50 -0400 |
| commit | 7a084f47ed17181af72629770cda3429af7656f7 (patch) | |
| tree | 464b5906069654002f88921b2e4b1bd788212965 /src/actions/firebaseFirestore.js | |
| parent | 2a66f7feee1a9d18cbfa2bb705b172d372fad72a (diff) | |
Separated basic Firestore actions and reducers into their own files.
Diffstat (limited to 'src/actions/firebaseFirestore.js')
| -rw-r--r-- | src/actions/firebaseFirestore.js | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/actions/firebaseFirestore.js b/src/actions/firebaseFirestore.js new file mode 100644 index 0000000..8cfb0da --- /dev/null +++ b/src/actions/firebaseFirestore.js @@ -0,0 +1,137 @@ +import { firestore } from '../firebase.js'; + +export const UPDATE_DIVISON = 'UPDATE_DIVISON'; +export const UPDATE_HOURS = 'UPDATE_HOURS'; +export const UPDATE_REGISTERED_COMPETITIONS = 'UPDATE_REGISTERED_COMPETITIONS'; +export const UPDATE_FORUM_POSTS = 'UPDATE_FORUM_POSTS'; + +export const updateDivison = (divison) => { + return { + type: UPDATE_DIVISON, + payload: divison + } +} + +export const updateHours = (hours, reqHours) => { + return { + type: 'UPDATE_HOURS', + approvedHours: hours, + requestedHours: reqHours + } +} + +export const updateRegisteredCompetitions = (registeredComps) => { + return { + type: UPDATE_REGISTERED_COMPETITIONS, + payload: registeredComps + } +} + +export const updateForumPosts = (_forumPosts) => { + return { + type: UPDATE_FORUM_POSTS, + payload: _forumPosts + } +} + +//Middleware to dispatches + +export const fetchDivison = () => (dispatch, getState) => { + const uid = getState().firebaseAuth.uid; + var docRef = firestore.collection('users').doc(uid); + docRef.get().then((doc) => { + dispatch(updateDivison(doc.data().divison)); + }); +} + +export const snapshotHours = () => (dispatch, getState) => { + const currentState = getState().firebase; + if(currentState.initialized) { + var totalHours; + var docRefUsers = firestore.collection('users').doc(currentState.uid); + var docRefReq = firestore.collection('requests').where('uid', '==', currentState.uid); + docRefUsers.onSnapshot((doc) => { + totalHours = doc.data().hours; + docRefReq.onSnapshot((query) => { + var requestedHours = 0; + query.forEach((docs) => { + requestedHours += docs.data().time; + }); + dispatch(updateHours(totalHours, requestedHours)); + }); + }); + } +} + +export const snapshotRegisteredCompetitions = () => (dispatch, getState) => { + var docRef = firestore.collection('competitions'); + docRef.onSnapshot((querySnapshot) => { + var registeredComps = []; + querySnapshot.forEach((doc) => { + if(doc.exists && doc.data().uids.includes(getState().firebase.uid)) { + registeredComps.push(doc.id); + } + }); + dispatch(updateRegisteredCompetitions(registeredComps)); + }); +} + +export const snapshotForums = () => (dispatch) => { + var docRef = firestore.collection('posts'); + docRef.onSnapshot((query) => { + var forumPosts = []; + query.forEach((doc) => { + forumPosts.push(doc.data()); + }); + dispatch(updateForumPosts(forumPosts)); + }); +} + +//Do not dipatch to store, only update firebaseFirestore + +export const requestHours = (_time, _trainee, _date) => (dispatch, getState) => { + var docRef = firestore.collection('requests'); + const _uid = getState().firebase.uid; + const _email = getState().firebase.userEmail; + docRef.add({ + time: _time, + trainee: _trainee, + day: _date, + uid: _uid, + email: _email + }); +} + +export const registerComp = (compName) => (dispatch, getState) => { + var docRef = firestore.collection('competitions').doc(compName); + var uid = getState().firebase.uid; + var email = getState().firebase.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] + }); + } + }); +} + + +export const createForumPost = (_subject, _content) => (dispatch, getState) => { + var docRef = firestore.collection('posts'); + const userEmail = getState().firebase.userEmail; + docRef.add({ + email: userEmail, + subject: _subject, + content: _content + }); +} |
