2022-03-04 04:45:49 +01:00
|
|
|
import { useState } from 'react';
|
2023-01-28 06:53:13 +01:00
|
|
|
import { Button, Icon, Icons, Text, Flexbox } from 'react-basics';
|
|
|
|
import Link from 'next/link';
|
2022-03-04 04:45:49 +01:00
|
|
|
import Page from 'components/layout/Page';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2023-01-10 08:59:26 +01:00
|
|
|
import WebsiteChartList from 'components/pages/websites/WebsiteChartList';
|
2023-01-19 00:05:39 +01:00
|
|
|
import DashboardSettingsButton from 'components/pages/dashboard/DashboardSettingsButton';
|
2023-01-11 23:47:38 +01:00
|
|
|
import DashboardEdit from 'components/pages/dashboard/DashboardEdit';
|
2023-01-28 06:53:13 +01:00
|
|
|
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2022-08-04 12:56:30 +02:00
|
|
|
import useDashboard from 'store/dashboard';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2022-03-04 04:45:49 +01:00
|
|
|
|
2022-10-26 00:48:49 +02:00
|
|
|
export default function Dashboard({ userId }) {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2022-08-04 12:56:30 +02:00
|
|
|
const dashboard = useDashboard();
|
|
|
|
const { showCharts, limit, editing } = dashboard;
|
2022-03-04 04:45:49 +01:00
|
|
|
const [max, setMax] = useState(limit);
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2023-01-28 06:53:13 +01:00
|
|
|
const { data, isLoading, error } = useQuery(['websites'], () => get('/websites', { userId }));
|
|
|
|
const hasData = data && data.length !== 0;
|
2022-03-04 04:45:49 +01:00
|
|
|
|
|
|
|
function handleMore() {
|
|
|
|
setMax(max + limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-28 06:53:13 +01:00
|
|
|
<Page loading={isLoading} error={error}>
|
|
|
|
<PageHeader title={formatMessage(labels.dashboard)}>
|
|
|
|
{!editing && hasData && <DashboardSettingsButton />}
|
2022-03-04 04:45:49 +01:00
|
|
|
</PageHeader>
|
2023-01-28 06:53:13 +01:00
|
|
|
{!hasData && (
|
2023-04-10 01:06:59 +02:00
|
|
|
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
|
2023-01-28 06:53:13 +01:00
|
|
|
<Link href="/settings/websites">
|
|
|
|
<Button>
|
|
|
|
<Icon>
|
|
|
|
<Icons.ArrowRight />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(messages.goToSettings)}</Text>
|
|
|
|
</Button>
|
|
|
|
</Link>
|
|
|
|
</EmptyPlaceholder>
|
|
|
|
)}
|
|
|
|
{hasData && (
|
|
|
|
<>
|
|
|
|
{editing && <DashboardEdit websites={data} />}
|
|
|
|
{!editing && <WebsiteChartList websites={data} showCharts={showCharts} limit={max} />}
|
|
|
|
{max < data.length && (
|
|
|
|
<Flexbox justifyContent="center">
|
|
|
|
<Button onClick={handleMore}>
|
|
|
|
<Icon>
|
|
|
|
<Icons.More />
|
|
|
|
</Icon>
|
|
|
|
<Text>{formatMessage(labels.more)}</Text>
|
|
|
|
</Button>
|
|
|
|
</Flexbox>
|
|
|
|
)}
|
|
|
|
</>
|
2022-03-04 04:45:49 +01:00
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|