umami/components/pages/settings/users/UsersList.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-03-22 22:05:55 +01:00
import { useToast } from 'react-basics';
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';
2023-04-21 17:00:42 +02:00
export 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;
const handleSave = () => {
refetch().then(() => showToast({ message: formatMessage(messages.saved), variant: 'success' }));
};
2023-01-25 16:42:46 +01:00
const handleDelete = () => {
refetch().then(() =>
showToast({ message: formatMessage(messages.userDeleted), variant: 'success' }),
);
};
2023-01-21 02:12:53 +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
)}
</Page>
);
}
2023-04-21 17:00:42 +02:00
export default UsersList;