From 28e728a07544c59ca161a7e1c834628bda9f606f Mon Sep 17 00:00:00 2001 From: "Michael S. molina" Date: Thu, 23 Mar 2023 13:52:50 -0300 Subject: [PATCH 1/3] fix: Preserves selected scopes when toggling between scope types --- .../FilterScope/FilterScope.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx index ce1a510e20a64..35fb2373fad41 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx @@ -67,6 +67,8 @@ const FilterScope: FC = ({ const [initialFilterScope] = useState( filterScope || getDefaultScopeValue(chartId, initiallyExcludedCharts), ); + const [lastSpecificScope, setLastSpecificScope] = + useState(initialFilterScope); const [initialScopingType] = useState( isScopingAll(initialFilterScope, chartId) ? ScopingType.all @@ -78,10 +80,13 @@ const FilterScope: FC = ({ const onUpdateFormValues = useCallback( (formValues: any) => { + if (formScopingType === ScopingType.specific) { + setLastSpecificScope(formValues.scope); + } updateFormValues(formValues); setHasScopeBeenModified(true); }, - [updateFormValues], + [formScopingType, updateFormValues], ); const updateScopes = useCallback(() => { @@ -113,12 +118,11 @@ const FilterScope: FC = ({ > { - if (value === ScopingType.all) { - const scope = getDefaultScopeValue(chartId); - updateFormValues({ - scope, - }); - } + const scope = + value === ScopingType.all + ? getDefaultScopeValue(chartId) + : lastSpecificScope; + updateFormValues({ scope }); setHasScopeBeenModified(true); forceUpdate(); }} From 82943a1b29e39eaa3c8fdd525daeb49c89cb651b Mon Sep 17 00:00:00 2001 From: "Michael S. molina" Date: Thu, 23 Mar 2023 16:51:31 -0300 Subject: [PATCH 2/3] Fixes test --- .../FiltersConfigForm/FilterScope/FilterScope.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.test.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.test.tsx index 10b48b9d9cc87..a808823a094f1 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.test.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.test.tsx @@ -129,7 +129,7 @@ describe('FilterScope', () => { expect(screen.getByRole('tree')).toBeInTheDocument(); expect( document.querySelectorAll('.ant-tree-checkbox-checked').length, - ).toBe(1); + ).toBe(4); }); }); }); From b92f12348d3719db6506950124479564529b9087 Mon Sep 17 00:00:00 2001 From: "Michael S. molina" Date: Tue, 28 Mar 2023 13:44:19 -0300 Subject: [PATCH 3/3] Uses useRef for lastSpecificScope --- .../FiltersConfigForm/FilterScope/FilterScope.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx index 35fb2373fad41..0682279120d0b 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/FilterScope.tsx @@ -17,7 +17,7 @@ * under the License. */ -import React, { FC, useCallback, useState } from 'react'; +import React, { FC, useCallback, useRef, useState } from 'react'; import { NativeFilterScope, styled, @@ -67,8 +67,7 @@ const FilterScope: FC = ({ const [initialFilterScope] = useState( filterScope || getDefaultScopeValue(chartId, initiallyExcludedCharts), ); - const [lastSpecificScope, setLastSpecificScope] = - useState(initialFilterScope); + const lastSpecificScope = useRef(initialFilterScope); const [initialScopingType] = useState( isScopingAll(initialFilterScope, chartId) ? ScopingType.all @@ -81,7 +80,7 @@ const FilterScope: FC = ({ const onUpdateFormValues = useCallback( (formValues: any) => { if (formScopingType === ScopingType.specific) { - setLastSpecificScope(formValues.scope); + lastSpecificScope.current = formValues.scope; } updateFormValues(formValues); setHasScopeBeenModified(true); @@ -121,7 +120,7 @@ const FilterScope: FC = ({ const scope = value === ScopingType.all ? getDefaultScopeValue(chartId) - : lastSpecificScope; + : lastSpecificScope.current; updateFormValues({ scope }); setHasScopeBeenModified(true); forceUpdate();