diff --git a/client/coral-admin/src/services/talk-adapter.js b/client/coral-admin/src/services/talk-adapter.js index ba8ec369b..9f4dd4715 100644 --- a/client/coral-admin/src/services/talk-adapter.js +++ b/client/coral-admin/src/services/talk-adapter.js @@ -45,15 +45,16 @@ Promise.all([fetch('/api/v1/comments/status/pending'), fetch('/api/v1/comments/s .catch(error => store.dispatch({type: 'COMMENTS_MODERATION_QUEUE_FETCH_FAILED', error})); // Update a comment. Now to update a comment we need to send back the whole object + const updateComment = (store, comment) => { fetch(`/api/v1/comments/${comment.get('id')}/status`, { method: 'POST', headers: jsonHeader, body: JSON.stringify({status: comment.get('status')}) }) -.then(res => res.json()) -.then(res => store.dispatch({type: 'COMMENT_UPDATE_SUCCESS', res})) -.catch(error => store.dispatch({type: 'COMMENT_UPDATE_FAILED', error})); + .then(res => res.json()) + .then(res => store.dispatch({type: 'COMMENT_UPDATE_SUCCESS', res})) + .catch(error => store.dispatch({type: 'COMMENT_UPDATE_FAILED', error})); }; // Create a new comment diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index 76d5a0e9b..28b70841f 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -13,52 +13,52 @@ import Count from '../../coral-plugin-comment-count/CommentCount'; import AuthorName from '../../coral-plugin-author-name/AuthorName'; import {ReplyBox, ReplyButton} from '../../coral-plugin-replies'; import Pym from 'pym.js'; +import FlagButton from '../../coral-plugin-flags/FlagButton'; const {addItem, updateItem, postItem, getStream, postAction, appendItemArray} = itemActions; const {addNotification, clearNotification} = notificationActions; const {setLoggedInUser} = authActions; -@connect( - (state) => { - return { - config: state.config.toJS(), - items: state.items.toJS(), - notification: state.notification.toJS(), - auth: state.auth.toJS() - }; - }, - (dispatch) => { - return { - addItem: (item) => { - return dispatch(addItem(item)); - }, - updateItem: (id, property, value) => { - return dispatch(updateItem(id, property, value)); - }, - postItem: (data, type, id) => { - return dispatch(postItem(data, type, id)); - }, - getStream: (rootId) => { - return dispatch(getStream(rootId)); - }, - addNotification: (type, text) => { - return dispatch(addNotification(type, text)); - }, - clearNotification: () => { - return dispatch(clearNotification()); - }, - setLoggedInUser: (user_id) => { - return dispatch(setLoggedInUser(user_id)); - }, - postAction: (item, action, user) => { - return dispatch(postAction(item, action, user)); - }, - appendItemArray: (item, property, value, addToFront) => { - return dispatch(appendItemArray(item, property, value, addToFront)); - } - }; - } -) +const mapStateToProps = (state) => { + return { + config: state.config.toJS(), + items: state.items.toJS(), + notification: state.notification.toJS(), + auth: state.auth.toJS() + }; +}; + +const mapDispatchToProps = (dispatch) => { + return { + addItem: (item, itemType) => { + return dispatch(addItem(item, itemType)); + }, + updateItem: (id, property, value, itemType) => { + return dispatch(updateItem(id, property, value, itemType)); + }, + postItem: (data, type, id) => { + return dispatch(postItem(data, type, id)); + }, + getStream: (rootId) => { + return dispatch(getStream(rootId)); + }, + addNotification: (type, text) => { + return dispatch(addNotification(type, text)); + }, + clearNotification: () => { + return dispatch(clearNotification()); + }, + setLoggedInUser: (user_id) => { + return dispatch(setLoggedInUser(user_id)); + }, + postAction: (item, action, user, itemType) => { + return dispatch(postAction(item, action, user, itemType)); + }, + appendItemArray: (item, property, value, addToFront, itemType) => { + return dispatch(appendItemArray(item, property, value, addToFront, itemType)); + } + }; +}; class CommentStream extends Component { @@ -90,93 +90,92 @@ class CommentStream extends Component { }); } - // TODO: Replace teststream id with id from params + // TODO: Replace teststream id with id from params const rootItemId = 'assetTest'; - const rootItem = this.props.items[rootItemId]; + const rootItem = this.props.items.assets && this.props.items.assets[rootItemId]; return
- { - rootItem ? -
-
- - -
- { - rootItem.comments.map((commentId) => { - const comment = this.props.items[commentId]; - return
-
- - - -
- { - // - } - -
- - { - comment.children && - comment.children.map((replyId) => { - let reply = this.props.items[replyId]; - return
+ { + rootItem + ?
+
+ + +
+ { + rootItem.comments.map((commentId) => { + const comment = this.props.items.comments[commentId]; + return
+
+ + + +
+ + +
+ + { + comment.children && + comment.children.map((replyId) => { + let reply = this.props.items.comments[replyId]; + return

- { - // - } +
; - }) - } -
; - }) - } - -
- : 'Loading' - } -
; - + }) + } +
; + }) + } + +
+ : 'Loading' + } +
; } } -export default CommentStream; +export default connect(mapStateToProps, mapDispatchToProps)(CommentStream); diff --git a/client/coral-framework/__tests__/store/itemReducer.spec.js b/client/coral-framework/__tests__/store/itemReducer.spec.js deleted file mode 100644 index 0e8106bae..000000000 --- a/client/coral-framework/__tests__/store/itemReducer.spec.js +++ /dev/null @@ -1,148 +0,0 @@ -import {Map, fromJS} from 'immutable'; -import {expect} from 'chai'; -import itemsReducer from '../../store/reducers/items'; - -describe ('itemsReducer', () => { - describe('ADD_ITEM', () => { - it('should add an item', () => { - const action = { - type: 'ADD_ITEM', - item: { - type: 'comment', - data: { - content: 'stuff' - }, - item_id: '123' - }, - item_id: '123' - }; - const store = new Map({}); - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - type: 'comment', - data: { - content: 'stuff' - }, - item_id: '123' - }); - }); - }); - - describe ('UPDATE_ITEM', () => { - it ('should update an item', () => { - const action = { - type: 'UPDATE_ITEM', - property: 'stuff', - value: 'things', - item_id: '123' - }; - const store = fromJS({ - '123': { - item_id: '123', - data: { - stuff: 'morestuff' - } - } - }); - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: 'things' - } - }); - }); - }); - - describe('APPEND_ITEM_ARRAY', () => { - let action; - let store; - - beforeEach (() => { - action = { - type: 'APPEND_ITEM_ARRAY', - property: 'stuff', - value: 'things', - item_id: '123' - }; - store = fromJS({ - '123': { - item_id: '123', - data: { - stuff: ['morestuff'] - } - } - }); - }); - it ('should append to an existing array', () => { - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: ['morestuff', 'things'] - } - }); - }); - it ('should create a new array', () => { - store = fromJS({ - '123': { - item_id: '123', - data: {} - } - }); - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - data: { - stuff: ['things'] - } - }); - }); - }); - - describe('APPEND_ITEM_RELATED', () => { - let action; - let store; - - beforeEach (() => { - action = { - type: 'APPEND_ITEM_RELATED', - property: 'stuff', - value: 'things', - item_id: '123' - }; - store = fromJS({ - '123': { - item_id: '123', - related: { - stuff: ['morestuff'] - } - } - }); - }); - it ('should append to an existing array', () => { - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - related: { - stuff: ['morestuff', 'things'] - } - }); - }); - it ('should create a new array', () => { - store = fromJS({ - '123': { - item_id: '123', - related: {} - } - }); - const result = itemsReducer(store, action); - expect(result.get('123').toJS()).to.deep.equal({ - item_id: '123', - related: { - stuff: ['things'] - } - }); - }); - }); -}); diff --git a/client/coral-framework/store/actions/config.js b/client/coral-framework/store/actions/config.js index b529fc313..7eb82d428 100644 --- a/client/coral-framework/store/actions/config.js +++ b/client/coral-framework/store/actions/config.js @@ -21,7 +21,9 @@ export const fetchConfig = () => async (dispatch) => { //TODO: Replace with fetching config from backend // const response = await fetch(`./talk.config.json`) // const json = await response.json() - dispatch({type: FETCH_CONFIG_SUCCESS, config: fromJS({})}); + dispatch({type: FETCH_CONFIG_SUCCESS, config: fromJS({ + notifLength: 4500 + })}); } catch (error) { dispatch({type: FETCH_CONFIG_FAILED}); } diff --git a/client/coral-framework/store/actions/items.js b/client/coral-framework/store/actions/items.js index 56e0aafc3..b690c6f0a 100644 --- a/client/coral-framework/store/actions/items.js +++ b/client/coral-framework/store/actions/items.js @@ -21,13 +21,14 @@ export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY'; * */ -export const addItem = (item) => { +export const addItem = (item, item_type) => { if (!item.id) { console.warn('addItem called without an item id.'); } return { type: ADD_ITEM, - item: item, + item, + item_type, id: item.id }; }; @@ -42,23 +43,24 @@ export const addItem = (item) => { * value - the value that the property should be set to * */ - -export const updateItem = (id, property, value) => { +export const updateItem = (id, property, value, item_type) => { return { type: UPDATE_ITEM, id, property, - value + value, + item_type }; }; -export const appendItemArray = (id, property, value, addToFront) => { +export const appendItemArray = (id, property, value, add_to_front, item_type) => { return { type: APPEND_ITEM_ARRAY, id, property, value, - addToFront + add_to_front, + item_type }; }; @@ -80,39 +82,49 @@ export function getStream (assetId) { return fetch(`/api/v1/stream?asset_id=${assetId}`) .then( response => { - return response.ok ? response.json() : Promise.reject(`${response.status } ${ response.statusText}`); + return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`); } ) .then((json) => { - /* Sort comments by date*/ - let rootComments = []; - let childComments = {}; - json.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); - json.forEach(item => { - dispatch(addItem(item)); + /* Add items to the store */ + const itemTypes = Object.keys(json); + for (let i = 0; i < itemTypes.length; i++ ) { + for (let j = 0; j < json[itemTypes[i]].length; j++ ) { + dispatch(addItem(json[itemTypes[i]][j], itemTypes[i])); + } + } + /* 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 ( item.asset_id === assetId && !item.parent_id) { - rootComments.push(item.id); + h.rootComments.push(item.id); } else if ( item.asset_id === assetId ) { - let children = childComments[item.parent_id] || []; - childComments[item.parent_id] = children.concat(item.id); + let children = h.childComments[item.parent_id] || []; + h.childComments[item.parent_id] = children.concat(item.id); } - }, {}); + return h; + }, {rootComments: [], childComments: {}}); dispatch(addItem({ id: assetId, - comments: rootComments - })); + comments: rels.rootComments, + }, 'assets')); - const keys = Object.keys(childComments); - for (let i = 0; i < keys.length; i++ ) { - dispatch(updateItem(keys[i], 'children', childComments[keys[i]].reverse())); + 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')); + } + + /* 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')); } return (json); @@ -178,16 +190,15 @@ export function postItem (item, type, id) { 'Content-Type':'application/json' } }; - console.log('postItem', options); - return fetch(`/api/v1/${ type}`, options) + return fetch(`/api/v1/${type}`, options) .then( response => { return response.ok ? response.json() - : Promise.reject(`${response.status } ${ response.statusText}`); + : Promise.reject(`${response.status} ${response.statusText}`); } ) .then((json) => { - dispatch(addItem({...item, id:json.id})); + dispatch(addItem({...item, id:json.id}, type)); return json.id; }); }; @@ -210,24 +221,28 @@ export function postItem (item, type, id) { * */ -export function postAction (id, type, user_id) { - return (dispatch) => { +export function postAction (item_id, action_type, user_id, item_type) { + return () => { const action = { - type, + action_type, user_id }; const options = { method: 'POST', + headers: { + 'Content-Type':'application/json' + }, body: JSON.stringify(action) }; - dispatch(appendItemArray(id, type, user_id)); - return fetch(`/api/v1/comments/${ id }/actions`, options) + return fetch(`/api/v1/${item_type}/${item_id}/actions`, options) .then( response => { - return response.ok ? response.text() - : Promise.reject(`${response.status } ${ response.statusText}`); + return response.ok ? response.json() + : Promise.reject(`${response.status} ${response.statusText}`); } - ); + ).then((json)=>{ + return json; + }); }; } diff --git a/client/coral-framework/store/reducers/items.js b/client/coral-framework/store/reducers/items.js index 77ee44130..17569c545 100644 --- a/client/coral-framework/store/reducers/items.js +++ b/client/coral-framework/store/reducers/items.js @@ -3,26 +3,27 @@ import {fromJS} from 'immutable'; import * as actions from '../actions/items'; -const initialState = fromJS({}); +const initialState = fromJS({ + comments: {}, + users: {}, + actions: {} +}); export default (state = initialState, action) => { switch (action.type) { case actions.ADD_ITEM: - return state.set(action.id, fromJS(action.item)); + return state.setIn([action.item_type, action.id], fromJS(action.item)); case actions.UPDATE_ITEM: - return state.updateIn([action.id, action.property], () => - fromJS(action.value) - ); + return state.setIn([action.item_type, action.id, action.property], fromJS(action.value)); case actions.APPEND_ITEM_ARRAY: - return state.updateIn([action.id, action.property], (prop) => { - if (action.addToFront) { - return prop ? prop.unshift(action.value) : fromJS([action.value]); + return state.updateIn([action.item_type, action.id, action.property], (prop) => { + console.log(prop); + if (action.add_to_front) { + return prop ? prop.unshift(fromJS(action.value)) : fromJS([action.value]); } else { - return prop ? prop.push(action.value) : fromJS([action.value]); + return prop ? prop.push(fromJS(action.value)) : fromJS([action.value]); } - - } - ); + }); default: return state; } diff --git a/client/coral-plugin-commentbox/CommentBox.js b/client/coral-plugin-commentbox/CommentBox.js index cf428fac2..a4e32d73b 100644 --- a/client/coral-plugin-commentbox/CommentBox.js +++ b/client/coral-plugin-commentbox/CommentBox.js @@ -26,16 +26,19 @@ class CommentBox extends Component { username: this.state.username }; let related; + let parent_type; if (parent_id) { comment.parent_id = parent_id; related = 'children'; + parent_type = 'comments'; } else { related = 'comments'; + parent_type = 'assets'; } - updateItem(parent_id, 'showReply', false); + updateItem(parent_id, 'showReply', false, 'comments'); postItem(comment, 'comments') .then((comment_id) => { - appendItemArray((parent_id || id, related, comment_id, parent_id)); + appendItemArray(parent_id || id, related, comment_id, !parent_id, parent_type); addNotification('success', 'Your comment has been posted.'); }).catch((err) => console.error(err)); this.setState({body: ''}); @@ -45,9 +48,9 @@ class CommentBox extends Component { const {styles, reply} = this.props; // How to handle language in plugins? Should we have a dependency on our central translation file? return
-
+
this.setState({username: e.target.value})}/>
+ className={`${name}-container`}>