2022-12-29 00:43:22 +01:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
2023-02-04 17:59:52 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
import { Button, Icon, Tooltip } from '../react-basics';
|
2022-02-23 07:47:59 +01:00
|
|
|
import useStore from 'store/queries';
|
|
|
|
import { setDateRange } from 'store/websites';
|
2020-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2023-01-31 06:44:07 +01:00
|
|
|
import Icons from 'components/icons';
|
2023-02-04 17:59:52 +01:00
|
|
|
import { labels } from 'components/messages';
|
2020-08-31 12:53:39 +02:00
|
|
|
|
2021-02-16 13:35:16 +01:00
|
|
|
function RefreshButton({ websiteId }) {
|
2023-02-04 17:59:52 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2020-09-19 19:35:05 +02:00
|
|
|
const [dateRange] = useDateRange(websiteId);
|
2020-09-03 01:49:14 +02:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-10-04 02:17:53 +02:00
|
|
|
const selector = useCallback(state => state[`/websites/${websiteId}/stats`], [websiteId]);
|
2022-02-23 07:47:59 +01:00
|
|
|
const completed = useStore(selector);
|
2020-08-31 23:11:30 +02:00
|
|
|
|
|
|
|
function handleClick() {
|
2022-03-02 06:13:15 +01:00
|
|
|
if (!loading && dateRange) {
|
2020-09-03 01:49:14 +02:00
|
|
|
setLoading(true);
|
2023-01-31 06:44:07 +01:00
|
|
|
if (/^\d+/.test(dateRange.value)) {
|
2022-03-02 06:13:15 +01:00
|
|
|
setDateRange(websiteId, dateRange.value);
|
|
|
|
} else {
|
|
|
|
setDateRange(websiteId, dateRange);
|
|
|
|
}
|
2020-08-31 23:11:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 01:49:14 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setLoading(false);
|
|
|
|
}, [completed]);
|
|
|
|
|
2020-09-16 05:34:30 +02:00
|
|
|
return (
|
2023-02-04 17:59:52 +01:00
|
|
|
<Tooltip label={formatMessage(labels.refresh)}>
|
|
|
|
<Button onClick={handleClick}>
|
|
|
|
<Icon>
|
|
|
|
<Icons.Refresh />
|
|
|
|
</Icon>
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
2020-09-16 05:34:30 +02:00
|
|
|
);
|
2020-08-31 12:53:39 +02:00
|
|
|
}
|
2021-02-16 13:35:16 +01:00
|
|
|
|
|
|
|
export default RefreshButton;
|