diff options
Diffstat (limited to 'src/actions/firebaseFirestore.js')
-rw-r--r-- | src/actions/firebaseFirestore.js | 37 |
1 files changed, 17 insertions, 20 deletions
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); }); } |