Hello everyone :wave: does anyone know how can i g...
# gooddata-ui
m
Hello everyone 👋 does anyone know how can i get current dashboard filters context? I am trying to apply current filters in a custom plugin code
i see there is a redux store and inside the store there is filter context, can i get the store instance from widget code
I found a way to be able to apply date filters to the plugin, it's kinda workaround but works 🙂 first added an effect to the component to get filters from dashboard on initiialize and on filters chage:
Copy code
useEffect(() => {
        emitter.subscribe((value: any) => {
            if(value.type === 'GDC.DASH/EVT.FILTER_CONTEXT.DATE_FILTER.SELECTION_CHANGED') {
                const {filter} = value;
                if(filter.type === 'relative') {
                    const { from, to, granularity } = filter;
                    setFilters(newRelativeDateFilter(Md.DateDatasets.CreatedAt, granularity, from, to));
                }
                return
            }
            const ff = filterContextItemsToDashboardFiltersByDateDataSet(value.filters, Md.DateDatasets.CreatedAt);
            const { relativeDateFilter } = ff[0] as any;
            const { from, to, granularity } = relativeDateFilter;
            setFilters(newRelativeDateFilter(Md.DateDatasets.CreatedAt, granularity, from, to));
        });
        pluginReeady.emit(true);
    }, []);
inside plugin register function added event handlers:
Copy code
handlers.addEventHandler("GDC.DASH/EVT.INITIALIZED", (evt) => {
            pluginReeady.subscribe((value: any) => {
                emitter.emit(evt.payload.dashboard.filterContext);
            });
        });

        handlers.addEventHandler("GDC.DASH/EVT.FILTER_CONTEXT.DATE_FILTER.SELECTION_CHANGED", (evt) => {
            emitter.emit({filter: evt.payload.filter.dateFilter, type: evt.type});
        });
one thing to is that, dashboard init is called before plugin is rendered so you need a way to tell dashboard that plugin is ready
🙌🏼 1