From 99ffbe52a44997a52a108242918e03008a01944b Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Wed, 4 Dec 2019 15:37:09 -0500 Subject: [PATCH] Posts on Home now display profile images --- functions/handlers/post.js | 58 ++++++++++++++++++++--------- twistter-frontend/src/pages/Home.js | 6 ++- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 7cd966b..3c6cb9e 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -61,27 +61,51 @@ exports.getallPostsforUser = (req, res) => { }; exports.getallPosts = (req, res) => { + let posts = []; + let users = {}; - var post_query = admin.firestore().collection("posts"); - post_query - .get() - .then(function(allPosts) { - let posts = []; - allPosts.forEach(function(doc) { - posts.push(doc.data()); + // Get all the posts + var postsPromise = new Promise((resolve, reject) => { + db.collection("posts").get() + .then((allPosts) => { + allPosts.forEach((post) => { + posts.push(post.data()); + }); + resolve(); + }) + .catch((error) => { + reject(error); + }) + }); + + // Get all users + var usersPromise = new Promise((resolve, reject) => { + db.collection("users").get() + .then((allUsers) => { + allUsers.forEach((user) => { + users[user.data().handle] = user.data(); + }) + resolve(); + }) + .catch((error) => { + reject(error); + }) + }); + + // Wait for the two promises + Promise.all([postsPromise, usersPromise]) + .then(() => { + let newPosts = [] + // Add the image url of the person who made the post to all of the post objects + posts.forEach((post) => { + post.profileImage = users[post.userHandle].imageUrl ? users[post.userHandle].imageUrl : null; + newPosts.push(post); }); - return res.status(200).json(posts); + return res.status(200).json(newPosts); }) - .then(function() { - return res - .status(200) - .json("Successfully retrieved every post from database."); + .catch((error) => { + return res.status(500).json({error}); }) - .catch(function(err) { - return res - .status(500) - .json("Failed to retrieve posts from database.", err); - }); }; exports.getOtherUsersPosts = (req, res) => { diff --git a/twistter-frontend/src/pages/Home.js b/twistter-frontend/src/pages/Home.js index 7b1ba6d..38610f9 100644 --- a/twistter-frontend/src/pages/Home.js +++ b/twistter-frontend/src/pages/Home.js @@ -60,9 +60,13 @@ class Home extends Component { - { + {/* { this.state.imageUrl ? () : () + } */} + { + post.profileImage ? () : + () } {post.userHandle}