umami/components/pages/settings/websites/WebsitesList.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-06-13 20:59:12 +02:00
import { Button, Icon, Text, Modal, ModalTrigger, useToasts, Icons } from 'react-basics';
2023-01-21 02:12:53 +01:00
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
2023-01-10 08:59:26 +01:00
import WebsiteAddForm from 'components/pages/settings/websites/WebsiteAddForm';
import WebsitesTable from 'components/pages/settings/websites/WebsitesTable';
2023-01-21 02:12:53 +01:00
import useApi from 'hooks/useApi';
import useUser from 'hooks/useUser';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-07-07 08:02:16 +02:00
import { ROLES } from 'lib/constants';
2023-01-21 02:12:53 +01:00
2023-04-21 17:00:42 +02:00
export function WebsitesList() {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
2023-01-10 08:59:26 +01:00
const { user } = useUser();
2023-01-21 02:12:53 +01:00
const { get, useQuery } = useApi();
2023-01-11 23:47:38 +01:00
const { data, isLoading, error, refetch } = useQuery(
['websites', user?.id],
() => get(`/users/${user?.id}/websites`),
{ enabled: !!user },
);
2023-06-13 20:59:12 +02:00
const { showToast } = useToasts();
2023-01-21 02:12:53 +01:00
const hasData = data && data.length !== 0;
2023-01-10 08:59:26 +01:00
const handleSave = async () => {
await refetch();
2023-01-21 02:12:53 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
2023-01-21 02:12:53 +01:00
const addButton = (
2023-07-07 08:02:16 +02:00
<>
{user.role !== ROLES.viewOnly && (
<ModalTrigger>
<Button variant="primary">
<Icon>
<Icons.Plus />
</Icon>
<Text>{formatMessage(labels.addWebsite)}</Text>
</Button>
<Modal title={formatMessage(labels.addWebsite)}>
{close => <WebsiteAddForm onSave={handleSave} onClose={close} />}
</Modal>
</ModalTrigger>
)}
</>
2023-01-21 02:12:53 +01:00
);
return (
<Page loading={isLoading} error={error}>
2023-01-25 16:42:46 +01:00
<PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>
2023-01-21 02:12:53 +01:00
{hasData && <WebsitesTable data={data} />}
{!hasData && (
2023-04-10 05:22:28 +02:00
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
2023-01-21 02:12:53 +01:00
{addButton}
</EmptyPlaceholder>
)}
</Page>
);
}
2023-04-21 17:00:42 +02:00
export default WebsitesList;