From 29b6fc08cd166ff0c1577c569f2e24d1cd7aeda1 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Wed, 14 Dec 2016 14:41:54 -0700 Subject: [PATCH] hydrate the assets in the comment response --- client/coral-framework/actions/assets.js | 11 ++++------- client/coral-framework/actions/user.js | 10 ++++++---- client/coral-framework/constants/assets.js | 3 +++ client/coral-framework/reducers/user.js | 2 +- client/coral-settings/containers/SettingsContainer.js | 8 +------- routes/api/comments/index.js | 4 +++- 6 files changed, 18 insertions(+), 20 deletions(-) create mode 100644 client/coral-framework/constants/assets.js diff --git a/client/coral-framework/actions/assets.js b/client/coral-framework/actions/assets.js index e5073b63c..f778831b7 100644 --- a/client/coral-framework/actions/assets.js +++ b/client/coral-framework/actions/assets.js @@ -1,19 +1,16 @@ +import * as actions from '../constants/assets'; import coralApi from '../helpers/response'; import {addItem} from './items'; -export const MULTIPLE_ASSETS_REQUEST = 'MULTIPLE_ASSETS_REQUEST'; -export const MULTIPLE_ASSETS_SUCCESS = 'MULTIPLE_ASSETS_SUCCESS'; -export const MULTIPLE_ASSSETS_FAILURE = 'MULTIPLE_ASSSETS_FAILURE'; - export const fetchMulitpleAssets = ids => { return dispatch => { - dispatch({type: MULTIPLE_ASSETS_REQUEST}); + dispatch({type: actions.MULTIPLE_ASSETS_REQUEST}); coralApi(`/assets/multi?ids=${encodeURIComponent(ids.join(','))}`) .then(assets => { assets.forEach(asset => dispatch(addItem(asset, 'assets'))); - dispatch({type: MULTIPLE_ASSETS_SUCCESS, assets: assets.map(asset => asset.id)}); + dispatch({type: actions.MULTIPLE_ASSETS_SUCCESS, assets: assets.map(asset => asset.id)}); }) - .catch(error => dispatch({type: MULTIPLE_ASSSETS_FAILURE, error})); + .catch(error => dispatch({type: actions.MULTIPLE_ASSSETS_FAILURE, error})); }; }; diff --git a/client/coral-framework/actions/user.js b/client/coral-framework/actions/user.js index b279ad0a4..69f4882bb 100644 --- a/client/coral-framework/actions/user.js +++ b/client/coral-framework/actions/user.js @@ -1,4 +1,5 @@ import * as actions from '../constants/user'; +import * as assetActions from '../constants/assets'; import {addNotification} from '../actions/notification'; import {addItem} from '../actions/items'; import coralApi from '../helpers/response'; @@ -32,12 +33,13 @@ export const fetchCommentsByUserId = userId => { return (dispatch) => { dispatch({type: actions.COMMENTS_BY_USER_REQUEST}); return coralApi(`/comments?user_id${userId}`) - .then(({comments}) => { - comments.forEach(comment => { - dispatch(addItem(comment, 'comments')); - }); + .then(({comments, assets}) => { + comments.forEach(comment => dispatch(addItem(comment, 'comments'))); + + assets.forEach(asset => dispatch(addItem(asset, 'assets'))); dispatch({type: actions.COMMENTS_BY_USER_SUCCESS, comments: comments.map(comment => comment.id)}); + dispatch({type: assetActions.MULTIPLE_ASSETS_SUCCESS, assets: assets.map(asset => asset.id)}); }) .catch(error => { console.error(error.stack); diff --git a/client/coral-framework/constants/assets.js b/client/coral-framework/constants/assets.js new file mode 100644 index 000000000..0224e0945 --- /dev/null +++ b/client/coral-framework/constants/assets.js @@ -0,0 +1,3 @@ +export const MULTIPLE_ASSETS_REQUEST = 'MULTIPLE_ASSETS_REQUEST'; +export const MULTIPLE_ASSETS_SUCCESS = 'MULTIPLE_ASSETS_SUCCESS'; +export const MULTIPLE_ASSSETS_FAILURE = 'MULTIPLE_ASSSETS_FAILURE'; \ No newline at end of file diff --git a/client/coral-framework/reducers/user.js b/client/coral-framework/reducers/user.js index 5c064e07a..6e9f3529c 100644 --- a/client/coral-framework/reducers/user.js +++ b/client/coral-framework/reducers/user.js @@ -1,7 +1,7 @@ import {Map, fromJS} from 'immutable'; import * as authActions from '../constants/auth'; import * as actions from '../constants/user'; -import * as assetActions from '../actions/assets'; +import * as assetActions from '../constants/assets'; const initialState = Map({ displayName: '', diff --git a/client/coral-settings/containers/SettingsContainer.js b/client/coral-settings/containers/SettingsContainer.js index 9d535d595..0981bafe3 100644 --- a/client/coral-settings/containers/SettingsContainer.js +++ b/client/coral-settings/containers/SettingsContainer.js @@ -23,13 +23,7 @@ class SignInContainer extends Component { componentWillMount () { // Fetch commentHistory - this.props.fetchCommentsByUserId(this.props.userData.id) - .then(() => { - const assetIds = this.props.user.myComments - .map(id => this.props.items.comments[id]) - .map(comment => comment.asset_id); - this.props.fetchMulitpleAssets(assetIds); - }); + this.props.fetchCommentsByUserId(this.props.userData.id); } handleTabChange(tab) { diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index fca2bcb31..8f21a9488 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -50,13 +50,15 @@ router.get('/', authorization.needed('admin'), (req, res, next) => { query.then((comments) => { return Promise.all([ comments, + Asset.findMultipleById(comments.map(comment => comment.asset_id)), User.findByIdArray(_.uniq(comments.map((comment) => comment.author_id))), Action.getActionSummariesFromComments(asset_id, comments, req.user ? req.user.id : false) ]); }) - .then(([comments, users, actions])=> + .then(([comments, assets, users, actions]) => res.status(200).json({ comments, + assets, users, actions }))