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/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..a36fcbd3a 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "mongoose": "^4.6.5", "morgan": "^1.7.0", "prompt": "^1.0.0", + "react-mdl-selectfield": "^0.2.0", "uuid": "^2.0.3" }, "devDependencies": { 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;