diff options
Diffstat (limited to 'src/actions')
| -rw-r--r-- | src/actions/app.js | 16 | ||||
| -rw-r--r-- | src/actions/firebaseFirestore.js | 37 |
2 files changed, 25 insertions, 28 deletions
diff --git a/src/actions/app.js b/src/actions/app.js index ef3f18e..aecefc4 100644 --- a/src/actions/app.js +++ b/src/actions/app.js @@ -26,8 +26,9 @@ export const navigate = (path) => (dispatch) => { dispatch(updateDrawerState(false)); }; -import { snapshotForums } from './firebaseFirestore.js'; -var hasSubscribedPosts = false; +import {snapshotForums} from './firebaseFirestore.js'; + +var isSubscribedForums = false; const loadPage = (page) => (dispatch) => { switch(page) { @@ -44,12 +45,11 @@ const loadPage = (page) => (dispatch) => { break; case 'forums': - import('../components/mao-forums.js').then((module) => { - if(!hasSubscribedPosts) { - dispatch(snapshotForums()); - hasSubscribedPosts = true; - } - }); + import('../components/mao-forums.js'); + if(!isSubscribedForums) { + dispatch(snapshotForums()); + isSubscribedForums = true; + } break; case 'account': diff --git a/src/actions/firebaseFirestore.js b/src/actions/firebaseFirestore.js index 636c944..feea51e 100644 --- a/src/actions/firebaseFirestore.js +++ b/src/actions/firebaseFirestore.js @@ -93,16 +93,8 @@ export const snapshotForums = () => (dispatch) => { docRef.onSnapshot((query) => { var forumPosts = []; query.forEach((doc) => { - var comments = []; - docRef.doc(doc.id).collection('comments') - .onSnapshot((queryComments)=> { - queryComments.forEach((comment) => { - comments.push(comment.data()); - }); - }); forumPosts.push({ ...doc.data(), - comments, postId: doc.id }); }); @@ -204,7 +196,8 @@ export const createForumPost = (_subject, _content) => (dispatch, getState) => { docRef.add({ email: userEmail, subject: _subject, - content: _content + content: _content, + day: new Date() }).then(()=> { alert("Successfuly posted forum with subject " + _subject + "."); }).catch((error) => { @@ -213,20 +206,24 @@ export const createForumPost = (_subject, _content) => (dispatch, getState) => { } } -export const createComment = (postId, subject, content) => (dispatch, getState) => { +export const createComment = (postId, content) => (dispatch, getState) => { if(getState().app.offline) { alert("Failed to create comment post. Please establish internet connection.") } else { - var docRef = firestore.collection('posts').doc(postId).collection('comments'); - var user = getState().firebaseAuth.userEmail.replace('@communityschoolnaples.org', ''); - docRef.add({ - subject, - content, - user, - date: new Date() - }).then(()=> { - alert("Successfuly posted comment."); - }).catch((error) => { + var docRef = firestore.collection('posts').doc(postId); + docRef.get().then((doc) => { + var comments = doc.data().comments ? doc.data().comments : []; + comments.push({ + content, + user : getState().firebaseAuth.userEmail.replace('@communityschoolnaples.org', ''), + date: new Date() + }); + console.log(comments); + docRef.update({ + comments + }); + }) + .catch((error) => { alert(error); }); } |
