2022-03-04 04:45:49 +01:00
|
|
|
import { useState } from 'react';
|
2022-12-29 00:49:28 +01:00
|
|
|
import { Button, Loading } from 'react-basics';
|
2022-08-04 12:56:30 +02:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2022-03-04 04:45:49 +01:00
|
|
|
import Page from 'components/layout/Page';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2022-12-28 21:37:09 +01:00
|
|
|
import WebsiteChartList from 'components/pages/WebsiteChartList';
|
2022-03-04 04:45:49 +01:00
|
|
|
import DashboardSettingsButton from 'components/settings/DashboardSettingsButton';
|
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';
|
|
|
|
import DashboardEdit from './DashboardEdit';
|
2022-03-04 04:45:49 +01:00
|
|
|
import styles from './WebsiteList.module.css';
|
|
|
|
|
2022-08-04 12:56:30 +02:00
|
|
|
const messages = defineMessages({
|
|
|
|
dashboard: { id: 'label.dashboard', defaultMessage: 'Dashboard' },
|
|
|
|
more: { id: 'label.more', defaultMessage: 'More' },
|
|
|
|
});
|
2022-03-04 04:45:49 +01:00
|
|
|
|
2022-10-26 00:48:49 +02:00
|
|
|
export default function Dashboard({ userId }) {
|
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();
|
|
|
|
const { data, isLoading } = useQuery(['websites'], () => get('/websites', { userId }));
|
2022-08-04 12:56:30 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2022-03-04 04:45:49 +01:00
|
|
|
|
|
|
|
function handleMore() {
|
|
|
|
setMax(max + limit);
|
|
|
|
}
|
|
|
|
|
2022-12-29 00:43:22 +01:00
|
|
|
if (isLoading) {
|
|
|
|
return <Loading />;
|
|
|
|
}
|
|
|
|
|
2022-03-04 04:45:49 +01:00
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<PageHeader>
|
2022-08-04 12:56:30 +02:00
|
|
|
<div>{formatMessage(messages.dashboard)}</div>
|
|
|
|
{!editing && <DashboardSettingsButton />}
|
2022-03-04 04:45:49 +01:00
|
|
|
</PageHeader>
|
2022-08-05 06:37:18 +02:00
|
|
|
{editing && <DashboardEdit websites={data} />}
|
2022-12-28 21:37:09 +01:00
|
|
|
{!editing && <WebsiteChartList websites={data} showCharts={showCharts} limit={max} />}
|
2022-03-04 04:45:49 +01:00
|
|
|
{max < data.length && (
|
|
|
|
<Button className={styles.button} onClick={handleMore}>
|
2022-08-04 12:56:30 +02:00
|
|
|
{formatMessage(messages.more)}
|
2022-03-04 04:45:49 +01:00
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|