2023-01-28 06:53:13 +01:00
|
|
|
import { Button, Icon, Text, Modal, ModalTrigger, useToast, Icons } from 'react-basics';
|
2023-01-21 02:12:53 +01:00
|
|
|
import Page from 'components/layout/Page';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2022-12-27 01:57:59 +01:00
|
|
|
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';
|
2022-12-27 01:57:59 +01:00
|
|
|
import useUser from 'hooks/useUser';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2023-01-21 02:12:53 +01:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
export default 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 },
|
2022-12-27 01:57:59 +01:00
|
|
|
);
|
|
|
|
const { toast, showToast } = useToast();
|
2023-01-21 02:12:53 +01:00
|
|
|
const hasData = data && data.length !== 0;
|
2022-12-27 01:57:59 +01:00
|
|
|
|
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' });
|
2022-12-27 01:57:59 +01:00
|
|
|
};
|
|
|
|
|
2023-01-21 02:12:53 +01:00
|
|
|
const addButton = (
|
2023-01-28 06:53:13 +01:00
|
|
|
<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
|
|
|
);
|
2022-12-27 01:57:59 +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} />}
|
2022-12-27 01:57:59 +01:00
|
|
|
{!hasData && (
|
2023-04-10 05:22:28 +02:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
|
2023-01-21 02:12:53 +01:00
|
|
|
{addButton}
|
2022-12-27 01:57:59 +01:00
|
|
|
</EmptyPlaceholder>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|