Skip to content

Commit cb36adb

Browse files
authored
fix: search conditional rendering (freeCodeCamp#57200)
1 parent 7f830a1 commit cb36adb

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

client/src/components/Header/components/universal-nav.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type UniversalNavProps = Omit<
2323
> & {
2424
fetchState: { pending: boolean };
2525
searchBarRef?: React.RefObject<HTMLDivElement>;
26+
pathname: string;
2627
};
2728
const UniversalNav = ({
2829
displayMenu,
@@ -31,21 +32,20 @@ const UniversalNav = ({
3132
menuButtonRef,
3233
searchBarRef,
3334
user,
34-
fetchState
35+
fetchState,
36+
pathname
3537
}: UniversalNavProps): JSX.Element => {
3638
const { pending } = fetchState;
3739
const { t } = useTranslation();
3840
const isSearchExposedWidth = useMediaQuery({
3941
query: `(min-width: ${SEARCH_EXPOSED_WIDTH}px)`
4042
});
4143

42-
const search =
43-
typeof window !== `undefined` && isLanding(window.location.pathname) ? (
44-
<SearchBarOptimized innerRef={searchBarRef} />
45-
) : (
46-
<SearchBar innerRef={searchBarRef} />
47-
);
48-
44+
const search = isLanding(pathname) ? (
45+
<SearchBarOptimized innerRef={searchBarRef} />
46+
) : (
47+
<SearchBar innerRef={searchBarRef} />
48+
);
4949
return (
5050
<nav
5151
aria-label={t('aria.primary-nav')}

client/src/components/Header/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Props = PropsFromRedux & {
2828
fetchState: { pending: boolean };
2929
user: User;
3030
skipButtonText: string;
31+
pathname: string;
3132
};
3233

3334
class Header extends React.Component<Props, { displayMenu: boolean }> {
@@ -79,7 +80,8 @@ class Header extends React.Component<Props, { displayMenu: boolean }> {
7980

8081
render(): JSX.Element {
8182
const { displayMenu } = this.state;
82-
const { examInProgress, fetchState, user, skipButtonText } = this.props;
83+
const { examInProgress, fetchState, user, skipButtonText, pathname } =
84+
this.props;
8385
return (
8486
<header className='site-header'>
8587
<a
@@ -96,6 +98,7 @@ class Header extends React.Component<Props, { displayMenu: boolean }> {
9698
displayMenu={displayMenu}
9799
fetchState={fetchState}
98100
hideMenu={this.hideMenu}
101+
pathname={pathname}
99102
menuButtonRef={this.menuButtonRef}
100103
searchBarRef={this.searchBarRef}
101104
showMenu={this.showMenu}

client/src/components/layouts/default.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function DefaultLayout({
136136
superBlock,
137137
theme,
138138
user,
139+
pathname,
139140
fetchUser
140141
}: DefaultLayoutProps): JSX.Element {
141142
const { t } = useTranslation();
@@ -269,6 +270,7 @@ function DefaultLayout({
269270
<Header
270271
fetchState={fetchState}
271272
user={user}
273+
pathname={pathname}
272274
skipButtonText={t('learn.skip-to-content')}
273275
/>
274276
<OfflineWarning

e2e/search-bar-optimized.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,20 @@ test.describe('Search bar optimized', () => {
9292

9393
await expect(searchInput).toHaveValue('');
9494
});
95+
96+
test('The optimized searchbar component should not render when not on the landing page', async ({
97+
page,
98+
isMobile
99+
}) => {
100+
// This means that the default search bar should be rendered ^.
101+
await page.getByTestId('curriculum-map-button').nth(0).click();
102+
103+
if (isMobile) {
104+
const menuButton = page.getByTestId('header-menu-button');
105+
await expect(menuButton).toBeVisible();
106+
await menuButton.click();
107+
}
108+
109+
await expect(page.getByTestId('header-search')).toBeVisible();
110+
});
95111
});

0 commit comments

Comments
 (0)