From 363c10731ff2d0de23ecfbcdd013cc2bb072280c Mon Sep 17 00:00:00 2001 From: "chantal.kelm" Date: Mon, 24 Jun 2024 10:47:46 -0300 Subject: [PATCH] Update logic to make it simpler --- .../agents/fim/inventory/registry-table.tsx | 13 ++++----- .../components/agents/fim/inventory/table.tsx | 13 ++++----- .../overview/mitre/intelligence/resource.tsx | 8 +++--- .../management/groups/groups-main.js | 7 ++--- .../management/ruleset/views/rule-info.tsx | 15 ++++------ .../react-services/navigation-service.tsx | 28 ++++++++----------- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/plugins/main/public/components/agents/fim/inventory/registry-table.tsx b/plugins/main/public/components/agents/fim/inventory/registry-table.tsx index b083efa596..58dd8f2e84 100644 --- a/plugins/main/public/components/agents/fim/inventory/registry-table.tsx +++ b/plugins/main/public/components/agents/fim/inventory/registry-table.tsx @@ -61,9 +61,9 @@ export const RegistryTable = withRouterSearch( }; } closeFlyout = () => { - NavigationService.getInstance().updateAndNavigateSearchParams({}, [ - 'file', - ]); + NavigationService.getInstance().updateAndNavigateSearchParams({ + file: null, + }); }; columns() { @@ -101,10 +101,9 @@ export const RegistryTable = withRouterSearch( return { 'data-test-subj': `row-${file}`, onClick: () => { - NavigationService.getInstance().updateAndNavigateSearchParams( - { file }, - [], - ); + NavigationService.getInstance().updateAndNavigateSearchParams({ + file, + }); }, }; }; diff --git a/plugins/main/public/components/agents/fim/inventory/table.tsx b/plugins/main/public/components/agents/fim/inventory/table.tsx index 43f92c8829..f9021891db 100644 --- a/plugins/main/public/components/agents/fim/inventory/table.tsx +++ b/plugins/main/public/components/agents/fim/inventory/table.tsx @@ -63,9 +63,9 @@ export const InventoryTable = withRouterSearch( } closeFlyout = () => { - NavigationService.getInstance().updateAndNavigateSearchParams({}, [ - 'file', - ]); + NavigationService.getInstance().updateAndNavigateSearchParams({ + file: null, + }); }; columns() { @@ -147,10 +147,9 @@ export const InventoryTable = withRouterSearch( return { 'data-test-subj': `row-${file}`, onClick: () => { - NavigationService.getInstance().updateAndNavigateSearchParams( - { file }, - [], - ); + NavigationService.getInstance().updateAndNavigateSearchParams({ + file, + }); }, }; }; diff --git a/plugins/main/public/components/overview/mitre/intelligence/resource.tsx b/plugins/main/public/components/overview/mitre/intelligence/resource.tsx index 1f5f79a2cc..575a34ded8 100644 --- a/plugins/main/public/components/overview/mitre/intelligence/resource.tsx +++ b/plugins/main/public/components/overview/mitre/intelligence/resource.tsx @@ -40,10 +40,10 @@ export const ModuleMitreAttackIntelligenceResource = ({ const idToRedirect = urlParams.get('idToRedirect'); const endpoint = `/mitre/${redirectTab}?q=external_id=${idToRedirect}`; getMitreItemToRedirect(endpoint); - navigationService.updateAndNavigateSearchParams({}, [ - 'tabRedirect', - 'idToRedirect', - ]); + NavigationService.getInstance().updateAndNavigateSearchParams({ + tabRedirect: null, + idToRedirect: null, + }); } }, []); diff --git a/plugins/main/public/controllers/management/components/management/groups/groups-main.js b/plugins/main/public/controllers/management/components/management/groups/groups-main.js index 1adc011526..77ad7d7260 100644 --- a/plugins/main/public/controllers/management/components/management/groups/groups-main.js +++ b/plugins/main/public/controllers/management/components/management/groups/groups-main.js @@ -50,10 +50,9 @@ class WzGroups extends Component { }); const dataGroup = responseGroup?.data?.data?.affected_items?.[0]; this.props.updateGroupDetail(dataGroup); - NavigationService.getInstance().updateAndNavigateSearchParams( - { group: undefined }, - ['group'], - ); + NavigationService.getInstance().updateAndNavigateSearchParams({ + group: null, + }); } catch (error) { const options = { context: `${WzGroups.name}.componentDidMount`, diff --git a/plugins/main/public/controllers/management/components/management/ruleset/views/rule-info.tsx b/plugins/main/public/controllers/management/components/management/ruleset/views/rule-info.tsx index f2b7041456..33a67071aa 100644 --- a/plugins/main/public/controllers/management/components/management/ruleset/views/rule-info.tsx +++ b/plugins/main/public/controllers/management/components/management/ruleset/views/rule-info.tsx @@ -318,9 +318,9 @@ export default withRouterSearch( // TODO: the view of the rule flyout should be managed through the routing instead component // states if (this.props.search.redirectRule) { - NavigationService.getInstance().updateAndNavigateSearchParams({}, [ - 'redirectRule', - ]); + NavigationService.getInstance().updateAndNavigateSearchParams({ + redirectRule: null, + }); } this.props.cleanFilters(); this.props.onFiltersChange(filters); @@ -713,12 +713,9 @@ export default withRouterSearch( if (this.state.currentRuleId == ruleId) { return; } - NavigationService.getInstance().updateAndNavigateSearchParams( - { - redirectRule: ruleId, - }, - [], - ); + NavigationService.getInstance().updateAndNavigateSearchParams({ + redirectRule: ruleId, + }); this.setState({ currentRuleId: ruleId, isLoading: true }); } diff --git a/plugins/main/public/react-services/navigation-service.tsx b/plugins/main/public/react-services/navigation-service.tsx index 79c635ac7d..8c0a032e51 100644 --- a/plugins/main/public/react-services/navigation-service.tsx +++ b/plugins/main/public/react-services/navigation-service.tsx @@ -130,27 +130,21 @@ class NavigationService { .join('&'); } - public updateAndNavigateSearchParams( - updateParams: { [key: string]: string | undefined }, - deleteParams: string[] = [], - ): void { - const params = this.getParams(); - - // Update params - Object.entries(updateParams).forEach(([key, value]) => { - if (value !== undefined) { - params.set(key, value); + public updateAndNavigateSearchParams(params: { + [key: string]: string | null; + }): void { + const urlParams = this.getParams(); + + // Update or delete parameters according to their value + Object.entries(params).forEach(([key, value]) => { + if (value === null) { + urlParams.delete(key); } else { - params.delete(key); + urlParams.set(key, value); } }); - // Delete params - deleteParams.forEach(paramName => { - params.delete(paramName); - }); - - const queryString = this.buildSearch(params); + const queryString = this.buildSearch(urlParams); this.navigate(`${this.getPathname()}?${queryString}`); }