diff --git a/client/coral-admin/src/actions/community.js b/client/coral-admin/src/actions/community.js index 06c8ab0f6..7a4112f8b 100644 --- a/client/coral-admin/src/actions/community.js +++ b/client/coral-admin/src/actions/community.js @@ -5,7 +5,8 @@ import { FETCH_COMMENTERS_SUCCESS, FETCH_COMMENTERS_FAILURE, SORT_UPDATE, - COMMENTERS_NEW_PAGE + COMMENTERS_NEW_PAGE, + SET_ROLE } from '../constants/community'; import {base, getInit, handleResp} from '../helpers/response'; @@ -40,3 +41,9 @@ export const newPage = () => ({ type: COMMENTERS_NEW_PAGE }); +export const setRole = (id, role) => dispatch => { + return fetch(`${base}/user/${id}/role`, getInit('POST', {role})) + .then(() => { + return dispatch({type: SET_ROLE, id, role}); + }); +}; diff --git a/client/coral-admin/src/constants/community.js b/client/coral-admin/src/constants/community.js index e628a14d6..2ea77ea77 100644 --- a/client/coral-admin/src/constants/community.js +++ b/client/coral-admin/src/constants/community.js @@ -3,3 +3,4 @@ export const FETCH_COMMENTERS_SUCCESS = 'FETCH_COMMENTERS_SUCCESS'; export const FETCH_COMMENTERS_FAILURE = 'FETCH_COMMENTERS_FAILURE'; export const SORT_UPDATE = 'SORT_UPDATE'; export const COMMENTERS_NEW_PAGE = 'COMMENTERS_NEW_PAGE'; +export const SET_ROLE = 'SET_ROLE'; diff --git a/client/coral-admin/src/containers/Community/Community.js b/client/coral-admin/src/containers/Community/Community.js index 8e0b955a4..fb6f5df8c 100644 --- a/client/coral-admin/src/containers/Community/Community.js +++ b/client/coral-admin/src/containers/Community/Community.js @@ -19,6 +19,10 @@ const tableHeaders = [ { title: lang.t('community.account_creation_date'), field: 'created_at' + }, + { + title: lang.t('community.newsroom_role'), + field: 'role' } ]; diff --git a/client/coral-admin/src/containers/Community/Table.js b/client/coral-admin/src/containers/Community/Table.js index a15a88723..97848bc9a 100644 --- a/client/coral-admin/src/containers/Community/Table.js +++ b/client/coral-admin/src/containers/Community/Table.js @@ -1,34 +1,66 @@ -import React from 'react'; +import React, {Component} from 'react'; +import {connect} from 'react-redux'; +import {SelectField, Option} from 'react-mdl-selectfield'; import styles from './Community.css'; +import I18n from 'coral-framework/i18n/i18n'; +import translations from '../../translations'; +import {setRole} from '../../actions/community'; -const Table = ({headers, data, onHeaderClickHandler}) => ( - - - - {headers.map((header, i) =>( - - ))} - - - - {data.map((row, i)=> ( - - - - - ))} - -
onHeaderClickHandler({field: header.field})}> - {header.title} -
- {row.displayName} - {row.profiles.map(({id}) => id)} - - {row.created_at} -
-); +const lang = new I18n(translations); -export default Table; +class Table extends Component { + + constructor (props) { + super(props); + this.onRoleChange = this.onRoleChange.bind(this); + } + + onRoleChange (id, role) { + this.props.dispatch(setRole(id, role)); + } + + render () { + const {headers, commenters, onHeaderClickHandler} = this.props; + + return ( + + + + {headers.map((header, i) =>( + + ))} + + + + {commenters.map((row, i)=> ( + + + + + + ))} + +
onHeaderClickHandler({field: header.field})}> + {header.title} +
+ {row.displayName} + {row.profiles.map(({id}) => id)} + + {row.created_at} + + this.onRoleChange(row.id, role)}> + + + + +
+ ); + } +} + +export default connect(state => ({commenters: state.community.get('commenters')}))(Table); diff --git a/client/coral-admin/src/reducers/community.js b/client/coral-admin/src/reducers/community.js index 8d5dfd2c3..81a09a1cc 100644 --- a/client/coral-admin/src/reducers/community.js +++ b/client/coral-admin/src/reducers/community.js @@ -4,7 +4,8 @@ import { FETCH_COMMENTERS_REQUEST, FETCH_COMMENTERS_FAILURE, FETCH_COMMENTERS_SUCCESS, - SORT_UPDATE + SORT_UPDATE, + SET_ROLE } from '../constants/community'; const initialState = Map({ @@ -37,6 +38,13 @@ export default function community (state = initialState, action) { }) .set('commenters', commenters); // Sets to normal array } + case SET_ROLE : { + const commenters = state.get('commenters'); + const idx = commenters.findIndex(el => el.id === action.id); + + commenters[idx].roles[0] = action.role; + return state.set('commenters', commenters.map(id => id)); + } case SORT_UPDATE : return state .set('field', action.sort.field) diff --git a/client/coral-admin/src/translations.js b/client/coral-admin/src/translations.js index 67a8142fd..fa655294a 100644 --- a/client/coral-admin/src/translations.js +++ b/client/coral-admin/src/translations.js @@ -2,7 +2,11 @@ export default { en: { 'community': { username_and_email: 'Username and Email', - account_creation_date: 'Account Creation Date' + account_creation_date: 'Account Creation Date', + newsroom_role: 'Newsroom Role', + admin: 'Administrator', + moderator: 'Moderator', + role: 'Select role...' }, 'modqueue': { 'pending': 'pending', @@ -30,7 +34,11 @@ export default { es: { 'community': { username_and_email: 'Usuario y E-mail', - account_creation_date: 'Fecha de creación de la cuenta' + account_creation_date: 'Fecha de creación de la cuenta', + newsroom_role: 'Rol en la redacción', + admin: 'Administrador', + moderator: 'Moderador', + role: 'Select role...' }, 'modqueue': { 'pending': 'pendiente', diff --git a/client/coral-framework/store/actions/items.js b/client/coral-framework/store/actions/items.js index eea3315b0..2dad64639 100644 --- a/client/coral-framework/store/actions/items.js +++ b/client/coral-framework/store/actions/items.js @@ -8,6 +8,23 @@ export const ADD_ITEM = 'ADD_ITEM'; export const UPDATE_ITEM = 'UPDATE_ITEM'; export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY'; +const getInit = (method, body) => { + const headers = { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }; + + const init = {method, headers}; + if (method.toLowerCase() !== 'get') { + init.body = JSON.stringify(body); + } + + return init; +}; + +const responseHandler = response => { + return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`); +}; /** * Action creators */ @@ -80,11 +97,7 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) => export function getStream (assetUrl) { return (dispatch) => { return fetch(`/api/v1/stream?asset_url=${encodeURIComponent(assetUrl)}`) - .then( - response => { - return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`); - } - ) + .then(responseHandler) .then((json) => { /* Add items to the store */ @@ -147,13 +160,8 @@ export function getStream (assetUrl) { export function getItemsArray (ids) { return (dispatch) => { - return fetch(`/v1/item/${ids}`) - .then( - response => { - return response.ok ? response.json() - : Promise.reject(`${response.status } ${ response.statusText}`); - } - ) + return fetch(`/v1/item/${ids}`, getInit('GET')) + .then(responseHandler) .then((json) => { for (let i = 0; i < json.items.length; i++) { dispatch(addItem(json.items[i])); @@ -182,20 +190,8 @@ export function postItem (item, type, id) { if (id) { item.id = id; } - let options = { - method: 'POST', - body: JSON.stringify(item), - headers: { - 'Content-Type':'application/json' - } - }; - return fetch(`/api/v1/${type}`, options) - .then( - response => { - return response.ok ? response.json() - : Promise.reject(`${response.status} ${response.statusText}`); - } - ) + return fetch(`/api/v1/${type}`, getInit('POST', item)) + .then(responseHandler) .then((json) => { dispatch(addItem({...item, id:json.id}, type)); return json.id; @@ -226,21 +222,9 @@ export function postAction (item_id, action_type, user_id, item_type) { action_type, user_id }; - const options = { - method: 'POST', - headers: { - 'Content-Type':'application/json' - }, - body: JSON.stringify(action) - }; - return fetch(`/api/v1/${item_type}/${item_id}/actions`, options) - .then( - response => { - return response.ok ? response.json() - : Promise.reject(`${response.status} ${response.statusText}`); - } - ); + return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('POST', action)) + .then(responseHandler); }; } @@ -265,20 +249,8 @@ export function deleteAction (item_id, action_type, user_id, item_type) { action_type, user_id }; - const options = { - method: 'DELETE', - headers: { - 'Content-Type':'application/json' - }, - body: JSON.stringify(action) - }; - return fetch(`/api/v1/${item_type}/${item_id}/actions`, options) - .then( - response => { - return response.ok ? response.text() - : Promise.reject(`${response.status} ${response.statusText}`); - } - ); + return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('DELETE', action)) + .then(responseHandler); }; } diff --git a/models/comment.js b/models/comment.js index 492c06e9c..d17e98860 100644 --- a/models/comment.js +++ b/models/comment.js @@ -194,12 +194,12 @@ CommentSchema.statics.removeById = function(id) { * @param {String} action_type the type of the action to be removed * @param {String} user_id the id of the user performing the action */ -CommentSchema.statics.removeAction = function(id, user_id, action_type) { +CommentSchema.statics.removeAction = function(item_id, user_id, action_type) { return Action.remove({ - action_type: action_type, + action_type, item_type: 'comment', - item_id: id, - user_id: user_id + item_id, + user_id }); }; diff --git a/models/user.js b/models/user.js index c0a846f1a..c6197a220 100644 --- a/models/user.js +++ b/models/user.js @@ -24,7 +24,9 @@ const UserSchema = new mongoose.Schema({ required: true } }], - roles: [String] + roles: { + type: [{type: String, enum: ['admin', 'moderator']}] + } }, { timestamps: { createdAt: 'created_at', diff --git a/package.json b/package.json index cbdaad174..354340ae1 100644 --- a/package.json +++ b/package.json @@ -5,25 +5,20 @@ "main": "app.js", "scripts": { "start": "./bin/www", - "build": "webpack --config webpack.config.js --bail", - "build-watch": "webpack --config webpack.config.dev.js --watch", + "build": "NODE_ENV=production webpack --config webpack.config.js --bail", + "build-watch": "NODE_ENV=development webpack --config webpack.config.dev.js --watch", "lint": "eslint bin/* .", "lint-fix": "eslint . --fix", "pretest": "npm install", "test": "mocha --compilers js:babel-core/register --recursive tests", "test-watch": "mocha --compilers js:babel-core/register --recursive -w tests", - "embed-start": "npm run build && ./bin/www" + "embed-start": "NODE_ENV=development npm run build && ./bin/www" }, "config": { "pre-git": { "commit-msg": [], - "pre-commit": [ - "npm run lint", - "npm test" - ], - "pre-push": [ - "npm test" - ], + "pre-commit": ["npm run lint", "npm test"], + "pre-push": ["npm test"], "post-commit": [], "post-merge": [] } @@ -32,12 +27,7 @@ "type": "git", "url": "git+https://github.com/coralproject/talk.git" }, - "keywords": [ - "talk", - "coral", - "coralproject", - "ask" - ], + "keywords": ["talk", "coral", "coralproject", "ask"], "author": "", "license": "Apache-2.0", "bugs": { @@ -103,6 +93,7 @@ "react": "15.3.2", "react-dom": "15.3.2", "react-mdl": "^1.7.2", + "react-mdl-selectfield": "^0.2.0", "react-onclickoutside": "^5.7.1", "react-redux": "^4.4.5", "react-router": "^3.0.0", diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index eabb1be7b..99957e987 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -126,7 +126,7 @@ router.delete('/:comment_id', (req, res, next) => { Comment .removeById(req.params.comment_id) .then(() => { - res.status(201).send('OK. Removed'); + res.status(201).send({}); }) .catch(error => { next(error); @@ -134,10 +134,11 @@ router.delete('/:comment_id', (req, res, next) => { }); router.delete('/:comment_id/actions', (req, res, next) => { + console.log(req.params); Comment .removeAction(req.params.comment_id, req.body.user_id, req.body.action_type) .then(() => { - res.status(201).send('OK. Removed'); + res.status(201).send({}); }) .catch(error => { next(error); diff --git a/routes/api/user/index.js b/routes/api/user/index.js index 18872eab7..4665f4e52 100644 --- a/routes/api/user/index.js +++ b/routes/api/user/index.js @@ -40,11 +40,13 @@ router.get('/', (req, res, next) => { ]) .then(([data, count]) => { const users = data.map((user) => { - const {displayName, created_at} = user; + const {id, displayName, created_at} = user; return { + id, displayName, created_at, - profiles: user.toObject().profiles + profiles: user.toObject().profiles, + roles: user.toObject().roles }; }); @@ -60,4 +62,12 @@ router.get('/', (req, res, next) => { .catch(next); }); +router.post('/:user_id/role', (req, res, next) => { + User.addRoleToUser(req.params.user_id, req.body.role) + .then(role => { + res.send(role); + }) + .catch(next); +}); + module.exports = router; diff --git a/tests/client/coral-framework/store/itemActions.spec.js b/tests/client/coral-framework/store/itemActions.spec.js index d6ce0c970..14da38b41 100644 --- a/tests/client/coral-framework/store/itemActions.spec.js +++ b/tests/client/coral-framework/store/itemActions.spec.js @@ -122,6 +122,7 @@ describe('itemActions', () => { { method: 'POST', headers: { + 'Accept': 'application/json', 'Content-Type':'application/json' }, body: JSON.stringify(item.data) @@ -169,11 +170,11 @@ describe('itemActions', () => { describe('deleteAction', () => { it ('should remove an action', () => { - fetchMock.delete('*', 'Action removed.'); + fetchMock.delete('*', {}); return actions.deleteAction('abc', 'flag', '123', 'comments')(store.dispatch) .then(response => { expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/comments/abc/actions'); - expect(response).to.equal('Action removed.'); + expect(response).to.deep.equal({}); }); }); diff --git a/webpack.config.dev.js b/webpack.config.dev.js index 8e2f253e5..571c31625 100644 --- a/webpack.config.dev.js +++ b/webpack.config.dev.js @@ -85,7 +85,7 @@ module.exports = { }), new webpack.DefinePlugin({ 'process.env': { - 'NODE_ENV': `"${'development'}"`, + 'NODE_ENV': `"${process.env.NODE_ENV}"`, 'VERSION': `"${require('./package.json').version}"` } }) diff --git a/webpack.config.js b/webpack.config.js index bf602733f..46cd2961b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,12 +5,6 @@ const devConfig = require('./webpack.config.dev'); devConfig.devtool = null; devConfig.plugins = devConfig.plugins.concat([ - new webpack.DefinePlugin({ - 'process.env': { - 'NODE_ENV': `"${'production'}"`, - 'VERSION': `"${require('./package.json').version}"` - } - }), new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false