Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import throttle from 'lodash.throttle'
import getConfig from 'next/config'
import { useRouter } from 'next/router'
import { ReactElement, useCallback, useEffect, useMemo, useState } from 'react'
import en from '../locales/en'
import fr from '../locales/fr'
import { lato, notoSans } from '../utils/fonts'
import Footer from './Footer'
import Header, { BreadcrumbItemProps } from './Header'
import IdleTimeout from './IdleTimeout'
import MetaData, { Data } from './MetaData'
import { signOut } from 'next-auth/react'
interface LayoutProps {
locale?: 'en' | 'fr' | 'und'
meta: Data
langToggleLink?: string
breadCrumbItems?: BreadcrumbItemProps[]
display?: { hideBanner: boolean }
refPageAA: string
dataGcAnalyticsCustomClickMenuVariable: string
children: ReactElement
}
export default function Layout({
locale = 'en',
langToggleLink = '',
breadCrumbItems = [],
meta,
refPageAA,
children,
dataGcAnalyticsCustomClickMenuVariable,
}: LayoutProps) {
const t = locale === 'en' ? en : fr
const [response, setResponse] = useState<Response>()
const router = useRouter()
const contactLink = locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'
const validationResponse = useCallback(
async () => setResponse(await fetch('/api/refresh-msca')),
[],
)
//Event listener for click events that revalidates MSCA session, throttled using lodash to only trigger every 1 minute
const throttledOnClickEvent = useMemo(
() => throttle(validationResponse, 60000, { trailing: false }),
[validationResponse],
)
//Event listener for visibility change events that revalidates MSCA session, throttled using lodash to only trigger every 15 seconds
const throttledVisiblityChangeEvent = useMemo(
() => throttle(validationResponse, 15000, { trailing: false }),
[validationResponse],
)
//If session is valid, add event listeners to check for valid sessions on click and visibility change events
useEffect(() => {
window.addEventListener('visibilitychange', throttledVisiblityChangeEvent)
window.addEventListener('click', throttledOnClickEvent)
//If validateSession call indicates an invalid MSCA session, end next-auth session and redirect to login
Iif (response?.status === 401) {
signOut()
router.push(`/${locale}/auth/login`)
}
//Remove event on unmount to prevent a memory leak with the cleanup
return () => {
window.removeEventListener('click', throttledOnClickEvent)
window.removeEventListener(
'visiblitychange',
throttledVisiblityChangeEvent,
)
}
}, [
throttledOnClickEvent,
throttledVisiblityChangeEvent,
response,
router,
locale,
])
return (
<>
<style jsx global>{`
:root {
--lato-font: ${lato.style.fontFamily};
--noto-sans-font: ${notoSans.style.fontFamily};
}
`}</style>
<MetaData language={locale} data={meta}></MetaData>
<Header
id="header"
linkPath={langToggleLink}
lang={locale}
breadCrumbItems={breadCrumbItems}
refPageAA={refPageAA}
topnavProps={{
skipToMainPath: '#mainContent',
skipToAboutPath: '#page-footer',
switchToBasicPath: '',
displayAlternateLink: false,
}}
dataGcAnalyticsCustomClickInstitutionVariable={children.props.aaPrefix}
dataGcAnalyticsCustomClickMenuVariable={
dataGcAnalyticsCustomClickMenuVariable
}
menuProps={{
menuList: [
{
key: 'dashKey',
id: 'my-dashboard',
value: t.menuItems.dashboard,
path: `${
locale === 'en' ? '/en/my-dashboard' : '/fr/mon-tableau-de-bord'
}`,
},
{
key: 'profileKey',
id: 'profile',
value: t.menuItems.profile,
path: `${locale === 'en' ? '/en/profile-and-preferences' : '/fr/profil-et-preferences'}`,
},
{
key: 'securityKey',
id: 'security',
value: t.menuItems.security,
path: `${
locale === 'en'
? '/en/security-settings'
: '/fr/parametres-securite'
}`,
},
{
key: 'contactKey',
id: 'contact',
value: t.menuItems.contactUs,
path: `${
locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'
}`,
},
{
key: 'signOutKey',
id: 'signOut',
value: t.menuItems.signOut,
path: '/auth/logout',
showIcon: true,
},
],
inboxLink: `${locale === 'en' ? '/en/inbox' : '/fr/boite-reception'}`,
}}
searchProps={{
onChange: function noRefCheck() {},
onSubmit: function noRefCheck() {},
}}
/>
<main id="mainContent" className="sch-container grid gap-[30px]">
{children}
</main>
<IdleTimeout locale={locale} refPageAA={refPageAA} />
<Footer
lang={locale}
brandLinks={[
{
href:
getConfig()?.publicRuntimeConfig.ENVIRONMENT === 'production'
? t.footerTermsAndConditionURL
: t.footerTermsAndConditionDevURL,
id: 'linkTC',
text: t.footerTermsAndCondition,
},
{
href:
getConfig()?.publicRuntimeConfig.ENVIRONMENT === 'production'
? t.footerPrivacyURL
: t.footerPrivacyDevURL,
id: 'linkPR',
text: t.footerPrivacy,
},
]}
contactLink={contactLink}
btnLink="#top"
id="page-footer"
/>
</>
)
}
|