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

DOP-4060 adds language selector to unified footer on docs' pages. #928

Merged
merged 13 commits into from
Oct 3, 2023
Merged
37 changes: 34 additions & 3 deletions src/components/DocumentBody.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { UnifiedFooter } from '@mdb/consistent-nav';
import { usePresentationMode } from '../hooks/use-presentation-mode';
Expand All @@ -9,6 +9,7 @@ import { getMetaFromDirective } from '../utils/get-meta-from-directive';
import { getPlaintext } from '../utils/get-plaintext';
import { getTemplate } from '../utils/get-template';
import useSnootyMetadata from '../utils/use-snooty-metadata';
import { isBrowser } from '../utils/is-browser';
import Widgets from './Widgets';
import SEO from './SEO';
import FootnoteContext from './Footnote/footnote-context';
Expand Down Expand Up @@ -68,10 +69,29 @@ const getAnonymousFootnoteReferences = (index, numAnonRefs) => {
};

const DocumentBody = (props) => {
const HIDE_UNIFIED_FOOTER_LOCALE = process.env['GATSBY_HIDE_UNIFIED_FOOTER_LOCALE'] === 'true';

caesarbell marked this conversation as resolved.
Show resolved Hide resolved
const {
location,
pageContext: { page, slug, template },
} = props;

useEffect(() => {
// A workaround to remove the other locale options.
if (!HIDE_UNIFIED_FOOTER_LOCALE) {
const footer = document.getElementById('footer-container');
const footerUlElement = footer?.querySelector('ul');
if (footerUlElement) {
caesarbell marked this conversation as resolved.
Show resolved Hide resolved
// For DOP-4060 we only want to support English and Simple Chinese (for now)
const en = footerUlElement.firstChild;
caesarbell marked this conversation as resolved.
Show resolved Hide resolved
const simpleChinese = footerUlElement.lastChild;
seungpark marked this conversation as resolved.
Show resolved Hide resolved
footerUlElement.innerHTML = null;
footerUlElement.appendChild(en);
footerUlElement.appendChild(simpleChinese);
}
}
}, [HIDE_UNIFIED_FOOTER_LOCALE]);

const initialization = () => {
const pageNodes = getNestedValue(['children'], page) || [];
const footnotes = getFootnotes(pageNodes);
Expand All @@ -90,6 +110,17 @@ const DocumentBody = (props) => {

const isInPresentationMode = usePresentationMode()?.toLocaleLowerCase() === 'true';

const onSelectLocale = (locale) => {
const localeHrefMap = {
'zh-cn': 'https://mongodbcom-cdn.website.staging.corp.mongodb.com/zh-cn/docs-qa/',
'en-us': 'https://mongodbcom-cdn.website.staging.corp.mongodb.com/docs-qa/,',
seungpark marked this conversation as resolved.
Show resolved Hide resolved
};
caesarbell marked this conversation as resolved.
Show resolved Hide resolved

if (isBrowser) {
window.location.href = localeHrefMap[locale];
}
};

return (
<>
<Widgets
Expand All @@ -109,8 +140,8 @@ const DocumentBody = (props) => {
</FootnoteContext.Provider>
</Widgets>
{!isInPresentationMode && (
<div data-testid="consistent-footer">
<UnifiedFooter hideLocale={true} />
<div data-testid="consistent-footer" id="footer-container">
<UnifiedFooter hideLocale={HIDE_UNIFIED_FOOTER_LOCALE} onSelectLocale={onSelectLocale} />
</div>
caesarbell marked this conversation as resolved.
Show resolved Hide resolved
)}
</>
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Presentation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jest.mock(`../../src/utils/use-snooty-metadata`, () => {
});

describe('DocumentBody', () => {
beforeAll(() => {
jest.spyOn(document, 'querySelector');
});
it('renders the necessary elements', () => {
mockLocation(null);
render(<DocumentBody location={window.location} pageContext={mockPageContext} />);
Expand All @@ -18,6 +21,12 @@ describe('DocumentBody', () => {
expect(footer).toBeVisible();
expect(footer).toMatchSnapshot();

if (!process.env.GATSBY_HIDE_UNIFIED_FOOTER_LOCALE) {
const languageSelector = screen.getByTestId('options');
expect(languageSelector).toBeInTheDocument();
expect(languageSelector.querySelectorAll('li')).toHaveLength(2);
}

const feedbackWidget = screen.getByText('Share Feedback');
expect(feedbackWidget).toBeVisible();
expect(feedbackWidget).toMatchSnapshot();
Expand Down
Loading
Loading