How to access API metadata (e.g. category counts that come from elasticsearch) in react-admin ListContext

38 Views Asked by At

I have a graphql API that uses elasticsearch to retrieve list results. The API responses has a 'meta' key that contains the aggregations for the facets present in the search results.

I want to provide a custom filter for users in the react-admin list view that only shows options for each facet's filter that are actually present in the response (e.g. if the list has items with colours red and green, only show red and green as options for the Colours filter.

I can't figure out how to make this work. So far I have this code:

const FilterForm = () => {
    const { displayedFilters, filterValues, setFilters, hideFilter, data, isLoading } =
        useListContext();

    const filters = ????

    const form = useForm({
        defaultValues: filterValues
    });

    console.log("filters: ", filters);

    if (!displayedFilters.main) {
        return null;
    }

    const onSubmit = (values: { [key: string]: string }) => {
        if (Object.keys(values).length > 0) {
            setFilters(values, displayedFilters);
        } else {
            hideFilter("main");
        }
    };

        return (
            <FormProvider {...form}>
                <form onSubmit={form.handleSubmit(onSubmit)}>
                    <Box display="flex" alignItems="flex-end" mb={1}>
                        {Object.keys(filters).map((filterKey: string) => {
                            return (
                                <SelectInput
                                    key={filterKey}
                                    source={filterKey}
                                    choices={Object.keys(filters[filterKey]).map(value => ({
                                        id: value,
                                        name: value
                                    }))}
                                />
                            );
                        })}{" "}
                    </Box>
                </form>
            </FormProvider>
        );
};

I tried to access the meta object from the ListContext, but there doesn't seem to be a way to pass it into the context from the dataprovider.

Any help appreciated!

0

There are 0 best solutions below