From f2dccf7845facb6d1488d67010fd1a00e13e8faa Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 24 Nov 2017 18:36:59 -0300 Subject: [PATCH] Adding addUserRole and removeUserRole --- client/coral-admin/src/actions/community.js | 8 ---- client/coral-admin/src/constants/community.js | 1 - .../src/routes/Community/containers/People.js | 20 +++++++--- client/coral-framework/graphql/fragments.js | 1 + client/coral-framework/graphql/mutations.js | 40 +++++++++++++++++++ graph/typeDefs.graphql | 3 ++ 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/client/coral-admin/src/actions/community.js b/client/coral-admin/src/actions/community.js index 875cc6cbe..669ff2f95 100644 --- a/client/coral-admin/src/actions/community.js +++ b/client/coral-admin/src/actions/community.js @@ -7,7 +7,6 @@ import { SORT_UPDATE, SET_PAGE, SET_SEARCH_VALUE, - SET_ROLE, SHOW_BANUSER_DIALOG, HIDE_BANUSER_DIALOG, SHOW_REJECT_USERNAME_DIALOG, @@ -55,13 +54,6 @@ export const setSearchValue = (value) => ({ value, }); -export const setRole = (id, role) => (dispatch, _, {rest}) => { - return rest(`/users/${id}/role`, {method: 'POST', body: {role}}) - .then(() => { - return dispatch({type: SET_ROLE, id, role}); - }); -}; - // Ban User Dialog export const showBanUserDialog = (user) => ({type: SHOW_BANUSER_DIALOG, user}); export const hideBanUserDialog = () => ({type: HIDE_BANUSER_DIALOG}); diff --git a/client/coral-admin/src/constants/community.js b/client/coral-admin/src/constants/community.js index e118b0824..a9cc2f1e3 100644 --- a/client/coral-admin/src/constants/community.js +++ b/client/coral-admin/src/constants/community.js @@ -6,7 +6,6 @@ export const FETCH_USERS_FAILURE = `${prefix}_FETCH_USERS_FAILURE`; export const SORT_UPDATE = `${prefix}_SORT_UPDATE`; export const SET_PAGE = `${prefix}_SET_PAGE`; -export const SET_ROLE = `${prefix}_SET_ROLE`; export const FETCH_FLAGGED_COMMENTERS_REQUEST = `${prefix}_FETCH_FLAGGED_COMMENTERS_REQUEST`; export const FETCH_FLAGGED_COMMENTERS_SUCCESS = `${prefix}_FETCH_FLAGGED_COMMENTERS_SUCCESS`; diff --git a/client/coral-admin/src/routes/Community/containers/People.js b/client/coral-admin/src/routes/Community/containers/People.js index 48a5acdd6..d08c15e25 100644 --- a/client/coral-admin/src/routes/Community/containers/People.js +++ b/client/coral-admin/src/routes/Community/containers/People.js @@ -4,7 +4,7 @@ import {bindActionCreators} from 'redux'; import {compose} from 'react-apollo'; import People from '../components/People'; import PropTypes from 'prop-types'; -import {withUnBanUser, withBanUser} from 'coral-framework/graphql/mutations'; +import {withUnBanUser, withBanUser, withAddUserRole, withRemoveUserRole} from 'coral-framework/graphql/mutations'; import {viewUserDetail} from '../../../actions/userDetail'; @@ -13,7 +13,6 @@ import { updateSorting, setPage, hideRejectUsernameDialog, - setRole, setSearchValue, } from '../../../actions/community'; @@ -66,7 +65,13 @@ class PeopleContainer extends React.Component { setUserBanStatus = async (id, bannedStatus) => { const {banUser, unBanUser} = this.props; await bannedStatus ? banUser({id, message: ''}) : unBanUser({id}); - this.fetchUsers(); + await this.fetchUsers(); + } + + setRole = async (id, role) => { + const {addUserRole, removeUserRole} = this.props; + await role ? addUserRole(id, role) : removeUserRole(id, role); + await this.fetchUsers(); } render() { @@ -78,7 +83,8 @@ class PeopleContainer extends React.Component { onPageChange={this.onPageChange} totalPages={this.props.community.totalPagesPeople} setUserBanStatus={this.setUserBanStatus} - setRole={this.props.setRole} + setRole={this.setRole} + removeUserRole={this.props.removeUserRole} page={this.props.community.pagePeople} viewUserDetail={this.props.viewUserDetail} />; @@ -89,7 +95,8 @@ PeopleContainer.propTypes = { setPage: PropTypes.func, fetchUsers: PropTypes.func, updateSorting: PropTypes.func, - setRole: PropTypes.func.isRequired, + addUserRole: PropTypes.func.isRequired, + removeUserRole: PropTypes.func.isRequired, banUser: PropTypes.func.isRequired, unBanUser: PropTypes.func.isRequired, setSearchValue: PropTypes.func.isRequired, @@ -103,7 +110,6 @@ const mapDispatchToProps = (dispatch) => fetchUsers, updateSorting, hideRejectUsernameDialog, - setRole, viewUserDetail, setSearchValue, }, dispatch); @@ -112,4 +118,6 @@ export default compose( connect(null, mapDispatchToProps), withBanUser, withUnBanUser, + withAddUserRole, + withRemoveUserRole, )(PeopleContainer); diff --git a/client/coral-framework/graphql/fragments.js b/client/coral-framework/graphql/fragments.js index aad038422..4981cb972 100644 --- a/client/coral-framework/graphql/fragments.js +++ b/client/coral-framework/graphql/fragments.js @@ -3,6 +3,7 @@ import {createDefaultResponseFragments} from '../utils'; // fragments defined here are automatically registered. export default { ...createDefaultResponseFragments( + 'ModifyUserRoleResponse', 'ChangeUsernameResponse', 'BanUsersResponse', 'UnBanUserResponse', diff --git a/client/coral-framework/graphql/mutations.js b/client/coral-framework/graphql/mutations.js index 021984ff9..0205f6e21 100644 --- a/client/coral-framework/graphql/mutations.js +++ b/client/coral-framework/graphql/mutations.js @@ -254,6 +254,46 @@ export const withUnBanUser = withMutation( }), }); +export const withAddUserRole = withMutation( + gql` + mutation AddUserRole($id: ID!, $role: USER_ROLES!) { + addUserRole(id: $id, role: $role) { + ...ModifyUserRoleResponse + } + } + `, { + props: ({mutate}) => ({ + addUserRole: (id, role) => { + return mutate({ + variables: { + id, + role, + }, + }); + } + }), + }); + +export const withRemoveUserRole = withMutation( + gql` + mutation RemoveUserRole($id: ID!, $role: USER_ROLES!) { + removeUserRole(id: $id, role: $role) { + ...ModifyUserRoleResponse + } + } + `, { + props: ({mutate}) => ({ + removeUserRole: (id, role) => { + return mutate({ + variables: { + id, + role, + }, + }); + } + }), + }); + export const withPostComment = withMutation( gql` mutation PostComment($input: CreateCommentInput!) { diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 8d9224768..689d3d6bf 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -37,6 +37,9 @@ enum USER_ROLES { # a moderator of the site MODERATOR + + # a staff of the site + STAFF } # Token is a personal access token associated with a given user.