Skip to content

Commit

Permalink
Merge branch 'links-hotfix' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz committed Apr 4, 2024
2 parents 4faf2ec + 28f7169 commit a43e3c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
27 changes: 12 additions & 15 deletions app/react/Layout/CurrentLocationLink.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Link, useLocation } from 'react-router-dom';
import { toUrlParams } from 'shared/JSONRequest';

const newParams = (oldQuery, newQuery) => {
const params = { ...oldQuery, ...newQuery };
return Object.keys(params).reduce((memo, key) => {
if (params[key] !== '') {
return Object.assign(memo, { [key]: params[key] });
}
return memo;
}, {});
};

const validProps = props => {
const { to, ...valid } = props;
Expand All @@ -20,11 +9,19 @@ const validProps = props => {

const CurrentLocationLink = ({ children, queryParams, ...otherProps }) => {
const location = useLocation();
const query = new URLSearchParams(location.search);
Object.keys(queryParams).forEach(key => {
query.set(key, queryParams[key]);
});

query.forEach((value, key) => {
if (value === '') {
query.delete(key);
}
});

return (
<Link
to={`${location.pathname}${toUrlParams(newParams(location.query, queryParams))}`}
{...validProps(otherProps)}
>
<Link to={`${location.pathname}?${query.toString()}`} {...validProps(otherProps)}>
{children}
</Link>
);
Expand Down
4 changes: 2 additions & 2 deletions app/react/Layout/specs/CurrentLocationLink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CurrentLocationLink } from '../CurrentLocationLink';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({ pathname: 'pathanem', query: { param: 'value', param2: 'value2' } }),
useLocation: () => ({ pathname: 'pathanem', search: 'param=value&param2=value2' }),
}));

describe('Link', () => {
Expand Down Expand Up @@ -40,6 +40,6 @@ describe('Link', () => {
props.queryParams = { param2: 'new value', test: 'test' };
render();
expect(component).toMatchSnapshot();
expect(component.props().to).toEqual('pathanem?param=value&param2=new%20value&test=test');
expect(component.props().to).toEqual('pathanem?param=value&param2=new+value&test=test');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Link should overwrite url query with query params passed (without mutat
<Link
className="className"
prop="value"
to="pathanem?param=value&param2=new%20value&test=test"
to="pathanem?param=value&param2=new+value&test=test"
>
text
</Link>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.161.0-rc1",
"version": "1.161.0-rc2",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit a43e3c5

Please sign in to comment.