datahub/datahub-web-react/src/app/shared/updateQueryParams.ts

21 lines
627 B
TypeScript
Raw Normal View History

import * as QueryString from 'query-string';
import { Location, History } from 'history';
type QueryParam = {
[key: string]: string | undefined;
};
export default function updateQueryParams(newParams: QueryParam, location: Location, history: History) {
const parsedParams = QueryString.parse(location.search, { arrayFormat: 'comma' });
const updatedParams = {
...parsedParams,
...newParams,
};
const stringifiedParams = QueryString.stringify(updatedParams, { arrayFormat: 'comma' });
history.replace({
pathname: location.pathname,
search: stringifiedParams,
});
}