2020-09-03 01:49:14 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-02-16 13:35:16 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-09-03 01:49:14 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2020-09-16 05:34:30 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-31 23:11:30 +02:00
|
|
|
import { setDateRange } from 'redux/actions/websites';
|
2020-08-31 12:53:39 +02:00
|
|
|
import Button from './Button';
|
|
|
|
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-09-03 17:42:08 +02:00
|
|
|
import { getDateRange } from '../../lib/date';
|
2021-03-10 10:38:35 +01:00
|
|
|
import useLocale from 'hooks/useLocale';
|
2020-08-31 12:53:39 +02:00
|
|
|
|
2021-02-16 13:35:16 +01:00
|
|
|
function RefreshButton({ websiteId }) {
|
2020-08-31 23:11:30 +02:00
|
|
|
const dispatch = useDispatch();
|
2021-06-30 03:41:34 +02:00
|
|
|
const { locale } = useLocale();
|
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);
|
2020-10-09 00:02:48 +02:00
|
|
|
const completed = useSelector(state => state.queries[`/api/website/${websiteId}/stats`]);
|
2020-08-31 23:11:30 +02:00
|
|
|
|
|
|
|
function handleClick() {
|
|
|
|
if (dateRange) {
|
2020-09-03 01:49:14 +02:00
|
|
|
setLoading(true);
|
2021-03-10 10:38:35 +01:00
|
|
|
dispatch(setDateRange(websiteId, getDateRange(dateRange.value, locale)));
|
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
|
|
|
|
icon={loading ? <Dots /> : <Refresh />}
|
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}
|
|
|
|
/>
|
|
|
|
);
|
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;
|