You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monitor-doc/resources/js/Pages/User/Index.jsx

166 lines
7.0 KiB
React

2 years ago
import React, { useState, useEffect } from 'react'
import { Head } from '@inertiajs/react'
import { router } from '@inertiajs/react'
2 years ago
import { usePrevious } from 'react-use'
import { toast } from 'react-toastify'
import { useModalState } from '@/Hooks'
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'
import Pagination from '@/Components/Pagination'
import ModalConfirm from '@/Components/ModalConfirm'
import UserFormModal from './UserFormModal'
import { hasPermission } from '@/utils'
2 years ago
export default function Users(props) {
const { data: users, links } = props.users
const [search, setSearch] = useState('')
const preValue = usePrevious(search)
const [user, setUser] = useState(null)
const formModal = useModalState(false)
const toggle = (user = null) => {
setUser(user)
formModal.toggle()
2 years ago
}
const confirmModal = useModalState(false)
const handleDelete = (user) => {
confirmModal.setData(user)
confirmModal.toggle()
2 years ago
}
const onDelete = () => {
const user = confirmModal.data
if(user != null) {
router.delete(route('users.destroy', user), {
onSuccess: () => toast.success('The Data has been deleted'),
})
}
2 years ago
}
const canCreate = hasPermission('create-user', props.auth.user)
const canUpdate = hasPermission('update-user', props.auth.user)
const canDelete = hasPermission('delete-user', props.auth.user)
2 years ago
useEffect(() => {
if (preValue) {
router.get(
2 years ago
route(route().current()),
{ q: search },
{
replace: true,
preserveState: true,
}
)
}
}, [search])
return (
<AuthenticatedLayout
auth={props.auth}
errors={props.errors}
flash={props.flash}
notify={props.notify}
2 years ago
>
<Head title="Users" />
<div className="flex flex-col w-full sm:px-6 lg:px-8 space-y-2">
<div className="card bg-base-100 w-full">
<div className="card-body">
<div className="flex w-full mb-4 justify-between">
{canCreate && (
<div
className="btn btn-neutral"
onClick={() => toggle()}
>
Tambah
</div>
)}
<div className="form-control">
<input
type="text"
className="input input-bordered"
value={search}
onChange={(e) =>
setSearch(e.target.value)
}
placeholder="Search"
/>
</div>
</div>
<div className="overflow-x-auto">
<table className="table w-full table-zebra">
<thead>
<tr>
<th>Id</th>
<th>Nama</th>
<th>Email</th>
<th>Role</th>
2 years ago
<th>Group</th>
<th>Region</th>
<th></th>
</tr>
</thead>
<tbody>
{users?.map((user) => (
<tr key={user.id}>
<th>{user.id}</th>
<td>{user.name}</td>
<td>{user.email}</td>
<td>{user?.role?.name}</td>
<td>{user?.region?.group?.name}</td>
<td>{user?.region?.name}</td>
<td className="text-right">
{canUpdate && (
2 years ago
<div
className="btn btn-primary mx-1"
2 years ago
onClick={() =>
toggle(user)
2 years ago
}
>
Edit
2 years ago
</div>
)}
{+user.is_admin === 0 && (
<>
{canDelete && (
<>
{props.auth.user.id !==
user.id && (
<div
className="btn btn-secondary mx-1"
onClick={() =>
handleDelete(user)
}
>
Delete
</div>
)}
</>
)}
</>
)}
</td>
</tr>
))}
</tbody>
</table>
2 years ago
</div>
<Pagination links={links} />
2 years ago
</div>
</div>
</div>
2 years ago
<UserFormModal
isOpen={formModal.isOpen}
toggle={toggle}
user={user}
/>
<ModalConfirm
isOpen={confirmModal.isOpen}
toggle={confirmModal.toggle}
onConfirm={onDelete}
/>
</AuthenticatedLayout>
)
}