Skip to content

Commit

Permalink
Update logic to make it simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
chantal-kelm committed Jun 24, 2024
1 parent 474ff63 commit 363c107
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export const RegistryTable = withRouterSearch(
};
}
closeFlyout = () => {
NavigationService.getInstance().updateAndNavigateSearchParams({}, [
'file',
]);
NavigationService.getInstance().updateAndNavigateSearchParams({
file: null,
});
};

columns() {
Expand Down Expand Up @@ -101,10 +101,9 @@ export const RegistryTable = withRouterSearch(
return {
'data-test-subj': `row-${file}`,
onClick: () => {
NavigationService.getInstance().updateAndNavigateSearchParams(
{ file },
[],
);
NavigationService.getInstance().updateAndNavigateSearchParams({
file,
});
},
};
};
Expand Down
13 changes: 6 additions & 7 deletions plugins/main/public/components/agents/fim/inventory/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export const InventoryTable = withRouterSearch(
}

closeFlyout = () => {
NavigationService.getInstance().updateAndNavigateSearchParams({}, [
'file',
]);
NavigationService.getInstance().updateAndNavigateSearchParams({
file: null,
});
};

columns() {
Expand Down Expand Up @@ -147,10 +147,9 @@ export const InventoryTable = withRouterSearch(
return {
'data-test-subj': `row-${file}`,
onClick: () => {
NavigationService.getInstance().updateAndNavigateSearchParams(
{ file },
[],
);
NavigationService.getInstance().updateAndNavigateSearchParams({
file,
});
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
}

Expand Down
28 changes: 11 additions & 17 deletions plugins/main/public/react-services/navigation-service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down

0 comments on commit 363c107

Please sign in to comment.