2023-03-09 01:37:43 +01:00
|
|
|
import { parseDateRange } from 'lib/date';
|
|
|
|
import { setItem } from 'next-basics';
|
2020-09-19 19:35:05 +02:00
|
|
|
import { DATE_RANGE_CONFIG, DEFAULT_DATE_RANGE } from 'lib/constants';
|
2021-03-10 10:39:31 +01:00
|
|
|
import useLocale from './useLocale';
|
2023-03-09 05:23:32 +01:00
|
|
|
import websiteStore, { setWebsiteDateRange } from 'store/websites';
|
|
|
|
import appStore, { setDateRange } from 'store/app';
|
2020-09-01 00:02:32 +02:00
|
|
|
|
2022-03-02 07:02:31 +01:00
|
|
|
export default function useDateRange(websiteId) {
|
2021-06-30 03:41:34 +02:00
|
|
|
const { locale } = useLocale();
|
2023-03-09 05:23:32 +01:00
|
|
|
const websiteConfig = websiteStore(state => state[websiteId]?.dateRange);
|
2023-03-09 01:37:43 +01:00
|
|
|
const defaultConfig = DEFAULT_DATE_RANGE;
|
2023-03-09 05:23:32 +01:00
|
|
|
const globalConfig = appStore(state => state.dateRange);
|
|
|
|
const dateRange = parseDateRange(websiteConfig || globalConfig || defaultConfig, locale);
|
2020-09-19 19:35:05 +02:00
|
|
|
|
2023-03-09 01:37:43 +01:00
|
|
|
function saveDateRange(value) {
|
2020-09-19 19:35:05 +02:00
|
|
|
if (websiteId) {
|
2023-03-09 01:37:43 +01:00
|
|
|
setWebsiteDateRange(websiteId, value);
|
2020-09-19 19:35:05 +02:00
|
|
|
} else {
|
2023-03-09 01:37:43 +01:00
|
|
|
setItem(DATE_RANGE_CONFIG, value);
|
|
|
|
setDateRange(value);
|
2020-09-19 19:35:05 +02:00
|
|
|
}
|
2020-09-17 09:17:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 01:37:43 +01:00
|
|
|
return [dateRange, saveDateRange];
|
2020-09-01 00:02:32 +02:00
|
|
|
}
|