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

56 lines
1.8 KiB
JavaScript
Raw Normal View History

import { Button, Icon, Text, Modal, ModalTrigger, useToast, Icons } from 'react-basics';
2023-01-25 16:42:46 +01:00
import { useIntl } from 'react-intl';
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-01-25 16:42:46 +01:00
import { labels, messages } from 'components/messages';
2023-01-21 02:12:53 +01:00
export default function WebsitesList() {
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 },
);
const { toast, showToast } = useToast();
2023-01-21 02:12:53 +01:00
const { formatMessage } = useIntl();
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 = (
<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}>
{toast}
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-01-21 02:12:53 +01:00
<EmptyPlaceholder message={formatMessage(messages.noWebsites)}>
{addButton}
</EmptyPlaceholder>
)}
</Page>
);
}