diff --git a/client/coral-admin/src/actions/comments.js b/client/coral-admin/src/actions/comments.js index d4aee034e..f89cb5dd3 100644 --- a/client/coral-admin/src/actions/comments.js +++ b/client/coral-admin/src/actions/comments.js @@ -1,3 +1,72 @@ +import coralApi from '../../../coral-framework/helpers/response'; +import * as actions from '../constants/comments'; + + +// Get comments to fill each of the three lists on the mod queue +export const fetchModerationQueueComments = () => { + return dispatch => { + + return Promise.all([ + coralApi('/queue/comments/pending'), + coralApi('/comments?status=rejected'), + coralApi('/comments?action_type=flag') + ]) + .then(([pending, rejected, flagged]) => { + + /* Combine seperate calls into a single object */ + let all = {}; + all.comments = pending.comments + .concat(rejected.comments) + .concat(flagged.comments.map(comment => { + comment.flagged = true; + return comment; + })); + all.users = pending.users + .concat(rejected.users) + .concat(flagged.users); + all.actions = pending.actions + .concat(rejected.actions) + .concat(flagged.actions); + return all; + }) + .then(all => { + + /* Post comments and users to redux store. Actions will be posted when they are needed. */ + dispatch({type: actions.USERS_MODERATION_QUEUE_FETCH_SUCCESS, + users: all.users}); + dispatch({type: actions.COMMENTS_MODERATION_QUEUE_FETCH_SUCCESS, + comments: all.comments}); + + }); + }; +}; + + +// .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 + +export const updateComment = (store, comment) => { + coralApi(`/comments/${comment.get('id')}/status`, {method: 'PUT', body: {status: comment.get('status')}}) + .then(res => store.dispatch({type: 'COMMENT_UPDATE_SUCCESS', res})) + .catch(error => store.dispatch({type: 'COMMENT_UPDATE_FAILED', error})); +}; + +// Create a new comment +export const createComment = (name, body) => { + return dispatch => { + const comment = { + status: 'Untouched', + body, + name + }; + return coralApi('/comments', {method: 'POST', comment}) + .then(res => dispatch({type: actions.COMMENT_CREATE_SUCCESS, comment: res})) + .catch(error => dispatch({type: actions.COMMENT_CREATE_FAILED, error})); + }; +}; + + /** * Action disptacher related to comments */ @@ -12,10 +81,6 @@ export const flagComment = id => (dispatch, getState) => { dispatch({type: 'COMMENT_UPDATE', comment: getState().comments.get('byId').get(id)}); }; -export const createComment = (name, body) => dispatch => { - dispatch({type: 'COMMENT_CREATE', name, body}); -}; - // Dialog Actions export const showBanUserDialog = (userId, userName, commentId) => { return dispatch => { diff --git a/client/coral-admin/src/actions/users.js b/client/coral-admin/src/actions/users.js index f2ff37cbd..f1c9e6972 100644 --- a/client/coral-admin/src/actions/users.js +++ b/client/coral-admin/src/actions/users.js @@ -1,14 +1,20 @@ +import coralApi from '../../../coral-framework/helpers/response'; /** * Action disptacher related to users */ -// -// export const banUser = (status, author_id) => (dispatch) => { -// dispatch({type: 'USER_STATUS_UPDATE', author_id, status}); +// export const banUser = (status, userId, commentId) => { +// return dispatch => { +// dispatch({type: 'USER_BAN', status, userId, commentId}); +// dispatch({type: 'COMMENTS_MODERATION_QUEUE_FETCH'}); +// }; // }; -export const banUser = (status, userId, commentId) => { + +// change status of a user +export const userStatusUpdate = (status, userId, commentId) => { return dispatch => { - dispatch({type: 'USER_BAN', status, userId, commentId}); - dispatch({type: 'COMMENTS_MODERATION_QUEUE_FETCH'}); + return coralApi(`/users/${userId}/status`, {method: 'POST', body: {status: status, comment_id: commentId}}) + .then(res => dispatch({type: 'USER_BAN_SUCESS', res})) + .catch(error => dispatch({type: 'USER_BAN_FAILED', error})); }; }; diff --git a/client/coral-admin/src/constants/comments.js b/client/coral-admin/src/constants/comments.js index 856f619d0..db954ad2c 100644 --- a/client/coral-admin/src/constants/comments.js +++ b/client/coral-admin/src/constants/comments.js @@ -1,3 +1,8 @@ export const SHOW_BANUSER_DIALOG = 'SHOW_BANUSER_DIALOG'; export const HIDE_BANUSER_DIALOG = 'HIDE_BANUSER_DIALOG'; export const USER_BAN_SUCESS = 'USER_BAN_SUCESS'; +export const USERS_MODERATION_QUEUE_FETCH_SUCCESS = 'USERS_MODERATION_QUEUE_FETCH_SUCCESS'; +export const COMMENTS_MODERATION_QUEUE_FETCH_SUCCESS = 'COMMENTS_MODERATION_QUEUE_FETCH_SUCCESS'; +export const COMMENT_CREATE_SUCCESS = 'COMMENT_CREATE_SUCCESS'; +export const COMMENT_CREATE_FAILED = 'COMMENT_CREATE_FAILED'; +export const COMMENT_STREAM_FETCH_SUCCESS = 'COMMENT_STREAM_FETCH_SUCCESS'; diff --git a/client/coral-admin/src/containers/ModerationQueue/ModerationQueue.js b/client/coral-admin/src/containers/ModerationQueue/ModerationQueue.js index e0e3c636f..3df0d210b 100644 --- a/client/coral-admin/src/containers/ModerationQueue/ModerationQueue.js +++ b/client/coral-admin/src/containers/ModerationQueue/ModerationQueue.js @@ -6,8 +6,13 @@ import ModerationKeysModal from 'components/ModerationKeysModal'; import CommentList from 'components/CommentList'; import BanUserDialog from 'components/BanUserDialog'; -import {updateStatus, showBanUserDialog, hideBanUserDialog} from 'actions/comments'; -import {banUser} from 'actions/users'; +import { + updateStatus, + showBanUserDialog, + hideBanUserDialog, + fetchModerationQueueComments +} from 'actions/comments'; +import {userStatusUpdate} from 'actions/users'; import styles from './ModerationQueue.css'; import I18n from 'coral-framework/modules/i18n/i18n'; @@ -31,7 +36,7 @@ class ModerationQueue extends React.Component { // Fetch comments and bind singleView key before render componentWillMount () { - this.props.dispatch({type: 'COMMENTS_MODERATION_QUEUE_FETCH'}); + this.props.dispatch(fetchModerationQueueComments()); key('s', () => this.setState({singleView: !this.state.singleView})); key('shift+/', () => this.setState({modalOpen: true})); key('esc', () => this.setState({modalOpen: false})); @@ -67,7 +72,10 @@ class ModerationQueue extends React.Component { } banUser (userId, commentId) { - this.props.dispatch(banUser('banned', userId, commentId)); + this.props.dispatch(userStatusUpdate('banned', userId, commentId)) + .then(() => { + this.props.dispatch(fetchModerationQueueComments()); + }); } onTabClick (activeTab) { diff --git a/client/coral-admin/src/reducers/comments.js b/client/coral-admin/src/reducers/comments.js index dca726b5e..b9f77692e 100644 --- a/client/coral-admin/src/reducers/comments.js +++ b/client/coral-admin/src/reducers/comments.js @@ -28,8 +28,8 @@ export default (state = initialState, action) => { case 'COMMENTS_MODERATION_QUEUE_FAILED': return state.set('loading', false); case 'COMMENT_STATUS_UPDATE': return updateStatus(state, action); case 'COMMENT_FLAG': return flag(state, action); - case 'COMMENT_CREATE_SUCCESS': return addComment(state, action); - case 'COMMENT_STREAM_FETCH_SUCCESS': return replaceComments(action, state); + case actions.COMMENT_CREATE_SUCCESS: return addComment(state, action); + case actions.COMMENT_STREAM_FETCH_SUCCESS: return replaceComments(action, state); case actions.SHOW_BANUSER_DIALOG: return setBanUser(state, true, action); case actions.HIDE_BANUSER_DIALOG: return setBanUser(state, false, action); case actions.USER_BAN_SUCESS: return setBanUser(state, false, action); diff --git a/client/coral-admin/src/services/talk-adapter.js b/client/coral-admin/src/services/talk-adapter.js index 7c9bc968f..6c9f3978c 100644 --- a/client/coral-admin/src/services/talk-adapter.js +++ b/client/coral-admin/src/services/talk-adapter.js @@ -12,59 +12,14 @@ import coralApi from '../../../coral-framework/helpers/response'; export default store => next => action => { switch (action.type) { - case 'COMMENTS_MODERATION_QUEUE_FETCH': - fetchModerationQueueComments(store); - break; case 'COMMENT_UPDATE': updateComment(store, action.comment); break; - case 'COMMENT_CREATE': - createComment(store, action.name, action.body); - break; - case 'USER_BAN': - userStatusUpdate(store, action.status, action.userId, action.commentId); - break; } next(action); }; -// Get comments to fill each of the three lists on the mod queue -const fetchModerationQueueComments = store => - -Promise.all([ - coralApi('/queue/comments/pending'), - coralApi('/comments?status=rejected'), - coralApi('/comments?action_type=flag') -]) -.then(([pending, rejected, flagged]) => { - - /* Combine seperate calls into a single object */ - let all = {}; - all.comments = pending.comments - .concat(rejected.comments) - .concat(flagged.comments.map(comment => { - comment.flagged = true; - return comment; - })); - all.users = pending.users - .concat(rejected.users) - .concat(flagged.users); - all.actions = pending.actions - .concat(rejected.actions) - .concat(flagged.actions); - return all; -}) -.then(all => { - - /* Post comments and users to redux store. Actions will be posted when they are needed. */ - store.dispatch({type: 'USERS_MODERATION_QUEUE_FETCH_SUCCESS', - users: all.users}); - store.dispatch({type: 'COMMENTS_MODERATION_QUEUE_FETCH_SUCCESS', - comments: all.comments}); - -}); - // .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 @@ -74,23 +29,3 @@ const updateComment = (store, comment) => { .then(res => store.dispatch({type: 'COMMENT_UPDATE_SUCCESS', res})) .catch(error => store.dispatch({type: 'COMMENT_UPDATE_FAILED', error})); }; - -// Create a new comment -const createComment = (store, name, comment) => { - const body = { - status: 'Untouched', - body: comment, - name: name, - createdAt: Date.now() - }; - return coralApi('/comments', {method: 'POST', body}) - .then(res => store.dispatch({type: 'COMMENT_CREATE_SUCCESS', comment: res})) - .catch(error => store.dispatch({type: 'COMMENT_CREATE_FAILED', error})); -}; - -// Ban a user -const userStatusUpdate = (store, status, userId, commentId) => { - return coralApi(`/users/${userId}/status`, {method: 'POST', body: {status: status, comment_id: commentId}}) - .then(res => store.dispatch({type: 'USER_BAN_SUCESS', res})) - .catch(error => store.dispatch({type: 'USER_BAN_FAILED', error})); -};