Allow filter search for country and region.

This commit is contained in:
Mike Cao 2024-11-27 22:51:50 -08:00
parent c6fd20974d
commit 2ed7628997

View File

@ -1,4 +1,6 @@
import { useApi } from '../useApi'; import { useApi } from '../useApi';
import { useCountryNames, useRegionNames } from 'components/hooks';
import useLocale from '../useLocale';
export function useWebsiteValues({ export function useWebsiteValues({
websiteId, websiteId,
@ -14,6 +16,26 @@ export function useWebsiteValues({
search?: string; search?: string;
}) { }) {
const { get, useQuery } = useApi(); const { get, useQuery } = useApi();
const { locale } = useLocale();
const { countryNames } = useCountryNames(locale);
const { regionNames } = useRegionNames(locale);
const names = {
country: countryNames,
region: regionNames,
};
const getSearch = (type: string, value: string) => {
if (value) {
const values = names[type];
return Object.keys(values).reduce((code: string, key: string) => {
if (!code && values[key].toLowerCase().includes(value.toLowerCase())) {
code = key;
}
return code;
}, '');
}
};
return useQuery({ return useQuery({
queryKey: ['websites:values', { websiteId, type, startDate, endDate, search }], queryKey: ['websites:values', { websiteId, type, startDate, endDate, search }],
@ -22,7 +44,7 @@ export function useWebsiteValues({
type, type,
startAt: +startDate, startAt: +startDate,
endAt: +endDate, endAt: +endDate,
search, search: getSearch(type, search),
}), }),
enabled: !!(websiteId && type && startDate && endDate), enabled: !!(websiteId && type && startDate && endDate),
}); });