Skip to content

Commit 2b65359

Browse files
committed
Persist mapper query param in docs.
1 parent e90488e commit 2b65359

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

packages/react-renderer-demo/src/components/common/connected-links.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ChevronLeft from '@material-ui/icons/ChevronLeft';
77
import Link from 'next/link';
88

99
import MenuContext from '../navigation/menu-context';
10+
import useMapperLink from '../../hooks/use-mapper-link';
1011

1112
const useStyles = makeStyles(() => ({
1213
linksContainer: {
@@ -23,12 +24,14 @@ const useStyles = makeStyles(() => ({
2324
const ConnectedLinks = () => {
2425
const { prev, next } = useContext(MenuContext);
2526
const classNames = useStyles();
27+
const prevLink = `/${useMapperLink(prev && prev.link)}`;
28+
const nextLink = `/${useMapperLink(next && next.link)}`;
2629
return (
2730
<Grid container justify="space-between" className={classNames.linksContainer}>
2831
<Grid item>
2932
{prev && prev.link && (
30-
<Link href={`/${prev.link}`}>
31-
<a className={classNames.link} href={`/${prev.link}`}>
33+
<Link href={prevLink}>
34+
<a className={classNames.link} href={prevLink}>
3235
<Button>
3336
<ChevronLeft />
3437
{prev.label}
@@ -39,8 +42,8 @@ const ConnectedLinks = () => {
3942
</Grid>
4043
<Grid item>
4144
{next && next.link && (
42-
<Link href={`/${next.link}`}>
43-
<a className={classNames.link} href={`/${next.link}`}>
45+
<Link href={nextLink}>
46+
<a className={classNames.link} href={nextLink}>
4447
<Button>
4548
{next.label}
4649
<ChevronRight />

packages/react-renderer-demo/src/components/navigation/mapper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { useRouter } from 'next/router';
1515

1616
import { navStyles } from './nav-styles';
1717
import { query } from './find-connected-links';
18+
import useMapperLink from '../../hooks/use-mapper-link';
1819

1920
const useStyles = makeStyles(navStyles);
2021

2122
const Item = ({ href, linkText, component }) => {
2223
const classes = useStyles();
2324
const router = useRouter();
25+
const link = useMapperLink(href.replace('/?', '?'));
2426

2527
return (
2628
<ListItem
@@ -29,8 +31,8 @@ const Item = ({ href, linkText, component }) => {
2931
key={href || linkText}
3032
className={classes.nested}
3133
component={forwardRef((props, ref) => (
32-
<RouterNavLink ref={ref} key={component} href={href.replace('/?', '?')}>
33-
<Link style={{ color: 'rgba(0, 0, 0, 0.87)' }} {...props} href={href.replace('/?', '?')} />
34+
<RouterNavLink ref={ref} key={component} href={link}>
35+
<Link style={{ color: 'rgba(0, 0, 0, 0.87)' }} {...props} href={link} />
3436
</RouterNavLink>
3537
))}
3638
>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import useQueryParam from './use-query-param';
2+
3+
/**
4+
* Appends current query mapper value to URL
5+
* @param {string} link url string to be appended with current mapper query param
6+
*/
7+
const useMapperLink = (link = '') => {
8+
const mapperQuery = useQueryParam('mapper');
9+
return link.includes('component-example/') ? `${link}${mapperQuery}` : link;
10+
};
11+
12+
export default useMapperLink;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useRouter } from 'next/router';
2+
/**
3+
* Returns a value of given query param from URL
4+
* @param {string} param name of the query param value to be extracted from URL
5+
*/
6+
const useQueryParam = (param) => {
7+
const { asPath } = useRouter();
8+
if (!param) {
9+
return '';
10+
}
11+
12+
const queryParam = new URLSearchParams(`?${asPath.split('?').pop()}`).get(param);
13+
if (!queryParam) {
14+
return '';
15+
}
16+
17+
return `?${param}=${queryParam}`;
18+
};
19+
20+
export default useQueryParam;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6335,6 +6335,11 @@ clsx@^1.0.2, clsx@^1.0.4:
63356335
version "1.0.4"
63366336
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.0.4.tgz#0c0171f6d5cb2fe83848463c15fcc26b4df8c2ec"
63376337

6338+
clsx@^1.1.0:
6339+
version "1.1.0"
6340+
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.1.0.tgz#62937c6adfea771247c34b54d320fb99624f5702"
6341+
integrity sha512-3avwM37fSK5oP6M5rQ9CNe99lwxhXDOeSWVPAOYF6OazUTgZCMb0yWlJpmdD74REy1gkEaFiub2ULv4fq9GUhA==
6342+
63386343
cmd-shim@^3.0.0, cmd-shim@^3.0.3:
63396344
version "3.0.3"
63406345
resolved "https://registry.yarnpkg.com/cmd-shim/-/cmd-shim-3.0.3.tgz#2c35238d3df37d98ecdd7d5f6b8dc6b21cadc7cb"

0 commit comments

Comments
 (0)