2023-03-22 22:05:55 +01:00
|
|
|
import { useToast } from 'react-basics';
|
2022-12-27 01:57:59 +01:00
|
|
|
import Page from 'components/layout/Page';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2023-01-21 02:12:53 +01:00
|
|
|
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
2023-01-24 00:32:35 +01:00
|
|
|
import UsersTable from './UsersTable';
|
|
|
|
import UserAddButton from './UserAddButton';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2023-01-21 02:12:53 +01:00
|
|
|
import useUser from 'hooks/useUser';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
export default function UsersList() {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2023-01-21 02:12:53 +01:00
|
|
|
const { user } = useUser();
|
|
|
|
const { get, useQuery } = useApi();
|
2023-01-25 16:42:46 +01:00
|
|
|
const { data, isLoading, error, refetch } = useQuery(['user'], () => get(`/users`), {
|
2023-01-21 02:12:53 +01:00
|
|
|
enabled: !!user,
|
|
|
|
});
|
2023-01-25 16:42:46 +01:00
|
|
|
const { toast, showToast } = useToast();
|
2023-01-21 02:12:53 +01:00
|
|
|
const hasData = data && data.length !== 0;
|
|
|
|
|
2023-01-25 16:42:46 +01:00
|
|
|
const handleSave = () => refetch();
|
|
|
|
|
|
|
|
const handleDelete = () =>
|
2023-01-28 06:53:13 +01:00
|
|
|
showToast({ message: formatMessage(messages.userDeleted), variant: 'success' });
|
2023-01-21 02:12:53 +01:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
return (
|
2023-01-21 02:12:53 +01:00
|
|
|
<Page loading={isLoading} error={error}>
|
2023-01-25 16:42:46 +01:00
|
|
|
{toast}
|
|
|
|
<PageHeader title={formatMessage(labels.users)}>
|
|
|
|
<UserAddButton onSave={handleSave} />
|
|
|
|
</PageHeader>
|
|
|
|
{hasData && <UsersTable data={data} onDelete={handleDelete} />}
|
2023-01-21 02:12:53 +01:00
|
|
|
{!hasData && (
|
2023-01-25 16:42:46 +01:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noUsers)}>
|
|
|
|
<UserAddButton onSave={handleSave} />
|
|
|
|
</EmptyPlaceholder>
|
2023-01-21 02:12:53 +01:00
|
|
|
)}
|
2022-12-27 01:57:59 +01:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|