Skip to content

Commit

Permalink
Merge pull request #868 from DTS-STN/hotfix-131761-breadcrumbs
Browse files Browse the repository at this point in the history
wrapped appending in useEffect, use useState for array decalration and state
  • Loading branch information
Maxim-Lam authored Sep 26, 2023
2 parents 6935878 + 703ad9a commit 3973c96
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ContextualAlert as Message,
} from '@dts-stn/service-canada-design-system'
import { useRouter } from 'next/router'
import React from 'react'
import React, { useEffect, useState } from 'react'
import { WebTranslations } from '../../i18n/web'
import { useTranslation } from '../Hooks'
import { Header } from './Header'
Expand Down Expand Up @@ -53,7 +53,7 @@ export const Layout: React.VFC<{
onSubmit: () => {},
}

const breadcrumbs =
const breadcrumbsArr =
process.env.APP_ENV === 'production'
? [
{
Expand Down Expand Up @@ -84,24 +84,46 @@ export const Layout: React.VFC<{
},
]

if (router.pathname === '/questions') {
breadcrumbs.push({
text: tsln.breadcrumb6Title,
link: tsln.breadcrumb6URL,
})
} else if (
router.pathname === '/results' ||
router.pathname === '/resultats'
) {
breadcrumbs.push({
text: tsln.breadcrumb6Title,
link: tsln.breadcrumb6URL,
}),
breadcrumbs.push({
text: tsln.breadcrumb7Title,
link: tsln.breadcrumb7URL,
})
}
const [breadcrumbs, setBreadcrumbsArr] = useState(breadcrumbsArr)
const [shouldAddItem, setShouldAddItem] = useState<boolean>(true)

useEffect(() => {
if (router.pathname === '/questions' && shouldAddItem) {
setBreadcrumbsArr((prevFriends) => [
...prevFriends,
{
text: tsln.breadcrumb6Title,
link: tsln.breadcrumb6URL,
},
])
setShouldAddItem(false)
} else if (
(router.pathname === '/results' || router.pathname === '/resultats') &&
shouldAddItem
) {
setBreadcrumbsArr((prevFriends) => [
...prevFriends,
{
text: tsln.breadcrumb6Title,
link: tsln.breadcrumb6URL,
},
{
text: tsln.breadcrumb7Title,
link: tsln.breadcrumb7URL,
},
])
setShouldAddItem(false)
}
}, [
breadcrumbs,
router.pathname,
shouldAddItem,
tsln.breadcrumb6Title,
tsln.breadcrumb6URL,
tsln.breadcrumb7Title,
tsln.breadcrumb7URL,
])

const handleOnClick = () => {
const link = `https://retraite-retirement.service.canada.ca/${router.locale}/home`
router.push(link)
Expand Down

0 comments on commit 3973c96

Please sign in to comment.