diff --git a/client/coral-framework/actions/items.js b/client/coral-framework/actions/items.js index f9f0157c7..cd798c6b9 100644 --- a/client/coral-framework/actions/items.js +++ b/client/coral-framework/actions/items.js @@ -6,9 +6,6 @@ import {UPDATE_CONFIG} from '../constants/config'; * Action name constants */ -export const REQUEST_COMMENTS_BY_USER = 'REQUEST_COMMENTS_BY_USER'; -export const RECEIVE_COMMENTS_BY_USER = 'RECEIVE_COMMENTS_BY_USER'; -export const FAILURE_COMMENTS_BY_USER = 'FAILURE_COMMENTS_BY_USER'; export const ADD_ITEM = 'ADD_ITEM'; export const UPDATE_ITEM = 'UPDATE_ITEM'; export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY'; @@ -82,34 +79,6 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) => }; }; -/** - * - * Get a list of comments by a single user - * - * @param {string} user_id - * @returns Promise - */ -export const fetchCommentsByUserId = userId => { - return (dispatch) => { - dispatch({type: REQUEST_COMMENTS_BY_USER}); - 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}); - }); - }; -}; - /* * Get Items from Query * Gets a set of items from a predefined query diff --git a/client/coral-framework/actions/user.js b/client/coral-framework/actions/user.js index cda2d765d..5cbd04792 100644 --- a/client/coral-framework/actions/user.js +++ b/client/coral-framework/actions/user.js @@ -1,5 +1,6 @@ import * as actions from '../constants/user'; import {addNotification} from '../actions/notification'; +import {addItem} from '../actions/items'; import coralApi from '../helpers/response'; import I18n from 'coral-framework/modules/i18n/i18n'; @@ -19,3 +20,29 @@ export const saveBio = (user_id, formData) => dispatch => { }) .catch(error => dispatch(saveBioFailure(error))); }; + +/** + * + * Get a list of comments by a single user + * + * @param {string} user_id + * @returns Promise + */ +export const fetchCommentsByUserId = userId => { + return (dispatch) => { + dispatch({type: actions.REQUEST_COMMENTS_BY_USER}); + return coralApi(`/comments/user/${userId}`) + .then(comments => { + comments.forEach(comment => { + dispatch(addItem(comment, 'comments')); + }); + + dispatch({type: actions.RECEIVE_COMMENTS_BY_USER, comments: comments.map(comment => comment.id)}); + }) + .catch(error => { + console.error(error.stack); + console.error('FAILURE_COMMENTS_BY_USER', error); + dispatch({type: actions.FAILURE_COMMENTS_BY_USER, error}); + }); + }; +}; diff --git a/client/coral-framework/constants/user.js b/client/coral-framework/constants/user.js index 0c316d48a..ce9af61a2 100644 --- a/client/coral-framework/constants/user.js +++ b/client/coral-framework/constants/user.js @@ -1,3 +1,6 @@ export const SAVE_BIO_REQUEST = 'SAVE_BIO_REQUEST'; export const SAVE_BIO_SUCCESS = 'SAVE_BIO_SUCCESS'; export const SAVE_BIO_FAILURE = 'SAVE_BIO_FAILURE'; +export const REQUEST_COMMENTS_BY_USER = 'REQUEST_COMMENTS_BY_USER'; +export const RECEIVE_COMMENTS_BY_USER = 'RECEIVE_COMMENTS_BY_USER'; +export const FAILURE_COMMENTS_BY_USER = 'FAILURE_COMMENTS_BY_USER'; diff --git a/client/coral-framework/reducers/items.js b/client/coral-framework/reducers/items.js index fa14085f3..93388c1ab 100644 --- a/client/coral-framework/reducers/items.js +++ b/client/coral-framework/reducers/items.js @@ -17,7 +17,6 @@ export default (state = initialState, action) => { return state.setIn([action.item_type, action.id, action.property], fromJS(action.value)); case actions.APPEND_ITEM_ARRAY: return state.updateIn([action.item_type, action.id, action.property], (prop) => { - console.log(action, prop); if (action.add_to_front) { return prop ? prop.unshift(fromJS(action.value)) : fromJS([action.value]); } else { diff --git a/client/coral-framework/reducers/user.js b/client/coral-framework/reducers/user.js index 11b57fc15..c7badf458 100644 --- a/client/coral-framework/reducers/user.js +++ b/client/coral-framework/reducers/user.js @@ -1,11 +1,12 @@ -import {Map} from 'immutable'; +import {Map, fromJS} from 'immutable'; import * as authActions from '../constants/auth'; import * as actions from '../constants/user'; const initialState = Map({ displayName: '', profiles: [], - settings: {} + settings: {}, + myComments: [] }); const purge = user => { @@ -30,6 +31,8 @@ export default function user (state = initialState, action) { case actions.SAVE_BIO_SUCCESS: return state .set('settings', action.settings); + case actions.RECEIVE_COMMENTS_BY_USER: + return state.set('myComments', fromJS(action.comments)); default : return state; } diff --git a/client/coral-plugin-history/CommentHistory.js b/client/coral-plugin-history/CommentHistory.js index ea8846373..de95ff2b2 100644 --- a/client/coral-plugin-history/CommentHistory.js +++ b/client/coral-plugin-history/CommentHistory.js @@ -1,24 +1,21 @@ -import React from 'react'; -import {connect} from 'react-redux'; +import React, {PropTypes} from 'react'; import styles from './CommentHistory.css'; -class CommentHistory extends React.Component { - render () { - return ( -
-

Comment History

-
- ); - } -} - -const mapStateToProps = state => { - return { - config: state.config.toJS(), - items: state.items.toJS(), - auth: state.auth.toJS() - }; +const CommentHistory = props => { + return ( +
+

Comment History

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

{comment.body}

; + })} +
+ ); }; -export default connect(mapStateToProps)(CommentHistory); +CommentHistory.propTypes = { + comments: PropTypes.arrayOf(PropTypes.object).isRequired +}; + +export default CommentHistory; diff --git a/client/coral-settings/components/CommentHistory.js b/client/coral-settings/components/CommentHistory.js deleted file mode 100644 index 3160c8a88..000000000 --- a/client/coral-settings/components/CommentHistory.js +++ /dev/null @@ -1,15 +0,0 @@ -import React from 'react'; -import styles from './CommentHistory.css'; - -export default ({comments = []}) => ( -
-

Comments

- -
-); diff --git a/client/coral-settings/containers/SettingsContainer.js b/client/coral-settings/containers/SettingsContainer.js index 913f5f62e..9ad489816 100644 --- a/client/coral-settings/containers/SettingsContainer.js +++ b/client/coral-settings/containers/SettingsContainer.js @@ -1,7 +1,7 @@ import React, {Component} from 'react'; import {connect} from 'react-redux'; -import {saveBio} from 'coral-framework/actions/user'; +import {saveBio, fetchCommentsByUserId} from 'coral-framework/actions/user'; import BioContainer from './BioContainer'; import NotLoggedIn from '../components/NotLoggedIn'; @@ -10,8 +10,6 @@ import CommentHistory from 'coral-plugin-history/CommentHistory'; import SettingsHeader from '../components/SettingsHeader'; import RestrictedContent from 'coral-framework/components/RestrictedContent'; -import {fetchCommentsByUserId} from 'coral-framework/actions/items'; - class SignInContainer extends Component { constructor (props) { super(props); @@ -35,7 +33,7 @@ class SignInContainer extends Component { } render() { - const {loggedIn, userData, showSignInDialog} = this.props; + const {loggedIn, userData, showSignInDialog, items, user} = this.props; const {activeTab} = this.state; return ( }> @@ -45,7 +43,7 @@ class SignInContainer extends Component { Profile Settings - + items.comments[id])} /> @@ -55,7 +53,9 @@ class SignInContainer extends Component { } } -const mapStateToProps = () => ({ +const mapStateToProps = state => ({ + items: state.items.toJS(), + user: state.user.toJS() }); const mapDispatchToProps = dispatch => ({