add get comments by user endpoint. refactor getStream to be functional programming

This commit is contained in:
Riley Davis
2016-11-23 17:14:51 -07:00
parent 71d4ddf030
commit 28520bfd4a
7 changed files with 67 additions and 26 deletions
@@ -19,6 +19,7 @@ import LikeButton from '../../coral-plugin-likes/LikeButton';
import PermalinkButton from '../../coral-plugin-permalinks/PermalinkButton';
import SignInContainer from '../../coral-sign-in/containers/SignInContainer';
import UserBox from '../../coral-sign-in/components/UserBox';
import CommentHistory from '../../coral-plugin-history/CommentHistory';
const {addItem, updateItem, postItem, getStream, postAction, deleteAction, appendItemArray} = itemActions;
const {addNotification, clearNotification} = notificationActions;
@@ -87,6 +88,7 @@ class CommentStream extends Component {
const {actions, users, comments} = this.props.items;
const {loggedIn, user, showSignInDialog} = this.props.auth;
return <div className={showSignInDialog ? 'expandForSignin' : ''}>
<CommentHistory />
{
rootItem
? <div>
+42 -17
View File
@@ -1,5 +1,11 @@
import sortBy from 'lodash/sortBy';
/* Item Actions */
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';
/**
* Action name constants
*/
@@ -84,6 +90,27 @@ 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 fetch(`/api/v1/comments?user_id=${userId}`, getInit('GET'))
.then(responseHandler)
.then(comments => {
dispatch({type: RECEIVE_COMMENTS_BY_USER, comments});
})
.catch(error => {
dispatch({type: FAILURE_COMMENTS_BY_USER, error});
});
};
};
/*
* Get Items from Query
* Gets a set of items from a predefined query
@@ -104,25 +131,24 @@ export function getStream (assetUrl) {
.then((json) => {
/* Add items to the store */
const itemTypes = Object.keys(json);
for (let i = 0; i < itemTypes.length; i++ ) {
if (itemTypes[i] === 'actions') {
for (let j = 0; j < json[itemTypes[i]].length; j++ ) {
let action = json[itemTypes[i]][j];
Object.keys(json).forEach(type => {
if (type === 'actions') {
json[type].forEach(action => {
action.id = `${action.action_type}_${action.item_id}`;
dispatch(addItem(action, 'actions'));
}
});
} else {
for (let j = 0; j < json[itemTypes[i]].length; j++ ) {
dispatch(addItem(json[itemTypes[i]][j], itemTypes[i]));
}
json[type].forEach(item => {
dispatch(addItem(item, type));
});
}
}
});
const assetId = json.assets[0].id;
/* 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 (
@@ -140,15 +166,14 @@ export function getStream (assetUrl) {
dispatch(updateItem(assetId, 'comments', rels.rootComments, 'assets'));
const childKeys = Object.keys(rels.childComments);
for (let i = 0; i < childKeys.length; i++ ) {
dispatch(updateItem(childKeys[i], 'children', rels.childComments[childKeys[i]].reverse(), 'comments'));
}
Object.keys(rels.childComments).forEach(key => {
dispatch(updateItem(key, 'children', rels.childComments[key].reverse(), 'comments'));
});
/* Hydrate actions on comments */
for (let i = 0; i < json.actions.length; i++ ) {
dispatch(updateItem(json.actions[i].item_id, json.actions[i].action_type, json.actions[i].id, 'comments'));
}
json.actions.forEach(action => {
dispatch(updateItem(action.item_id, action.action_type, action.id, 'comments'));
});
return (json);
});
+2 -2
View File
@@ -6,7 +6,7 @@ import * as actions from '../actions/items';
const initialState = fromJS({
comments: {},
users: {},
actions: {}
actions: {}
});
export default (state = initialState, action) => {
@@ -17,7 +17,7 @@ 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(prop);
console.log(action, prop);
if (action.add_to_front) {
return prop ? prop.unshift(fromJS(action.value)) : fromJS([action.value]);
} else {
@@ -1,20 +1,23 @@
import React from 'react';
import {I18n} from '../coral-framework';
import translations from './translations.json';
import has from 'lodash/has';
import reduce from 'lodash/reduce';
const name = 'coral-plugin-comment-count';
const CommentCount = ({items, id}) => {
let count = 0;
if (items.assets[id] && items.assets[id].comments) {
if (has(items, `assets.${id}.comments`)) {
count += items.assets[id].comments.length;
}
const itemKeys = Object.keys(items.comments);
for (let i = 0; i < itemKeys.length; i++) {
const item = items.comments[itemKeys[i]];
if (item.children) {
count += item.children.length;
// lodash reduce works on {}
count += reduce(items.comments, (accum, comment) => {
if (comment.children) {
accum += comment.children.length;
}
}
return accum;
}, 0);
return <div className={`${name}-text`}>
{`${count} ${count === 1 ? lang.t('comment') : lang.t('comment-plural')}`}
+9
View File
@@ -210,6 +210,15 @@ CommentSchema.statics.all = () => {
return Comment.find();
};
/**
* Returns all the comments by user
* probably to be paginated at some point in the future
* @return {Promise} array resolves to an array of comments by that user
*/
CommentSchema.statics.findByUserId = function (author_id) {
return Comment.find({author_id});
};
// Comment model.
const Comment = mongoose.model('Comment', CommentSchema);
+2
View File
@@ -11,6 +11,8 @@ router.get('/', (req, res, next) => {
query = Comment.findByStatus(req.query.status);
} else if (req.query.action_type) {
query = Comment.findByActionType(req.query.action_type);
} else if (req.query.user_id) {
query = Comment.findByUserId(req.query.user_id);
} else {
query = Comment.all();
}