From ca377d46692a17f2a87437ff9a6cae79802dd7c1 Mon Sep 17 00:00:00 2001 From: Edward Brunton <42774600+EdwardBrunton@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:46:07 +0100 Subject: [PATCH] wip --- .../src/lib/classes/powerBiController.ts | 1 + .../src/lib/components/Filter/Filter.tsx | 83 ++++++++----------- .../components/QuickFilter/QuickFilter.tsx | 9 +- .../power-bi/src/lib/utils/getFiltersAsync.ts | 43 +++------- 4 files changed, 49 insertions(+), 87 deletions(-) diff --git a/packages/power-bi/src/lib/classes/powerBiController.ts b/packages/power-bi/src/lib/classes/powerBiController.ts index a985d9fcc..6a64db9cb 100644 --- a/packages/power-bi/src/lib/classes/powerBiController.ts +++ b/packages/power-bi/src/lib/classes/powerBiController.ts @@ -34,6 +34,7 @@ export class PowerBiController { }; reportReady = (newValue: Report) => { + console.log('Report Ready'); newValue.getActivePage().then(this.setActivePage); newValue.on('pageChanged', (page: ICustomEvent) => { this.setActivePage(page.detail.newPage); diff --git a/packages/power-bi/src/lib/components/Filter/Filter.tsx b/packages/power-bi/src/lib/components/Filter/Filter.tsx index 96f9b7e8b..8e455cdb7 100644 --- a/packages/power-bi/src/lib/components/Filter/Filter.tsx +++ b/packages/power-bi/src/lib/components/Filter/Filter.tsx @@ -155,57 +155,46 @@ export const PowerBIFilter = ({ report, options }: PowerBIFilterProps): JSX.Elem }, []); /** - * Effect should be triggered when report has first loaded, - * initializing all possible filters and also checking for default active filters. - * Also need to handle when user changes page, so this effect has to also be triggered when - * page is changed. - */ - useEffect(() => { - if (report) { - const initFilters = async () => { - const filters = await getFiltersAsync(report); - const defaultActiveFilters = await getActiveFilterValues(filters); - setSlicerFilters(filters.sort((a, b) => a.type.localeCompare(b.type))); - setActiveFilters(defaultActiveFilters); - const state = getVisibleFiltersFromLocalStorage(report.getId()); - if (state) { - setFilterGroupVisible(state); - } else { - setFilterGroupVisible(filters.map((s) => s.type)); - } - setisFiltersLoading(false); - }; - - initFilters(); - } - }, [report]); - - /** - * Effect should be triggered when activeFilters has changed. - * Some filters may not longer be applicable, therefore the need to get filters again. - * Dependency array needs to check for length because checking only object will not fire the effect. + * Triggers when the report is loaded + * It handles the initial setup of filters and active filters as well as setting up event listeners for future filter changes */ useEffect(() => { + const updateFilters = async () => { + console.log('updateFilters'); + var filters = await getFiltersAsync(report); + setSlicerFilters(filters.sort((a, b) => a.type.localeCompare(b.type))); + const filterGroupNames = getActiveFilterGroupArray(activeFilters); + const reportId = report.getId(); + const state = getVisibleFiltersFromLocalStorage(reportId); + if (state) { + setFilterGroupVisible(state); + } else { + setFilterGroupVisible((s) => [...s, ...filterGroupNames].filter((v, i, a) => a.indexOf(v) === i)); + } + setisFiltersLoading(false); + console.log('updateFilters done', isFiltersLoading, activeFilters, slicerFilters); + }; + + const initFilters = async () => { + debugger; + report.off('rendered', initFilters); + console.log('initFilters'); + const filters = await getFiltersAsync(report); + const defaultActiveFilters = await getActiveFilterValues(filters); + setActiveFilters(defaultActiveFilters); + report.on('rendered', updateFilters); + updateFilters(); + }; if (report) { - const reCreateFilters = async () => { - const filters = await getFiltersAsync(report); - - setSlicerFilters(filters.sort((a, b) => a.type.localeCompare(b.type))); - - const filterGroupNames = getActiveFilterGroupArray(activeFilters); - - const reportId = report.getId(); - - const state = getVisibleFiltersFromLocalStorage(reportId); - if (state) { - setFilterGroupVisible(state); - } else { - setFilterGroupVisible((s) => [...s, ...filterGroupNames].filter((v, i, a) => a.indexOf(v) === i)); - } - }; - reCreateFilters(); + console.log('initFilters'); + report.on('rendered', initFilters); } - }, [activeFilters, Object.keys(activeFilters).length]); + return () => { + report.off('rendered', initFilters); + report.off('rendered', updateFilters); + console.log('cleanup'); + }; + }, []); if (isFiltersLoading || !activeFilters || !slicerFilters) return ; diff --git a/packages/power-bi/src/lib/components/QuickFilter/QuickFilter.tsx b/packages/power-bi/src/lib/components/QuickFilter/QuickFilter.tsx index b22b9356d..70a5e06c7 100644 --- a/packages/power-bi/src/lib/components/QuickFilter/QuickFilter.tsx +++ b/packages/power-bi/src/lib/components/QuickFilter/QuickFilter.tsx @@ -82,14 +82,7 @@ export const PowerBIQuickFilter = ({ controller }: PowerBIQuickFilterProps): JSX - + await resetFilter()} isDisabled={!isAnyFiltersActive()} />