2022-12-27 01:57:59 +01:00
|
|
|
import { useState } from 'react';
|
2023-01-19 00:05:39 +01:00
|
|
|
import { Button, Icon, Text, Modal, useToast, Icons } from 'react-basics';
|
2023-01-21 02:12:53 +01:00
|
|
|
import { useIntl, defineMessages } from 'react-intl';
|
|
|
|
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-01-21 02:12:53 +01:00
|
|
|
const messages = defineMessages({
|
|
|
|
saved: { id: 'messages.website-saved', defaultMessage: 'Website saved.' },
|
|
|
|
noWebsites: {
|
|
|
|
id: 'messages.no-websites',
|
|
|
|
defaultMessage: "You don't have any websites configured.",
|
|
|
|
},
|
|
|
|
websites: { id: 'label.websites', defaultMessage: 'Websites' },
|
|
|
|
addWebsite: { id: 'label.add-website', defaultMessage: 'Add website' },
|
|
|
|
});
|
|
|
|
|
2023-01-19 00:05:39 +01:00
|
|
|
const { Plus } = Icons;
|
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
export default function WebsitesList() {
|
|
|
|
const [edit, setEdit] = useState(false);
|
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 { formatMessage } = useIntl();
|
|
|
|
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();
|
2022-12-27 01:57:59 +01:00
|
|
|
setEdit(false);
|
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 handleAdd = () => setEdit(true);
|
|
|
|
|
|
|
|
const handleClose = () => setEdit(false);
|
|
|
|
|
|
|
|
const addButton = (
|
|
|
|
<Button variant="primary" onClick={handleAdd}>
|
|
|
|
<Icon>
|
|
|
|
<Plus />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(messages.addWebsite)}</Text>
|
|
|
|
</Button>
|
|
|
|
);
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Page loading={isLoading} error={error}>
|
|
|
|
{toast}
|
2023-01-21 02:12:53 +01:00
|
|
|
<PageHeader title={formatMessage(messages.websites)}>{addButton}</PageHeader>
|
|
|
|
{hasData && <WebsitesTable data={data} />}
|
2022-12-27 01:57:59 +01:00
|
|
|
{!hasData && (
|
2023-01-21 02:12:53 +01:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noWebsites)}>
|
|
|
|
{addButton}
|
2022-12-27 01:57:59 +01:00
|
|
|
</EmptyPlaceholder>
|
|
|
|
)}
|
|
|
|
{edit && (
|
2023-01-21 02:12:53 +01:00
|
|
|
<Modal title={formatMessage(messages.addWebsite)} onClose={handleClose}>
|
2022-12-27 01:57:59 +01:00
|
|
|
{close => <WebsiteAddForm onSave={handleSave} onClose={close} />}
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|