From 37d8c3ad38a1490fb2663cb9e86a407e3edf19a6 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Tue, 13 Dec 2016 12:40:06 -0700 Subject: [PATCH] try to request a list of assets and fail --- client/coral-framework/actions/assets.js | 19 +++++++++++++++++++ client/coral-plugin-history/Comment.css | 7 +++++++ client/coral-plugin-history/Comment.js | 18 ++++++++++++++++++ client/coral-plugin-history/CommentHistory.js | 6 +++--- .../containers/SettingsContainer.js | 13 ++++++++++--- models/asset.js | 10 ++++++++++ routes/api/asset/index.js | 14 ++++++++++++++ 7 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 client/coral-framework/actions/assets.js create mode 100644 client/coral-plugin-history/Comment.css create mode 100644 client/coral-plugin-history/Comment.js diff --git a/client/coral-framework/actions/assets.js b/client/coral-framework/actions/assets.js new file mode 100644 index 000000000..226f1b2b9 --- /dev/null +++ b/client/coral-framework/actions/assets.js @@ -0,0 +1,19 @@ +import coralApi from '../helpers/response'; +import addItem from './items'; + +export const FETCH_MULTIPLE_ASSETS = 'FETCH_MULTIPLE_ASSETS'; +export const RECEIVE_MULTIPLE_ASSETS = 'RECEIVE_MULTIPLE_ASSETS'; +export const FAILURE_MULTIPLE_ASSSETS = 'FAILURE_MULTIPLE_ASSSETS'; + +export const fetchMulitpleAssets = ids => { + return dispatch => { + dispatch({type: FETCH_MULTIPLE_ASSETS}); + + coralApi(`/asset/multi?ids=${encodeURIComponent(ids.join(','))}`) + .then(assets => { + dispatch({type: RECEIVE_MULTIPLE_ASSETS, assets}); + console.log('assets!', assets); + }) + .catch(error => dispatch({type: FAILURE_MULTIPLE_ASSSETS, error})); + }; +}; diff --git a/client/coral-plugin-history/Comment.css b/client/coral-plugin-history/Comment.css new file mode 100644 index 000000000..121839a1d --- /dev/null +++ b/client/coral-plugin-history/Comment.css @@ -0,0 +1,7 @@ +.assetURL { + +} + +.commentBody { + +} diff --git a/client/coral-plugin-history/Comment.js b/client/coral-plugin-history/Comment.js new file mode 100644 index 000000000..a14320ecd --- /dev/null +++ b/client/coral-plugin-history/Comment.js @@ -0,0 +1,18 @@ +import React, {PropTypes} from 'react'; + +import styles from './Comment.css'; + +const Comment = props => { + return ( +
+

{props.comment.asset_id}

+

{props.comment.body}

+
+ ); +}; + +Comment.propTypes = { + comment: PropTypes.object.isRequired +}; + +export default Comment; diff --git a/client/coral-plugin-history/CommentHistory.js b/client/coral-plugin-history/CommentHistory.js index de95ff2b2..cdeaaf3cd 100644 --- a/client/coral-plugin-history/CommentHistory.js +++ b/client/coral-plugin-history/CommentHistory.js @@ -1,14 +1,14 @@ import React, {PropTypes} from 'react'; - +import Comment from './Comment'; import styles from './CommentHistory.css'; const CommentHistory = props => { return (
-

Comment History

+

All Comments

{props.comments.map((comment, i) => { console.log('a comment', comment); - return

{comment.body}

; + return ; })}
); diff --git a/client/coral-settings/containers/SettingsContainer.js b/client/coral-settings/containers/SettingsContainer.js index 9ad489816..0c63cc77d 100644 --- a/client/coral-settings/containers/SettingsContainer.js +++ b/client/coral-settings/containers/SettingsContainer.js @@ -2,6 +2,7 @@ import React, {Component} from 'react'; import {connect} from 'react-redux'; import {saveBio, fetchCommentsByUserId} from 'coral-framework/actions/user'; +import {fetchMulitpleAssets} from 'coral-framework/actions/assets'; import BioContainer from './BioContainer'; import NotLoggedIn from '../components/NotLoggedIn'; @@ -22,8 +23,13 @@ class SignInContainer extends Component { componentWillMount () { // Fetch commentHistory - console.log('userData', this.props.userData); - this.props.fetchCommentsByUserId(this.props.userData.id); + 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); + }); } handleTabChange(tab) { @@ -60,7 +66,8 @@ const mapStateToProps = state => ({ const mapDispatchToProps = dispatch => ({ saveBio: (user_id, formData) => dispatch(saveBio(user_id, formData)), - fetchCommentsByUserId: userId => dispatch(fetchCommentsByUserId(userId)) + fetchCommentsByUserId: userId => dispatch(fetchCommentsByUserId(userId)), + fetchMulitpleAssets: assetIds => dispatch(fetchMulitpleAssets(assetIds)) }); export default connect( diff --git a/models/asset.js b/models/asset.js index 3c68b7c13..6441e00f3 100644 --- a/models/asset.js +++ b/models/asset.js @@ -152,6 +152,16 @@ AssetSchema.statics.search = (value) => value.length === 0 ? Asset.find({}) : As } }); +/** + * Finds multiple assets with matching ids + * @param {Array} ids an array of Strings of asset_id + * @return {Promise} resolves to list of Assets + */ +AssetSchema.statics.findMultipleById = function (ids) { + const query = ids.map(id => ({id})); + return Asset.find(query); +}; + const Asset = mongoose.model('Asset', AssetSchema); module.exports = Asset; diff --git a/routes/api/asset/index.js b/routes/api/asset/index.js index 2ddc3ea23..1090c8e58 100644 --- a/routes/api/asset/index.js +++ b/routes/api/asset/index.js @@ -58,6 +58,20 @@ router.get('/:asset_id', (req, res, next) => { }); }); +// get multiple assets with a comma-separated list of asset ids +router.get('/multi', (req, res, next) => { + const assetIds = req.query.ids.split(','); + + Asset.findMultipleById(assetIds) + .then(assets => { + res.json(assets); + }) + .catch(error => { + error.status = 500; + next(error); + }); +}); + // Adds the asset id to the queue to be scraped. router.post('/:asset_id/scrape', (req, res, next) => {