Skip to content

Commit

Permalink
Remove usages of query-string in cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed May 11, 2023
1 parent 3b64113 commit 1ef0211
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/cases/public/common/navigation/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import { useCallback, useEffect, useState } from 'react';
import { useLocation, useParams } from 'react-router-dom';

import { parse, stringify } from 'query-string';
import { APP_ID, CASES_CONFIGURE_PATH, CASES_CREATE_PATH } from '../../../common/constants';
import { useNavigation } from '../lib/kibana';
import { useCasesContext } from '../../components/cases_context/use_cases_context';
import type { ICasesDeepLinkId } from './deep_links';
import type { CaseViewPathParams, CaseViewPathSearchParams } from './paths';
import { generateCaseViewPath } from './paths';

// @ts-ignore
const stringify = (parsedParams) => new URLSearchParams(parsedParams).toString();
const parse = (queryString: string) => Object.fromEntries(new URLSearchParams(queryString));

export const useCaseViewParams = () => useParams<CaseViewPathParams>();

export function useUrlParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
getFilterOptionsLocalStorageKey,
} from './use_all_cases_state';
import { DEFAULT_FILTER_OPTIONS, DEFAULT_QUERY_PARAMS } from '../../containers/use_get_cases';
import { stringify } from 'query-string';
import { DEFAULT_TABLE_ACTIVE_PAGE, DEFAULT_TABLE_LIMIT } from '../../containers/constants';
import { CaseStatuses } from '../../../common';
import { SortFieldCase } from '../../containers/types';
Expand Down Expand Up @@ -56,6 +55,9 @@ const APP_ID = 'testAppId';
const LOCALSTORAGE_QUERY_PARAMS_KEY = getQueryParamsLocalStorageKey(APP_ID);
const LOCALSTORAGE_FILTER_OPTIONS_KEY = getFilterOptionsLocalStorageKey(APP_ID);

// @ts-ignore
const stringify = (parsedParams) => new URLSearchParams(parsedParams).toString();

describe('useAllCasesQueryParams', () => {
beforeEach(() => {
localStorage.clear();
Expand Down Expand Up @@ -173,7 +175,6 @@ describe('useAllCasesQueryParams', () => {

it('takes into account existing url filter options on first run', () => {
const nonDefaultUrlParams = { severity: 'critical', status: 'open' };
const expectedUrl = { ...URL_DEFAULTS, ...nonDefaultUrlParams };

mockLocation.search = stringify(nonDefaultUrlParams);

Expand All @@ -182,15 +183,14 @@ describe('useAllCasesQueryParams', () => {
});

expect(useHistory().replace).toHaveBeenCalledWith({
search: stringify(expectedUrl),
search: 'severity=critical&status=open&page=1&perPage=10&sortField=createdAt&sortOrder=desc',
});
});

it('preserves other url parameters', () => {
const nonDefaultUrlParams = {
foo: 'bar',
};
const expectedUrl = { ...URL_DEFAULTS, ...nonDefaultUrlParams };

mockLocation.search = stringify(nonDefaultUrlParams);

Expand All @@ -199,7 +199,8 @@ describe('useAllCasesQueryParams', () => {
});

expect(useHistory().replace).toHaveBeenCalledWith({
search: stringify(expectedUrl),
search:
'foo=bar&page=1&perPage=10&sortField=createdAt&sortOrder=desc&severity=all&status=all',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useLocation, useHistory } from 'react-router-dom';
import { isEqual } from 'lodash';

import useLocalStorage from 'react-use/lib/useLocalStorage';
import { parse, stringify } from 'query-string';

import { DEFAULT_FILTER_OPTIONS, DEFAULT_QUERY_PARAMS } from '../../containers/use_get_cases';
import { parseUrlQueryParams } from './utils';
Expand All @@ -36,6 +35,10 @@ export const getFilterOptionsLocalStorageKey = (appId: string) => {
return `${appId}.${filteringKey}`;
};

// @ts-ignore
const stringify = (parsedParams) => new URLSearchParams(parsedParams).toString();
const parse = (queryString: string) => Object.fromEntries(new URLSearchParams(queryString));

const getQueryParams = (
params: PartialQueryParams,
urlParams: PartialQueryParams,
Expand Down

0 comments on commit 1ef0211

Please sign in to comment.