import { firestore, storage } 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 currentAuthState = getState().firebaseAuth; if(currentAuthState.signedIn) { var totalHours; var docRefUsers = firestore.collection('users').doc(currentAuthState.uid); var docRefReq = firestore.collection('requests').where('uid', '==', currentAuthState.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().firebaseAuth.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 setUserData = (_divison) => (dispatch, getState) => { const uid = getState().firebaseAuth.uid; var docRef = firestore.collection('users').doc(uid); docRef.set({ hours: 0, divison: _divison }).catch((error) => { console.log(error); }) } 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; var storageRef = storage.ref().child('requests/' + _uid + '/' + _pictureName); storageRef.getDownloadURL().then((url) => { docRef.add({ time: _time, trainee: _trainee, location: _location, subject: _subject, day: _date, imgPath: url, uid: _uid, email: _email }); }); } 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] }); } }); } 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 }); }