mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-28 08:47:50 +02:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import { useState } from 'react';
|
|
import FieldSelectForm from './FieldSelectForm';
|
|
import FieldFilterForm from './FieldFilterForm';
|
|
import { useApi } from 'components/hooks';
|
|
import { Loading } from 'react-basics';
|
|
|
|
function useValues(websiteId, type) {
|
|
const { get, useQuery } = useApi();
|
|
const { data, error, isLoading } = useQuery(
|
|
['websites:values', websiteId, type],
|
|
() =>
|
|
get(`/websites/${websiteId}/values`, {
|
|
type,
|
|
}),
|
|
{ enabled: !!(websiteId && type) },
|
|
);
|
|
|
|
return { data, error, isLoading };
|
|
}
|
|
|
|
export default function FilterSelectForm({ websiteId, items, onSelect, allowFilterSelect }) {
|
|
const [field, setField] = useState();
|
|
const { data, isLoading } = useValues(websiteId, field?.name);
|
|
|
|
if (!field) {
|
|
return <FieldSelectForm items={items} onSelect={setField} showType={false} />;
|
|
}
|
|
|
|
if (isLoading) {
|
|
return <Loading position="center" icon="dots" />;
|
|
}
|
|
|
|
return (
|
|
<FieldFilterForm
|
|
name={field?.name}
|
|
label={field?.label}
|
|
type={field?.type}
|
|
values={data}
|
|
onSelect={onSelect}
|
|
allowFilterSelect={allowFilterSelect}
|
|
/>
|
|
);
|
|
}
|