110 lines
2.9 KiB
JavaScript
110 lines
2.9 KiB
JavaScript
import { useMutation, useFlash } from '@redwoodjs/web'
|
|
import { Link, routes } from '@redwoodjs/router'
|
|
|
|
const DELETE_USER_MUTATION = gql`
|
|
mutation DeleteUserMutation($id: Int!) {
|
|
deleteUser(id: $id) {
|
|
id
|
|
}
|
|
}
|
|
`
|
|
|
|
const MAX_STRING_LENGTH = 150
|
|
|
|
const truncate = (text) => {
|
|
let output = text
|
|
if (text && text.length > MAX_STRING_LENGTH) {
|
|
output = output.substring(0, MAX_STRING_LENGTH) + '...'
|
|
}
|
|
return output
|
|
}
|
|
|
|
const jsonTruncate = (obj) => {
|
|
return truncate(JSON.stringify(obj, null, 2))
|
|
}
|
|
|
|
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 UsersList = ({ users }) => {
|
|
const { addMessage } = useFlash()
|
|
const [deleteUser] = useMutation(DELETE_USER_MUTATION, {
|
|
onCompleted: () => {
|
|
addMessage('User deleted.', { classes: 'rw-flash-success' })
|
|
},
|
|
})
|
|
|
|
const onDeleteClick = (id) => {
|
|
if (confirm('Are you sure you want to delete user ' + id + '?')) {
|
|
deleteUser({ variables: { id }, refetchQueries: ['USERS'] })
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="rw-segment rw-table-wrapper-responsive">
|
|
<table className="rw-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Id</th>
|
|
<th>Email</th>
|
|
<th>Created at</th>
|
|
<th>Updated at</th>
|
|
<th>Image</th>
|
|
<th>Bio</th>
|
|
<th> </th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{users.map((user) => (
|
|
<tr key={user.id}>
|
|
<td>{truncate(user.id)}</td>
|
|
<td>{truncate(user.email)}</td>
|
|
<td>{timeTag(user.createdAt)}</td>
|
|
<td>{timeTag(user.updatedAt)}</td>
|
|
<td>{truncate(user.image)}</td>
|
|
<td>{truncate(user.bio)}</td>
|
|
<td>
|
|
<nav className="rw-table-actions">
|
|
<Link
|
|
to={routes.user({ id: user.id })}
|
|
title={'Show user ' + user.id + ' detail'}
|
|
className="rw-button rw-button-small"
|
|
>
|
|
Show
|
|
</Link>
|
|
<Link
|
|
to={routes.editUser({ id: user.id })}
|
|
title={'Edit user ' + user.id}
|
|
className="rw-button rw-button-small rw-button-blue"
|
|
>
|
|
Edit
|
|
</Link>
|
|
<a
|
|
href="#"
|
|
title={'Delete user ' + user.id}
|
|
className="rw-button rw-button-small rw-button-red"
|
|
onClick={() => onDeleteClick(user.id)}
|
|
>
|
|
Delete
|
|
</a>
|
|
</nav>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default UsersList
|