hydrate the assets in the comment response

This commit is contained in:
Riley Davis
2016-12-14 14:41:54 -07:00
parent 601010f1f9
commit 29b6fc08cd
6 changed files with 18 additions and 20 deletions
+4 -7
View File
@@ -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}));
};
};
+6 -4
View File
@@ -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);
@@ -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';
+1 -1
View File
@@ -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: '',
@@ -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) {
+3 -1
View File
@@ -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
}))