Skip to content

PROD-3881 universal nav #6798

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

Merged
merged 1 commit into from
Jan 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`Matches shallow shapshot 1`] = `
<div
id="uninav-footerNav"
id="uninav-footerNav-0"
/>
`;
41 changes: 28 additions & 13 deletions src/shared/components/TopcoderFooter/index.jsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
/* global tcUniNav */
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useMemo, useRef } from 'react';
import { getSubPageConfiguration } from '../../utils/url';

const footerElId = 'uninav-footerNav';
let counter = 0;
const footerElIdTmpl = 'uninav-footerNav';

export default function TopcoderFooter() {
const footerRef = useRef();
const footerInitialized = useRef(false);
const footerElId = useRef(`${footerElIdTmpl}-${counter}`);

useEffect(() => {
if (footerInitialized.current) {
return;
const navType = useMemo(() => {
if (typeof window === 'undefined') {
return '';
}

footerInitialized.current = true;
let { type } = getSubPageConfiguration();

let { fullFooter } = getSubPageConfiguration();
const sessionNavType = sessionStorage.getItem('uni-nav[navType]');
if (sessionNavType && (sessionNavType === 'tool' || sessionNavType === 'marketing')) {
type = sessionNavType;
}

// If url contains navTool url parameter. Overwrite settings with parameter.
const url = new URL(window.location.href);
const urlParams = new URLSearchParams(url.search);
const navToolParam = urlParams.get('navTool');
if (navToolParam) {
fullFooter = navToolParam !== 'tool';
if (urlParams.get('navTool')) {
type = urlParams.get('navTool');
}

return type;
}, []);

useEffect(() => {
if (footerInitialized.current) {
return;
}

tcUniNav('init', footerElId, {
fullFooter,
footerInitialized.current = true;
counter += 1;

tcUniNav('init', footerElId.current, {
fullFooter: navType !== 'tool',
type: 'footer',
});
}, []);

return <div id={footerElId} ref={footerRef} />;
return <div id={footerElId.current} ref={footerRef} />;
}
51 changes: 35 additions & 16 deletions src/shared/containers/TopcoderHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* global tcUniNav */
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useMemo, useRef } from 'react';
import PT from 'prop-types';
import { connect } from 'react-redux';
import { config } from 'topcoder-react-utils';
import _ from 'lodash';
import { getInitials, getSubPageConfiguration } from '../utils/url';

const headerElId = 'uninav-headerNav';
let counter = 0;
const headerElIdTmpl = 'uninav-headerNav';

const TopcoderHeader = ({ auth }) => {
const uniNavInitialized = useRef(false);
Expand All @@ -15,6 +16,32 @@ const TopcoderHeader = ({ auth }) => {
const isAuthenticated = !!authToken;
const authURLs = config.HEADER_AUTH_URLS;
const headerRef = useRef();
const headerElId = useRef(`${headerElIdTmpl}-${counter}`);

const navType = useMemo(() => {
if (typeof window === 'undefined') {
return '';
}

let { type } = getSubPageConfiguration();

// if there's a stored nav type in session storage, retrieve it and overwrite type
const sessionNavType = sessionStorage.getItem('uni-nav[navType]');
if (sessionNavType && (sessionNavType === 'tool' || sessionNavType === 'marketing')) {
type = sessionNavType;
}

// If url contains navTool url parameter. Overwrite settings with parameter.
const url = new URL(window.location.href);
const urlParams = new URLSearchParams(url.search);
if (urlParams.get('navTool')) {
type = urlParams.get('navTool');
}

// store nav type for current session
sessionStorage.setItem('uni-nav[navType]', type);
return type;
}, []);

const navigationUserInfo = {
...user,
Expand All @@ -27,21 +54,13 @@ const TopcoderHeader = ({ auth }) => {
}

uniNavInitialized.current = true;
counter += 1;

const regSource = window.location.pathname.split('/')[1];
const retUrl = encodeURIComponent(window.location.href);

let { type } = getSubPageConfiguration();

// If url contains navTool url parameter. Overwrite settings with parameter.
const url = new URL(window.location.href);
const urlParams = new URLSearchParams(url.search);
if (urlParams.get('navTool')) {
type = urlParams.get('navTool');
}

tcUniNav('init', headerElId, {
type,
tcUniNav('init', headerElId.current, {
type: navType,
toolName: getSubPageConfiguration().toolName,
toolRoot: getSubPageConfiguration().toolRoot,
signOut: () => {
Expand All @@ -54,15 +73,15 @@ const TopcoderHeader = ({ auth }) => {
window.location = `${authURLs.location.replace('%S', retUrl).replace('member?', '#!/member?')}&mode=signUp&regSource=${regSource}`;
},
});
}, []);
}, [navType]);

useEffect(() => {
tcUniNav('update', headerElId, {
tcUniNav('update', headerElId.current, {
user: isAuthenticated ? navigationUserInfo : null,
});
}, [isAuthenticated, navigationUserInfo]);

return <div id={headerElId} ref={headerRef} />;
return <div id={headerElId.current} ref={headerRef} />;
};

TopcoderHeader.defaultProps = {
Expand Down