2023-05-18 08:20:06 +02:00
|
|
|
import useApi from 'hooks/useApi';
|
|
|
|
import useDateRange from 'hooks/useDateRange';
|
|
|
|
import DateFilter from './DateFilter';
|
2023-07-10 13:35:19 +02:00
|
|
|
import styles from './WebsiteDateFilter.module.css';
|
2023-05-18 08:20:06 +02:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
export default function WebsiteDateFilter({ websiteId }) {
|
2023-05-18 08:20:06 +02:00
|
|
|
const { get } = useApi();
|
|
|
|
const [dateRange, setDateRange] = useDateRange(websiteId);
|
2023-07-10 13:35:19 +02:00
|
|
|
const { value, startDate, endDate } = dateRange;
|
2023-05-18 08:20:06 +02:00
|
|
|
|
|
|
|
const handleChange = async value => {
|
|
|
|
if (value === 'all' && websiteId) {
|
|
|
|
const data = await get(`/websites/${websiteId}`);
|
|
|
|
|
|
|
|
if (data) {
|
2023-05-29 07:28:11 +02:00
|
|
|
setDateRange(`range:${new Date(data.createdAt)}:${Date.now()}`);
|
2023-05-18 08:20:06 +02:00
|
|
|
}
|
|
|
|
} else if (value !== 'all') {
|
|
|
|
setDateRange(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-07-10 13:35:19 +02:00
|
|
|
<DateFilter
|
|
|
|
className={styles.dropdown}
|
|
|
|
value={value}
|
|
|
|
startDate={startDate}
|
|
|
|
endDate={endDate}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
2023-05-18 08:20:06 +02:00
|
|
|
);
|
|
|
|
}
|