Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5105; For the Sitemap component, don't hardcode root as navigation action path #5106

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/5106.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For folders inside navigation roots, properly fetch navigation from the
navroot, rather then the site root @tiberiuichim
37 changes: 24 additions & 13 deletions src/components/theme/Sitemap/Sitemap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const messages = defineMessages({
defaultMessage: 'Sitemap',
},
});

export function getSitemapPath(pathname = '', lang) {
const prefix = pathname.replace(/\/sitemap$/gm, '').replace(/^\//, '');
const path = prefix || lang || '';
return path;
}

/**
* Sitemap class.
* @class Sitemap
Expand All @@ -39,11 +46,13 @@ class Sitemap extends Component {

componentDidMount() {
const { settings } = config;
if (settings.isMultilingual) {
this.props.getNavigation(`${toBackendLang(this.props.lang)}`, 4);
} else {
this.props.getNavigation('', 4);
}

const lang = settings.isMultilingual
? `${toBackendLang(this.props.lang)}`
: null;

const path = getSitemapPath(this.props.location.pathname, lang);
this.props.getNavigation(path, 4);
}

/**
Expand Down Expand Up @@ -105,15 +114,17 @@ export default compose(
{
key: 'navigation',
promise: ({ location, store: { dispatch, getState } }) => {
if (!__SERVER__) return;
const { settings } = config;
const lang = getState().intl.locale;
if (settings.isMultilingual) {
return (
__SERVER__ && dispatch(getNavigation(`${toBackendLang(lang)}`, 4))
);
} else {
return __SERVER__ && dispatch(getNavigation('', 4));
}

const path = getSitemapPath(
location.pathname,
settings.isMultilingual
? toBackendLang(getState().intl.locale)
: null,
);

return dispatch(getNavigation(path, 4));
},
},
]),
Expand Down
25 changes: 23 additions & 2 deletions src/components/theme/Sitemap/Sitemap.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import configureStore from 'redux-mock-store';
import { Provider } from 'react-intl-redux';
import { MemoryRouter } from 'react-router-dom';

import { __test__ as Sitemap } from './Sitemap';
import { __test__ as Sitemap, getSitemapPath } from './Sitemap';

const mockStore = configureStore();

Expand Down Expand Up @@ -46,11 +46,32 @@ describe('Sitemap', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<Sitemap />
<Sitemap location={{ pathname: '/page-1' }} />
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});
});

describe('getSitemapPath', () => {
it('accepts empty path', () => {
expect(getSitemapPath('', null)).toBe('');
});

it('accepts a path', () => {
expect(getSitemapPath('/page-1/sitemap', null)).toBe('page-1');
});
it('accepts a path', () => {
expect(getSitemapPath('/page-1/sitemap', null)).toBe('page-1');
});

it('uses a language as default root', () => {
expect(getSitemapPath('/', 'de')).toBe('de');
});

it('accepts language-rooted paths', () => {
expect(getSitemapPath('/de/mission', 'de')).toBe('de/mission');
});
});