108 lines
2.4 KiB
JavaScript
108 lines
2.4 KiB
JavaScript
import { useMutation, useFlash } from '@redwoodjs/web'
|
|
import { Link, routes, navigate } from '@redwoodjs/router'
|
|
|
|
const DELETE_USER_MUTATION = gql`
|
|
mutation DeleteUserMutation($id: String!) {
|
|
deleteUser(id: $id) {
|
|
id
|
|
}
|
|
}
|
|
`
|
|
|
|
const jsonDisplay = (obj) => {
|
|
return (
|
|
<pre>
|
|
<code>{JSON.stringify(obj, null, 2)}</code>
|
|
</pre>
|
|
)
|
|
}
|
|
|
|
const timeTag = (datetime) => {
|
|
return (
|
|
<time dateTime={datetime} title={datetime}>
|
|
{new Date(datetime).toUTCString()}
|
|
</time>
|
|
)
|
|
}
|
|
|
|
const checkboxInputTag = (checked) => {
|
|
return <input type="checkbox" checked={checked} disabled />
|
|
}
|
|
|
|
const User = ({ user }) => {
|
|
const { addMessage } = useFlash()
|
|
const [deleteUser] = useMutation(DELETE_USER_MUTATION, {
|
|
onCompleted: () => {
|
|
navigate(routes.users())
|
|
addMessage('User deleted.', { classes: 'rw-flash-success' })
|
|
},
|
|
})
|
|
|
|
const onDeleteClick = (id) => {
|
|
if (confirm('Are you sure you want to delete user ' + id + '?')) {
|
|
deleteUser({ variables: { id } })
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="rw-segment">
|
|
<header className="rw-segment-header">
|
|
<h2 className="rw-heading rw-heading-secondary">
|
|
User {user.id} Detail
|
|
</h2>
|
|
</header>
|
|
<table className="rw-table">
|
|
<tbody>
|
|
<tr>
|
|
<th>Id</th>
|
|
<td>{user.id}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>User name</th>
|
|
<td>{user.userName}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>Email</th>
|
|
<td>{user.email}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>Created at</th>
|
|
<td>{timeTag(user.createdAt)}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>Updated at</th>
|
|
<td>{timeTag(user.updatedAt)}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>Image</th>
|
|
<td>{user.image}</td>
|
|
</tr>
|
|
<tr>
|
|
<th>Bio</th>
|
|
<td>{user.bio}</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<nav className="rw-button-group">
|
|
<Link
|
|
to={routes.editUser({ id: user.id })}
|
|
className="rw-button rw-button-blue"
|
|
>
|
|
Edit
|
|
</Link>
|
|
<a
|
|
href="#"
|
|
className="rw-button rw-button-red"
|
|
onClick={() => onDeleteClick(user.id)}
|
|
>
|
|
Delete
|
|
</a>
|
|
</nav>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default User
|