From c016e29290835940fbc41e430363975ac3ccbb7c Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Mon, 12 Dec 2016 13:00:39 -0700 Subject: [PATCH] separate route for loading comments per user --- client/coral-framework/actions/items.js | 6 ++++-- routes/api/comments/index.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/client/coral-framework/actions/items.js b/client/coral-framework/actions/items.js index e2dfc1ee9..f9f0157c7 100644 --- a/client/coral-framework/actions/items.js +++ b/client/coral-framework/actions/items.js @@ -92,16 +92,19 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) => export const fetchCommentsByUserId = userId => { return (dispatch) => { dispatch({type: REQUEST_COMMENTS_BY_USER}); - return coralApi(`/comments?user_id=${userId}`) + return coralApi(`/comments/user/${userId}`) .then(comments => { dispatch({type: RECEIVE_COMMENTS_BY_USER, comments}); + console.log('comments?', comments); + comments.forEach(comment => { dispatch(addItem(comment, 'comments')); }); }) .catch(error => { + console.error('FAILURE_COMMENTS_BY_USER', error); dispatch({type: FAILURE_COMMENTS_BY_USER, error}); }); }; @@ -145,7 +148,6 @@ export function getStream (assetUrl) { /* Sort comments by date*/ json.comments.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); - const rels = json.comments.reduce((h, item) => { /* Check for root and child comments. */ if ( diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 168b89fc3..ec01c2984 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -116,6 +116,18 @@ router.post('/', wordlist.filter('body'), (req, res, next) => { }); }); +router.get('/user/:user_id', (req, res, next) => { + // how to only get YOUR comments? + Comment.findByUserId(req.params.user_id) + .then(comments => { + res.json(comments); + }) + .catch(error => { + error.status = 500; + next(error); + }); +}); + router.get('/:comment_id', authorization.needed('admin'), (req, res, next) => { Comment .findById(req.params.comment_id)