2020-09-03 01:49:14 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
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';
|
2020-08-31 12:53:39 +02:00
|
|
|
|
2020-08-31 23:11:30 +02:00
|
|
|
export default function RefreshButton({ websiteId }) {
|
|
|
|
const dispatch = useDispatch();
|
2020-09-01 00:02:32 +02:00
|
|
|
const dateRange = useDateRange(websiteId);
|
2020-09-03 01:49:14 +02:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const completed = useSelector(state => state.queries[`/api/website/${websiteId}/metrics`]);
|
2020-08-31 23:11:30 +02:00
|
|
|
|
|
|
|
function handleClick() {
|
|
|
|
if (dateRange) {
|
2020-09-03 01:49:14 +02:00
|
|
|
setLoading(true);
|
2020-09-03 17:42:08 +02:00
|
|
|
dispatch(setDateRange(websiteId, getDateRange(dateRange.value)));
|
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 />}
|
|
|
|
tooltip={<FormattedMessage id="button.refresh" defaultMessage="Refresh" />}
|
|
|
|
tooltipId="button-refresh"
|
|
|
|
size="small"
|
|
|
|
onClick={handleClick}
|
|
|
|
/>
|
|
|
|
);
|
2020-08-31 12:53:39 +02:00
|
|
|
}
|