Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement change role feature #62

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 59 additions & 44 deletions assets/src/components/admin/Users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react"
import { Query } from "react-apollo"
import { Mutation, Query } from "react-apollo"

import Paper from "@material-ui/core/Paper"
import Table from "@material-ui/core/Table"
Expand All @@ -15,6 +15,8 @@ import Loader from "components/Loader"
import EditRecord from "./EditRecord"

import { LIST_USERS } from "queries/users"
import { UPDATE_USER } from "mutations/user"
import { CURRENT_USER } from "queries/users"

import {
page,
Expand All @@ -34,8 +36,10 @@ export default class Users extends Component {
pageSize: 10
}

handleEditClick = user => e => {
this.setState({ user, isEditing: true })
handleChangeRoleClick = ( user, UpdateUser ) => e => {
UpdateUser({
variables: { id: parseInt(user.id), admin: user.admin }
})
}

onClose = () => this.setState({ isEditing: false })
Expand Down Expand Up @@ -78,47 +82,58 @@ export default class Users extends Component {
editableFields={this.state.editableFields}
/>
)}
<Table>
<TableHead>
<TableRow>
<TableCell>ID</TableCell>
<TableCell>Email</TableCell>
<TableCell align="right">Admin</TableCell>
{/* <TableCell align="right">Actions</TableCell> */}
</TableRow>
</TableHead>
<TableBody>
{users.entries.map(user => (
<TableRow key={user.email} hover={true}>
<TableCell>{user.id}</TableCell>
<TableCell>{user.email}</TableCell>
<TableCell align="right">
{(user.admin && "Yes") || "-"}
</TableCell>
{/* <TableCell align="right">
<Button
variant="text"
onClick={this.handleEditClick(user)}
>
Edit
</Button>
</TableCell> */}
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TablePagination
count={totalEntries}
page={currentPage - 1}
rowsPerPage={pageSize(this.props)}
onChangePage={onChangePage(this.props, 1)}
onChangeRowsPerPage={onChangeRowsPerPage(this.props)}
rowsPerPageOptions={this.state.rowOptions}
/>
</TableRow>
</TableFooter>
</Table>
<Query query={CURRENT_USER}>
{({ data, error, loading }) => {
const currentuser = data.currentUser

return (<Table>
<TableHead>
<TableRow>
<TableCell>ID</TableCell>
<TableCell>Email</TableCell>
<TableCell align="right">Admin</TableCell>
<TableCell align="right">Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{users.entries.map(user => (
<TableRow key={user.email + user.admin} hover={true}>
<TableCell>{user.id}</TableCell>
<TableCell>{user.email}</TableCell>
<TableCell align="right">
{(user.admin && "Yes") || "No"}
</TableCell>
<TableCell align="right">
<Mutation mutation={UPDATE_USER}>
{(UpdateUser, { data }) => (
<Button
variant="text"
onClick={this.handleChangeRoleClick(user, UpdateUser)}
disabled={(user.id == currentuser.id) ? true : false}
>
Change Role
</Button>
)}
</Mutation>
</TableCell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TablePagination
count={totalEntries}
page={currentPage - 1}
rowsPerPage={pageSize(this.props)}
onChangePage={onChangePage(this.props, 1)}
onChangeRowsPerPage={onChangeRowsPerPage(this.props)}
rowsPerPageOptions={this.state.rowOptions}
/>
</TableRow>
</TableFooter>
</Table>)
}}
</Query>
</Paper>
)
}}
Expand Down
15 changes: 15 additions & 0 deletions assets/src/mutations/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import gql from "graphql-tag"

export const UPDATE_USER = gql`
mutation updateUser(
$id: ID!
$admin: Boolean!
) {
updateUser(
id: $id
admin: $admin
) {
id
admin
}
}`
2 changes: 1 addition & 1 deletion lib/orcasite/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Orcasite.Accounts.User do

def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:email, :first_name, :last_name])
|> cast(attrs, [:email, :first_name, :last_name, :admin])
|> validate_required([:email])
|> update_change(:email, &String.downcase/1)
|> validate_format(:email, ~r/^.+@.+$/)
Expand Down
2 changes: 1 addition & 1 deletion lib/orcasite_web/graphql/resolvers/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule OrcasiteWeb.Resolvers.Accounts do

def update_user(%{id: user_id, admin: admin}, %{context: %{current_user: current_user}}) do
with user <- Accounts.get_user!(user_id),
{:ok, user} <- Accounts.update_user(user, %{admin: admin}, current_user) do
{:ok, user} <- Accounts.update_user(user, %{admin: !admin}, current_user) do
{:ok, user}
else
error -> error
Expand Down
2 changes: 1 addition & 1 deletion lib/orcasite_web/graphql/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule OrcasiteWeb.Schema do

@desc "Update user details"
field :update_user, :user do
arg(:id)
arg(:id, :id)
arg(:admin, :boolean)
resolve(&Resolvers.Accounts.update_user/2)
end
Expand Down