Skip to content

Commit

Permalink
Merge pull request #784 from DTS-STN/typescript-conversion
Browse files Browse the repository at this point in the history
Convert layout to typescript
  • Loading branch information
Charles-Pham authored Jan 3, 2025
2 parents e515e95 + b2ae672 commit a05ad69
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 106 deletions.
8 changes: 4 additions & 4 deletions components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ interface Links {
id: string
text: string
href: string
onClick: () => void
onClick?: () => void
}

interface FooterProps {
id: string
lang: 'en' | 'fr' | 'und'
btnLink: string
error: boolean
error?: boolean
contactLink: string
brandLinks: Links[]
target: string
target?: string
}

const Footer = ({
id = 'mscaPlaceholder',
lang = 'en',
btnLink,
error,
error = false,
contactLink = 'https://www.canada.ca/en/contact.html',
brandLinks = [
{
Expand Down
8 changes: 4 additions & 4 deletions components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface SearchProps {
}

interface MenuProps {
onSignOut: () => void
onSignOut?: () => void
menuList: MenuList[]
}

Expand All @@ -21,8 +21,8 @@ interface MenuList {
key: string
value: string
path: string
showIcon: boolean
onSignOut: () => void
showIcon?: boolean
onSignOut?: () => void
}

interface TopNavProps {
Expand All @@ -32,7 +32,7 @@ interface TopNavProps {
displayAlternateLink: boolean
}

interface BreadcrumbItemProps {
export interface BreadcrumbItemProps {
text: string
link: string
}
Expand Down
138 changes: 43 additions & 95 deletions components/Layout.js → components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,40 @@ import throttle from 'lodash.throttle'
import { signOut } from 'next-auth/react'
import getConfig from 'next/config'
import { useRouter } from 'next/router'
import PropTypes from 'prop-types'
import { useCallback, useEffect, useMemo, useState } from 'react'
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 from './Header'
import Header, { BreadcrumbItemProps } from './Header'
import IdleTimeout from './IdleTimeout'
import MetaData from './MetaData'
import MetaData, { Data } from './MetaData'

export default function Layout(
props = {
locale: 'en',
meta: '',
langToggleLink: '',
breadCrumbItems: [],
bannerContent: {
bannerBoldText: '',
bannerText: '',
bannerLink: '',
bannerLinkHref: '',
bannerSummaryTitle: '',
bannerSummaryContent: '',
bannerButtonText: '',
bannerButtonLink: '',
icon: '',
},
popupContentNA,
content,
popupContent,
display: { hideBanner: true },
popupStaySignedIn,
refPageAA,
dataGcAnalyticsCustomClickMenuVariable,
title: 'Service.Canada.ca',
},
) {
const t = props.locale === 'en' ? en : fr
const [response, setResponse] = useState()
interface LayoutProps {
locale?: 'en' | 'fr' | 'und'
meta: Data
langToggleLink?: string
breadCrumbItems?: BreadcrumbItemProps[]
display?: { hideBanner: boolean }
refPageAA: string
dataGcAnalyticsCustomClickMenuVariable: string
title: 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 defaultBreadcrumbs = []
const contactLink =
props.locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'
const contactLink = locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'

const validationResponse = useCallback(
async () => setResponse(await fetch('/api/refresh-msca')),
Expand All @@ -68,7 +59,7 @@ export default function Layout(
//If validateSession call indicates an invalid MSCA session, end next-auth session and redirect to login
if (response?.status === 401) {
signOut()
router.push(`/${props.locale}/auth/login`)
router.push(`/${locale}/auth/login`)
}
//Remove event on unmount to prevent a memory leak with the cleanup
return () => {
Expand All @@ -83,7 +74,7 @@ export default function Layout(
throttledVisiblityChangeEvent,
response,
router,
props.locale,
locale,
])

return (
Expand All @@ -94,54 +85,45 @@ export default function Layout(
--noto-sans-font: ${notoSans.style.fontFamily};
}
`}</style>
<MetaData language={props.locale} data={props.meta}></MetaData>
<MetaData language={locale} data={meta}></MetaData>
<Header
legacyBehavior
dataTestId="topnav"
id="header"
linkPath={props.langToggleLink}
lang={props.locale}
breadCrumbItems={
props.breadCrumbItems ? props.breadCrumbItems : defaultBreadcrumbs
}
refPageAA={props.refPageAA}
linkPath={langToggleLink}
lang={locale}
breadCrumbItems={breadCrumbItems}
refPageAA={refPageAA}
topnavProps={{
skipToMainPath: '#mainContent',
skipToAboutPath: '#page-footer',
switchToBasicPath: '',
displayAlternateLink: false,
}}
dataGcAnalyticsCustomClickInstitutionVariable={
props.children.props.aaPrefix
}
dataGcAnalyticsCustomClickInstitutionVariable={children.props.aaPrefix}
dataGcAnalyticsCustomClickMenuVariable={
props.dataGcAnalyticsCustomClickMenuVariable
dataGcAnalyticsCustomClickMenuVariable
}
menuProps={{
legacyBehavior: true,
menuList: [
{
key: 'dashKey',
id: 'my-dashboard',
value: t.menuItems.dashboard,
path: `${
props.locale === 'en'
? '/en/my-dashboard'
: '/fr/mon-tableau-de-bord'
locale === 'en' ? '/en/my-dashboard' : '/fr/mon-tableau-de-bord'
}`,
},
{
key: 'profileKey',
id: 'profile',
value: t.menuItems.profile,
path: `${props.locale === 'en' ? '/en/profile' : '/fr/profil'}`,
path: `${locale === 'en' ? '/en/profile' : '/fr/profil'}`,
},
{
key: 'securityKey',
id: 'security',
value: t.menuItems.security,
path: `${
props.locale === 'en'
locale === 'en'
? '/en/security-settings'
: '/fr/parametres-securite'
}`,
Expand All @@ -151,7 +133,7 @@ export default function Layout(
id: 'contact',
value: t.menuItems.contactUs,
path: `${
props.locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'
locale === 'en' ? '/en/contact-us' : '/fr/contactez-nous'
}`,
},
{
Expand All @@ -169,11 +151,11 @@ export default function Layout(
}}
/>
<main id="mainContent" className="sch-container grid gap-[30px]">
{props.children}
{children}
</main>
<IdleTimeout locale={props.locale} refPageAA={props.refPageAA} />
<IdleTimeout locale={locale} refPageAA={refPageAA} />
<Footer
lang={!props.locale ? 'en' : props.locale}
lang={locale}
brandLinks={[
{
href:
Expand All @@ -199,37 +181,3 @@ export default function Layout(
</>
)
}

Layout.propTypes = {
/*
* Locale current language
*/
locale: PropTypes.string,
/*
* Meta Tags
*/
meta: PropTypes.object,
/*
* Title of the page
*/
title: PropTypes.string,
/*
* Link of the page in opposite language
*/
langToggleLink: PropTypes.string,
display: PropTypes.shape({
/*
* Toggle use of Phase (default false)
*/
hideBanner: PropTypes.bool,
/*
* Toggle use of DS header (default false)
*/
hideHeader: PropTypes.bool,
/*
* Toggle use of DS footer (default false)
*/
hideFooter: PropTypes.bool,
}),
breadCrumbItems: PropTypes.array,
}
4 changes: 2 additions & 2 deletions components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ interface MenuItem {
key: string
value: string
path: string
showIcon: boolean
onSignOut: () => void
showIcon?: boolean
onSignOut?: () => void
}

interface MenuProps {
Expand Down
2 changes: 1 addition & 1 deletion components/MetaData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Content {
service: string
}

interface Data {
export interface Data {
data_en: Content
data_fr: Content
}
Expand Down

0 comments on commit a05ad69

Please sign in to comment.