Moved filter params into a hook. Close #2691.

This commit is contained in:
Mike Cao 2024-05-02 19:44:26 -07:00
parent 2ba87cd483
commit ab94f5d1cd
6 changed files with 63 additions and 101 deletions

View file

@ -1,30 +1,14 @@
import { useApi, useDateRange, useNavigation } from 'components/hooks';
import { useApi } from './useApi';
import { useFilterParams } from '../useFilterParams';
export function useWebsiteStats(websiteId: string, options?: { [key: string]: string }) {
const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange;
const {
query: { url, referrer, title, os, browser, device, country, region, city },
} = useNavigation();
const params = {
startAt: +startDate,
endAt: +endDate,
url,
referrer,
title,
os,
browser,
device,
country,
region,
city,
};
const params = useFilterParams(websiteId);
return useQuery({
queryKey: ['websites:stats', { websiteId, ...params }],
queryFn: () => get(`/websites/${websiteId}/stats`, params),
enabled: !!websiteId,
...options,
});
}