2022-12-16 02:06:14 +00:00
|
|
|
import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
|
|
|
|
import { Fragment, useEffect, useState } from 'react';
|
2023-01-25 18:14:39 +00:00
|
|
|
import { NavLink } from 'react-router-dom';
|
2022-12-16 02:06:14 +00:00
|
|
|
|
|
|
|
import { useGetUsers } from '@/api/admin/users';
|
|
|
|
import type { UUID } from '@/api/definitions';
|
|
|
|
import { Transition } from '@/components/elements/transitions';
|
|
|
|
import { Button } from '@/components/elements/button/index';
|
|
|
|
import Checkbox from '@/components/elements/inputs/Checkbox';
|
|
|
|
import InputField from '@/components/elements/inputs/InputField';
|
|
|
|
import UserTableRow from '@/components/admin/users/UserTableRow';
|
|
|
|
import TFootPaginated from '@/components/elements/table/TFootPaginated';
|
|
|
|
import type { User } from '@definitions/admin';
|
|
|
|
import extractSearchFilters from '@/helpers/extractSearchFilters';
|
|
|
|
import useDebouncedState from '@/plugins/useDebouncedState';
|
2022-12-21 21:27:50 +00:00
|
|
|
import { Shape } from '@/components/elements/button/types';
|
2022-12-16 02:06:14 +00:00
|
|
|
|
|
|
|
const filters = ['id', 'uuid', 'external_id', 'username', 'email'] as const;
|
|
|
|
|
2023-01-25 18:14:39 +00:00
|
|
|
function UsersContainer() {
|
2022-12-16 02:06:14 +00:00
|
|
|
const [search, setSearch] = useDebouncedState('', 500);
|
|
|
|
const [selected, setSelected] = useState<UUID[]>([]);
|
|
|
|
const { data: users } = useGetUsers(
|
|
|
|
extractSearchFilters(search, filters, {
|
|
|
|
splitUnmatched: true,
|
|
|
|
returnUnmatched: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.title = 'Admin | Users';
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onRowChange = (user: User, checked: boolean) => {
|
|
|
|
setSelected(state => {
|
|
|
|
return checked ? [...state, user.uuid] : selected.filter(uuid => uuid !== user.uuid);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectAllChecked = users && users.items.length > 0 && selected.length > 0;
|
|
|
|
const onSelectAll = () =>
|
|
|
|
setSelected(state => (state.length > 0 ? [] : users?.items.map(({ uuid }) => uuid) || []));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2023-01-25 18:14:39 +00:00
|
|
|
<div className="mb-4 flex justify-end">
|
|
|
|
<NavLink to="/admin/users/new">
|
|
|
|
<Button className="shadow focus:ring-offset-2 focus:ring-offset-neutral-800">
|
|
|
|
Add User <PlusIcon className="ml-2 h-5 w-5" />
|
|
|
|
</Button>
|
|
|
|
</NavLink>
|
2022-12-16 02:06:14 +00:00
|
|
|
</div>
|
2023-01-25 18:14:39 +00:00
|
|
|
|
|
|
|
<div className="relative flex items-center rounded-t bg-neutral-700 px-4 py-2">
|
|
|
|
<div className="mr-6">
|
2022-12-16 02:06:14 +00:00
|
|
|
<Checkbox
|
|
|
|
checked={selectAllChecked}
|
|
|
|
disabled={!users?.items.length}
|
|
|
|
indeterminate={selected.length !== users?.items.length}
|
|
|
|
onChange={onSelectAll}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-01-25 18:14:39 +00:00
|
|
|
<div className="flex-1">
|
2022-12-16 02:06:14 +00:00
|
|
|
<InputField
|
2023-01-25 18:14:39 +00:00
|
|
|
type="text"
|
|
|
|
name="filter"
|
|
|
|
placeholder="Begin typing to filter..."
|
|
|
|
className="w-56 focus:w-96"
|
2022-12-16 02:06:14 +00:00
|
|
|
onChange={e => setSearch(e.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-01-25 18:14:39 +00:00
|
|
|
<Transition.Fade as={Fragment} show={selected.length > 0} duration="duration-75">
|
|
|
|
<div className="absolute top-0 left-0 flex h-full w-full items-center justify-end space-x-4 rounded-t bg-neutral-700 px-4">
|
|
|
|
<div className="flex-1">
|
2022-12-16 02:06:14 +00:00
|
|
|
<Checkbox
|
|
|
|
checked={selectAllChecked}
|
|
|
|
indeterminate={selected.length !== users?.items.length}
|
|
|
|
onChange={onSelectAll}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-21 21:27:50 +00:00
|
|
|
<Button.Text shape={Shape.IconSquare}>
|
2023-01-25 18:14:39 +00:00
|
|
|
<SupportIcon className="h-4 w-4" />
|
2022-12-16 02:06:14 +00:00
|
|
|
</Button.Text>
|
2022-12-21 21:27:50 +00:00
|
|
|
<Button.Text shape={Shape.IconSquare}>
|
2023-01-25 18:14:39 +00:00
|
|
|
<LockOpenIcon className="h-4 w-4" />
|
2022-12-16 02:06:14 +00:00
|
|
|
</Button.Text>
|
2022-12-21 21:27:50 +00:00
|
|
|
<Button.Text shape={Shape.IconSquare}>
|
2023-01-25 18:14:39 +00:00
|
|
|
<TrashIcon className="h-4 w-4" />
|
2022-12-16 02:06:14 +00:00
|
|
|
</Button.Text>
|
|
|
|
</div>
|
|
|
|
</Transition.Fade>
|
|
|
|
</div>
|
2023-01-25 18:14:39 +00:00
|
|
|
<table className="min-w-full rounded bg-neutral-700">
|
|
|
|
<thead className="bg-neutral-900">
|
2022-12-16 02:06:14 +00:00
|
|
|
<tr>
|
2023-01-25 18:14:39 +00:00
|
|
|
<th scope="col" className="w-8" />
|
|
|
|
<th scope="col" className="w-full px-6 py-2 text-left">
|
2022-12-16 02:06:14 +00:00
|
|
|
Email
|
|
|
|
</th>
|
2023-01-25 18:14:39 +00:00
|
|
|
<th scope="col" />
|
|
|
|
<th scope="col" />
|
2022-12-16 02:06:14 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{users?.items.map(user => (
|
|
|
|
<UserTableRow
|
|
|
|
key={user.uuid}
|
|
|
|
user={user}
|
|
|
|
selected={selected.includes(user.uuid)}
|
|
|
|
onRowChange={onRowChange}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</tbody>
|
2023-01-25 18:14:39 +00:00
|
|
|
{users ? <TFootPaginated span={4} pagination={users.pagination} /> : null}
|
2022-12-16 02:06:14 +00:00
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
);
|
2023-01-25 18:14:39 +00:00
|
|
|
}
|
2022-12-16 02:06:14 +00:00
|
|
|
|
|
|
|
export default UsersContainer;
|