2022-02-23 07:47:59 +01:00
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
2021-02-16 13:35:16 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-09-16 05:34:30 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-02-23 07:47:59 +01:00
|
|
|
import useStore from 'store/queries';
|
|
|
|
import { setDateRange } from 'store/websites';
|
2022-12-27 01:57:59 +01:00
|
|
|
import { Button, Icon } from 'react-basics';
|
2020-08-31 12:53:39 +02:00
|
|
|
import Refresh from 'assets/redo.svg';
|
2020-09-03 01:49:14 +02:00
|
|
|
import Dots from 'assets/ellipsis-h.svg';
|
2020-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-08-31 12:53:39 +02:00
|
|
|
|
2021-02-16 13:35:16 +01:00
|
|
|
function RefreshButton({ websiteId }) {
|
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);
|
2022-03-02 06:13:15 +01:00
|
|
|
if (/^[\d]+/.test(dateRange.value)) {
|
|
|
|
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 (
|
|
|
|
<Button
|
2020-10-13 07:53:59 +02:00
|
|
|
tooltip={<FormattedMessage id="label.refresh" defaultMessage="Refresh" />}
|
2020-09-16 05:34:30 +02:00
|
|
|
tooltipId="button-refresh"
|
|
|
|
size="small"
|
|
|
|
onClick={handleClick}
|
2022-12-27 01:57:59 +01:00
|
|
|
>
|
|
|
|
<Icon>{loading ? <Dots /> : <Refresh />}</Icon>
|
|
|
|
</Button>
|
2020-09-16 05:34:30 +02:00
|
|
|
);
|
2020-08-31 12:53:39 +02:00
|
|
|
}
|
2021-02-16 13:35:16 +01:00
|
|
|
|
|
|
|
RefreshButton.propTypes = {
|
2021-02-16 13:51:14 +01:00
|
|
|
websiteId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2021-02-16 13:35:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default RefreshButton;
|