diff --git a/src/client/components/ActivityFeed/CollectionList/state.js b/src/client/components/ActivityFeed/CollectionList/state.js index 9da289bb619..7a85e6d3362 100644 --- a/src/client/components/ActivityFeed/CollectionList/state.js +++ b/src/client/components/ActivityFeed/CollectionList/state.js @@ -1,7 +1,7 @@ import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from './constants' import { transformWasPolicyfeedBackProvidedToApi } from './transformers' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const TASK_GET_COMPANY_ACTIVITIES_LIST = 'TASK_GET_COMPANY_ACTIVITIES_LIST' @@ -26,8 +26,7 @@ export const TASK_GET_INTERACTIONS_COMPANY_NAME = export const ID = 'companyActivitiesList' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { currentAdviserId } = state const { metadata, selectedAdvisers, selectedCompanies, createdByOthers } = state[ID] diff --git a/src/client/components/ContactForm/index.jsx b/src/client/components/ContactForm/index.jsx index 3149a97e34b..383451b4a3d 100644 --- a/src/client/components/ContactForm/index.jsx +++ b/src/client/components/ContactForm/index.jsx @@ -1,4 +1,3 @@ -import qs from 'qs' import React, { useEffect } from 'react' import PropTypes from 'prop-types' import _ from 'lodash' @@ -9,6 +8,7 @@ import Label from '@govuk-react/label' import multiInstance from '../../utils/multiinstance' import { CONTACT_FORM__SUBMIT } from '../../actions' +import { getQueryParamsFromLocation } from '../../utils/url' import Form from '../Form' import { @@ -153,9 +153,8 @@ const _ContactForm = ({
{({ referrerUrl, router }) => { - const { origin_url, origin_search } = qs.parse( - router.location.search - ) + const { origin_url, origin_search } = + getQueryParamsFromLocation(router.location) const redirectTo = ({ name, id }) => { return origin_url ? appendParamsToUrl(origin_url, origin_search, id, name) diff --git a/src/client/components/Resource/Paginated.js b/src/client/components/Resource/Paginated.js index 8d1e548d30b..d2a2e6f0a83 100644 --- a/src/client/components/Resource/Paginated.js +++ b/src/client/components/Resource/Paginated.js @@ -5,6 +5,7 @@ import qs from 'qs' import styled from 'styled-components' +import { getQueryParamsFromLocation } from '../../utils/url' import multiInstance from '../../utils/multiinstance' import Task from '../Task' import LoadingBox from '../Task/LoadingBox' @@ -94,7 +95,7 @@ const PaginatedResource = multiInstance({ }) => ( {({ location }) => { - const qsParams = qs.parse(location.search.slice(1)) + const qsParams = getQueryParamsFromLocation(location) const routePage = parseInt(qsParams.page, 10) || 1 const totalPages = result ? Math.ceil(result.count / pageSize) : 0 const hasZeroResults = result?.count === 0 diff --git a/src/client/modules/Companies/CollectionList/state.js b/src/client/modules/Companies/CollectionList/state.js index 01958871bdd..9949239aeba 100644 --- a/src/client/modules/Companies/CollectionList/state.js +++ b/src/client/modules/Companies/CollectionList/state.js @@ -1,7 +1,7 @@ import { buildSelectedFilters } from './filters' import { COMPANY_STATUS_OPTIONS, SORT_OPTIONS } from './constants' import { transformArchivedToApi, transformPostcodeToApi } from './transformers' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const TASK_GET_COMPANIES_LIST = 'TASK_GET_COMPANIES_LIST' export const TASK_GET_COMPANIES_METADATA = 'TASK_GET_COMPANIES_METADATA' @@ -14,8 +14,7 @@ export const ID = 'companiesList' * Convert both location and redux state to props */ export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const archived = transformArchivedToApi(queryParams.archived) const ukPostcode = transformPostcodeToApi(queryParams.uk_postcode) diff --git a/src/client/modules/Companies/CompanyBusinessDetails/LinkGlobalHQ/state.js b/src/client/modules/Companies/CompanyBusinessDetails/LinkGlobalHQ/state.js index 4440ad77e6f..3194c81892d 100644 --- a/src/client/modules/Companies/CompanyBusinessDetails/LinkGlobalHQ/state.js +++ b/src/client/modules/Companies/CompanyBusinessDetails/LinkGlobalHQ/state.js @@ -1,7 +1,7 @@ import { transformPostcodeToApi } from '../../CollectionList/transformers' import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from '../../CollectionList/constants' -import { parseQueryString } from '../../../../utils' +import { locationToQSParamsWithPage } from '../../../../utils/url' export const ID = 'linkGlobalHQ' export const SET_GLOBAL_HQ_ID = 'setGlobalHQ' @@ -12,8 +12,7 @@ export const TASK_SET_GLOBAL_HQ = 'TASK_SET_GLOBAL_HQ' export const TASK_REMOVE_GLOBAL_HQ = 'TASK_REMOVE_GLOBAL_HQ' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const ukPostcode = transformPostcodeToApi(queryParams.uk_postcode) const { metadata } = state[ID] diff --git a/src/client/modules/Companies/CompanyBusinessDetails/LinkSubsidiary/state.js b/src/client/modules/Companies/CompanyBusinessDetails/LinkSubsidiary/state.js index 8070ebca09c..a5011263f1f 100644 --- a/src/client/modules/Companies/CompanyBusinessDetails/LinkSubsidiary/state.js +++ b/src/client/modules/Companies/CompanyBusinessDetails/LinkSubsidiary/state.js @@ -1,15 +1,14 @@ import { transformPostcodeToApi } from '../../CollectionList/transformers' import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from '../../CollectionList/constants' -import { parseQueryString } from '../../../../utils' +import { locationToQSParamsWithPage } from '../../../../utils/url' export const ID = 'linkSubsidiary' export const TASK_GET_SUBSIDIARY_LIST = 'TASK_GET_SUBSIDIARY_LIST' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const ukPostcode = transformPostcodeToApi(queryParams.uk_postcode) const { metadata } = state[ID] diff --git a/src/client/modules/Companies/CompanyOverview/TableCards/state.js b/src/client/modules/Companies/CompanyOverview/TableCards/state.js index 93fadc211b2..0e78862318d 100644 --- a/src/client/modules/Companies/CompanyOverview/TableCards/state.js +++ b/src/client/modules/Companies/CompanyOverview/TableCards/state.js @@ -1,5 +1,5 @@ import { SORT_OPTIONS } from '../../../Contacts/CollectionList/constants' -import { parseQueryString } from '../../../../utils' +import { locationToQSParamsWithPage } from '../../../../utils/url' export const OVERVIEW_COMPANY_PROJECTS_LIST_ID = 'overviewCompanyProjectsList' export const TASK_GET_PROJECT_WON_COUNT = 'TASK_GET_PROJECT_WON_COUNT' @@ -8,8 +8,7 @@ export const OVERVIEW_COMPANY_EXPORT_WINS_LIST_ID = export const TASK_GET_LATEST_EXPORT_WINS = 'TASK_GET_LATEST_EXPORT_WINS' export const companyProjectsState2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) return { ...state[OVERVIEW_COMPANY_PROJECTS_LIST_ID], @@ -21,8 +20,7 @@ export const companyProjectsState2props = ({ router, ...state }) => { } export const exportWinsState2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) return { ...state[OVERVIEW_COMPANY_EXPORT_WINS_LIST_ID], diff --git a/src/client/modules/Contacts/CollectionList/state.js b/src/client/modules/Contacts/CollectionList/state.js index 3f8e7c6fc98..02555064d98 100644 --- a/src/client/modules/Contacts/CollectionList/state.js +++ b/src/client/modules/Contacts/CollectionList/state.js @@ -1,6 +1,4 @@ -import { omitBy, isEmpty } from 'lodash' -import qs from 'qs' - +import { locationToQSParamsWithPage } from '../../../utils/url' import { buildSelectedFilters } from './filters' import { transformArchivedToApi } from './transformers' import { STATUS_OPTIONS, SORT_OPTIONS } from './constants' @@ -11,17 +9,8 @@ export const COMPANY_CONTACTS_LIST_ID = 'companyContactsList' export const TASK_GET_CONTACTS_LIST = 'TASK_GET_CONTACTS_LIST' export const TASK_GET_CONTACTS_METADATA = 'TASK_GET_CONTACTS_METADATA' -const getQueryParams = (router) => { - const queryString = router.location.search.slice(1) - const queryParams = omitBy({ ...qs.parse(queryString) }, isEmpty) - return { - ...queryParams, - page: parseInt(queryParams.page || 1, 10), - } -} - export const contactsState2props = ({ router, ...state }) => { - const queryParams = getQueryParams(router) + const queryParams = locationToQSParamsWithPage(router.location) const metadata = state[CONTACTS_LIST_ID].metadata const selectedFilters = buildSelectedFilters(queryParams, metadata) const archived = transformArchivedToApi(queryParams.archived) @@ -42,7 +31,7 @@ export const contactsState2props = ({ router, ...state }) => { } export const companyContactsState2props = ({ router, ...state }) => { - const queryParams = getQueryParams(router) + const queryParams = locationToQSParamsWithPage(router.location) const archived = transformArchivedToApi(queryParams.archived) return { diff --git a/src/client/modules/Contacts/ContactActivity/state.js b/src/client/modules/Contacts/ContactActivity/state.js index 74f86837d4c..27e65179d74 100644 --- a/src/client/modules/Contacts/ContactActivity/state.js +++ b/src/client/modules/Contacts/ContactActivity/state.js @@ -1,12 +1,12 @@ -import qs from 'qs' +import { getQueryParamsFromLocation } from '../../../utils/url' export const TASK_GET_CONTACT_ACTIVITIES = 'TASK_GET_CONTACT_ACTIVITIES' export const ID = 'contactActivity' export const state2props = ({ ...state }) => { - const selectedSortBy = qs.parse(location.search.slice(1)).sortby || 'newest' - const page = qs.parse(location.search.slice(1)).page || '1' + const selectedSortBy = getQueryParamsFromLocation(location).sortby || 'newest' + const page = getQueryParamsFromLocation(location).page || '1' return { ...state[ID], diff --git a/src/client/modules/Events/AttendeeSearch/state.js b/src/client/modules/Events/AttendeeSearch/state.js index 48248ea1448..2dc57f55b9b 100644 --- a/src/client/modules/Events/AttendeeSearch/state.js +++ b/src/client/modules/Events/AttendeeSearch/state.js @@ -1,6 +1,4 @@ -import { omitBy, isEmpty } from 'lodash' -import qs from 'qs' - +import { locationToQSParamsWithPage } from '../../../utils/url' import { SORT_OPTIONS } from '../../Contacts/CollectionList/constants' import { buildSelectedFilters } from './filters' @@ -8,17 +6,8 @@ export const TASK_SEARCH_ATTENDEE = 'TASK_SEARCH_ATTENDEE' export const TASK_GET_ATTENDEE_METADATA = 'TASK_GET_ATTENDEE_METADATA' export const SEARCH_ATTENDEE_ID = 'findAttendees' -const getQueryParams = (router) => { - const queryString = router.location.search.slice(1) - const queryParams = omitBy({ ...qs.parse(queryString) }, isEmpty) - return { - ...queryParams, - page: parseInt(queryParams.page || 1, 10), - } -} - export const state2props = ({ router, ...state }) => { - const queryParams = getQueryParams(router) + const queryParams = locationToQSParamsWithPage(router.location) const metadata = state[SEARCH_ATTENDEE_ID].metadata const selectedFilters = buildSelectedFilters(queryParams, metadata) diff --git a/src/client/modules/Events/CollectionList/state.js b/src/client/modules/Events/CollectionList/state.js index 501963e066d..03b7a7998fc 100644 --- a/src/client/modules/Events/CollectionList/state.js +++ b/src/client/modules/Events/CollectionList/state.js @@ -1,6 +1,6 @@ import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from './constants' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const TASK_GET_EVENTS_LIST = 'TASK_GET_EVENTS_LIST' export const TASK_GET_EVENTS_METADATA = 'TASK_GET_EVENTS_METADATA' @@ -14,8 +14,7 @@ export const ID = 'eventsList' * Convert both location and redux state to props */ export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { metadata, selectedOrganisers } = state[ID] diff --git a/src/client/modules/Events/EventAventriRegistrationStatus/state.js b/src/client/modules/Events/EventAventriRegistrationStatus/state.js index f27cdf60924..96d5b5d97f2 100644 --- a/src/client/modules/Events/EventAventriRegistrationStatus/state.js +++ b/src/client/modules/Events/EventAventriRegistrationStatus/state.js @@ -1,7 +1,5 @@ -import qs from 'qs' - import { EVENT_ATTENDEES_MAPPING } from '../../../../apps/companies/apps/activity-feed/constants' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const TASK_GET_EVENT_AVENTRI_REGISTRATION_STATUS_ATTENDEES = 'TASK_GET_EVENT_AVENTRI_REGISTRATION_STATUS_ATTENDEES' @@ -15,12 +13,9 @@ export const mapUrlSlugToRegistrationStatus = (urlSlug) => { return Array.isArray(status) ? status[0] : null } -export const state2props = (state, router) => { - const { match, location } = router - const queryString = location.search.slice(1) - const queryParams = parseQueryString(queryString) - const selectedSortBy = - qs.parse(location.search.slice(1)).sortby || 'first_name:asc' +export const state2props = (state, { match, location }) => { + const queryParams = locationToQSParamsWithPage(location) + const selectedSortBy = queryParams.sortby || 'first_name:asc' const registrationStatus = mapUrlSlugToRegistrationStatus(match.params.status) diff --git a/src/client/modules/Interactions/CollectionList/state.js b/src/client/modules/Interactions/CollectionList/state.js index be46495c306..366efc0b5b7 100644 --- a/src/client/modules/Interactions/CollectionList/state.js +++ b/src/client/modules/Interactions/CollectionList/state.js @@ -1,7 +1,7 @@ import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from './constants' import { transformWasPolicyfeedBackProvidedToApi } from './transformers' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const TASK_GET_INTERACTIONS_LIST = 'TASK_GET_INTERACTIONS_LIST' export const TASK_GET_INTERACTIONS_ADVISER_NAME = @@ -14,8 +14,7 @@ export const TASK_GET_INTERACTIONS_COMPANY_NAME = export const ID = 'interactionsList' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { currentAdviserId } = state const { metadata, selectedAdvisers, selectedTeams, selectedCompanies } = state[ID] diff --git a/src/client/modules/Investments/Opportunities/CollectionList/state.js b/src/client/modules/Investments/Opportunities/CollectionList/state.js index fd946072398..eb3c96af496 100644 --- a/src/client/modules/Investments/Opportunities/CollectionList/state.js +++ b/src/client/modules/Investments/Opportunities/CollectionList/state.js @@ -1,5 +1,4 @@ -import qs from 'qs' - +import { getQueryParamsFromLocation } from '../../../../utils/url' import { sortOptions } from './metadata' export const TASK_GET_OPPORTUNITIES_LIST = 'TASK_GET_OPPORTUNITIES_LIST' @@ -19,7 +18,7 @@ const collectionListPayload = (paramProps) => { export const state2props = ({ router, ...state }) => { const { metadata } = state.opportunitiesList - const queryProps = qs.parse(router.location.search.slice(1)) + const queryProps = getQueryParamsFromLocation(router.location) const filteredQueryProps = collectionListPayload(queryProps) return { diff --git a/src/client/modules/Investments/Profiles/state.js b/src/client/modules/Investments/Profiles/state.js index 1492334aaf1..3adc490124e 100644 --- a/src/client/modules/Investments/Profiles/state.js +++ b/src/client/modules/Investments/Profiles/state.js @@ -1,6 +1,7 @@ -import qs from 'qs' import { omit } from 'lodash' +import { getQueryParamsFromLocation } from '../../../utils/url' + export const TASK_GET_PROFILES_LIST = 'TASK_GET_PROFILES_LIST' export const ID = 'profilesList' @@ -26,5 +27,5 @@ const collectionListPayload = ({ export const state2props = ({ router, ...state }) => ({ ...state[ID], - payload: collectionListPayload(qs.parse(router.location.search.slice(1))), + payload: collectionListPayload(getQueryParamsFromLocation(router.location)), }) diff --git a/src/client/modules/Investments/Projects/Details/EditAssociatedProject/state.js b/src/client/modules/Investments/Projects/Details/EditAssociatedProject/state.js index 06d08a64b7b..a165e9f53c5 100644 --- a/src/client/modules/Investments/Projects/Details/EditAssociatedProject/state.js +++ b/src/client/modules/Investments/Projects/Details/EditAssociatedProject/state.js @@ -1,4 +1,4 @@ -import { parseQueryString } from '../../../../../utils' +import { locationToQSParamsWithPage } from '../../../../../utils/url' import { generateFinancialYearLabel, getFinancialYearStart, @@ -12,9 +12,7 @@ export const TASK_GET_NON_FDI_PROJECTS_LIST = 'TASK_GET_NON_FDI_PROJECTS_LIST' export const NON_FDI_LIST_ID = 'nonFdiProjectsList' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { metadata } = state[NON_FDI_LIST_ID] const financialYearStart = getFinancialYearStart(new Date()) const financialYearOptions = [ diff --git a/src/client/modules/Investments/Projects/Details/EditRecipientCompany/state.js b/src/client/modules/Investments/Projects/Details/EditRecipientCompany/state.js index 353400d1013..faf4065f717 100644 --- a/src/client/modules/Investments/Projects/Details/EditRecipientCompany/state.js +++ b/src/client/modules/Investments/Projects/Details/EditRecipientCompany/state.js @@ -1,4 +1,4 @@ -import { parseQueryString } from '../../../../../utils' +import { locationToQSParamsWithPage } from '../../../../../utils/url' import { transformPostcodeToApi } from '../../../../Companies/CollectionList/transformers' import { COMPANY_STATUS_OPTIONS, @@ -12,8 +12,7 @@ export const TASK_UPDATE_RECIPIENT_COMPANY = 'TASK_UPDATE_RECIPIENT_COMPANY' export const RECIPIENT_COMPANY_LIST_ID = 'recipientCompanyList' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const ukPostcode = transformPostcodeToApi(queryParams.uk_postcode) const { metadata } = state[RECIPIENT_COMPANY_LIST_ID] diff --git a/src/client/modules/Investments/Projects/state.js b/src/client/modules/Investments/Projects/state.js index 6f7feb07c51..b4023a12521 100644 --- a/src/client/modules/Investments/Projects/state.js +++ b/src/client/modules/Investments/Projects/state.js @@ -2,7 +2,7 @@ import { getFinancialYearStart, generateFinancialYearLabel, } from '../../../utils/date' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' import { buildSelectedFilters } from './filters' import { SORT_OPTIONS, @@ -31,9 +31,7 @@ export const ID = 'propositionComplete' export const TASK_PROPOSITION_COMPLETE = 'TASK_PROPOSITION_COMPLETE' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { metadata, selectedAdvisers, results } = state[INVESTMENT_PROJECTS_ID] const financialYearStart = getFinancialYearStart(new Date()) const financialYearOptions = [ diff --git a/src/client/modules/Omis/CollectionList/state.js b/src/client/modules/Omis/CollectionList/state.js index 3068512d346..0785c1a3555 100644 --- a/src/client/modules/Omis/CollectionList/state.js +++ b/src/client/modules/Omis/CollectionList/state.js @@ -5,7 +5,7 @@ import { STATUSES, RECONCILIATION_STATUSES, } from './constants' -import { parseQueryString } from '../../../utils' +import { locationToQSParamsWithPage } from '../../../utils/url' export const ORDERS_LIST_ID = 'ordersList' export const COMPANY_ORDERS_LIST_ID = 'companyOrdersList' @@ -18,8 +18,7 @@ export const TASK_GET_ORDERS_RECONCILIATION_METADATA = 'TASK_GET_ORDERS_RECONCILIATION_METADATA' export const ordersState2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { metadata } = state[ORDERS_LIST_ID] const selectedFilters = buildSelectedFilters(queryParams, metadata) return { @@ -35,8 +34,7 @@ export const ordersState2props = ({ router, ...state }) => { } export const companyOrdersState2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) return { ...state[COMPANY_ORDERS_LIST_ID], payload: { ...queryParams }, @@ -49,8 +47,7 @@ export const companyOrdersState2props = ({ router, ...state }) => { } export const reconciliationOrdersState2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const { metadata } = state[ORDERS_RECONCILIATION_LIST_ID] const selectedFilters = buildSelectedFilters(queryParams, metadata) return { diff --git a/src/client/modules/Omis/CreateOrder/CompanySelect/state.js b/src/client/modules/Omis/CreateOrder/CompanySelect/state.js index 5b78fd552b3..19061ebf697 100644 --- a/src/client/modules/Omis/CreateOrder/CompanySelect/state.js +++ b/src/client/modules/Omis/CreateOrder/CompanySelect/state.js @@ -1,15 +1,14 @@ import { transformPostcodeToApi } from '../../../Companies/CollectionList/transformers' import { buildSelectedFilters } from './filters' import { SORT_OPTIONS } from '../../../Companies/CollectionList/constants' -import { parseQueryString } from '../../../../utils' +import { locationToQSParamsWithPage } from '../../../../utils/url' export const ID = 'selectOmisCompany' export const TASK_GET_COMPANIES = 'TASK_GET_COMPANIES' export const state2props = ({ router, ...state }) => { - const queryString = router.location.search.slice(1) - const queryParams = parseQueryString(queryString) + const queryParams = locationToQSParamsWithPage(router.location) const ukPostcode = transformPostcodeToApi(queryParams.uk_postcode) const { metadata } = state[ID] diff --git a/src/client/modules/Tasks/TaskDetails/state.js b/src/client/modules/Tasks/TaskDetails/state.js index 12c4b7c0987..b348f9e8415 100644 --- a/src/client/modules/Tasks/TaskDetails/state.js +++ b/src/client/modules/Tasks/TaskDetails/state.js @@ -1,7 +1,6 @@ -import qs from 'qs' - import { transformIdNameToValueLabel } from '../../../transformers' import { getTaskBreadcrumbs } from '../TaskForm/state' +import { getQueryParamsFromLocation } from '../../../utils/url' export const ID = 'taskDetails' @@ -25,6 +24,6 @@ export const state2props = (state) => { export const buttonState2props = ({ router }) => { const { location } = router - const { returnUrl } = qs.parse(location.search.slice(1)) + const { returnUrl } = getQueryParamsFromLocation(location) return { returnUrl: returnUrl } } diff --git a/src/client/utils/index.js b/src/client/utils/index.js index 981f2126775..617da464f9e 100644 --- a/src/client/utils/index.js +++ b/src/client/utils/index.js @@ -1,5 +1,4 @@ -import { isEmpty, omitBy, isPlainObject, camelCase } from 'lodash' -import qs from 'qs' +import { isPlainObject, camelCase } from 'lodash' export const idNameToValueLabel = ({ id, name }) => ({ value: id, @@ -8,14 +7,6 @@ export const idNameToValueLabel = ({ id, name }) => ({ export const idNamesToValueLabels = (idNames) => idNames.map(idNameToValueLabel) -export const parseQueryString = (queryString) => { - const queryParams = omitBy({ ...qs.parse(queryString) }, isEmpty) - return { - ...queryParams, - page: parseInt(queryParams.page || 1, 10), - } -} - export const deepKeysToCamelCase = (x) => Array.isArray(x) ? x.map(deepKeysToCamelCase) diff --git a/src/client/utils/url.js b/src/client/utils/url.js index da95d8b56f6..6a3015cf20e 100644 --- a/src/client/utils/url.js +++ b/src/client/utils/url.js @@ -1,7 +1,15 @@ -import { omitBy, isEmpty } from 'lodash' -import qs from 'qs' +export const parseQueryString = (queryString) => + Object.fromEntries(new URLSearchParams(queryString).entries()) -export const getQueryParamsFromLocation = (location) => { - const queryString = location.search.slice(1) - return omitBy({ ...qs.parse(queryString) }, isEmpty) +// TODO: Rename to locationToQSParams +export const getQueryParamsFromLocation = (location) => + parseQueryString(location.search) + +export const locationToQSParamsWithPage = (location) => { + const queryParams = parseQueryString(location.search) + + return { + ...queryParams, + page: parseInt(queryParams.page || 1, 10), + } }