-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c96151
commit 5dc724d
Showing
9 changed files
with
48 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
'use client'; | ||
|
||
import { Dictionary, getDictionary } from '~/i18n'; | ||
import useI18n from '@/hooks/useI18n'; | ||
import { H1 } from '../Heading'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
function NotFound() { | ||
const { dict } = useI18n(); | ||
const { lang } = useI18n(); | ||
const [dictionary, setDictionary] = useState<Dictionary | null>(null); | ||
|
||
return <h1>{dict.notFound.message}</h1>; | ||
useEffect(() => { | ||
getDictionary(lang).then(setDictionary); | ||
}, [lang]); | ||
|
||
if (!dictionary) return null; | ||
|
||
return ( | ||
<div className="flex h-screen items-center justify-center"> | ||
<H1>{dictionary.notFound.message}</H1> | ||
</div> | ||
); | ||
} | ||
|
||
export default NotFound; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import zhTW from '~/i18n/locales/zh-TW'; | ||
import en from '~/i18n/locales/en'; | ||
import NotFound from '.'; | ||
|
||
const mockPathname = jest.fn(); | ||
|
||
jest.mock('next/navigation', () => ({ | ||
usePathname: () => mockPathname(), | ||
})); | ||
|
||
describe('NotFound component', () => { | ||
it('should render correct element', () => { | ||
it('should render correct element', async () => { | ||
mockPathname.mockReturnValueOnce('/en/test/path'); | ||
|
||
render(<NotFound />); | ||
|
||
const heading = screen.getByRole('heading'); | ||
const heading = await screen.findByRole('heading'); | ||
|
||
expect(heading).toBeInTheDocument(); | ||
expect(heading).toHaveTextContent(zhTW.notFound.message) | ||
expect(heading).toHaveTextContent(en.notFound.message) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import { usePathname } from 'next/navigation'; | ||
import { defaultLocale } from '~/i18n'; | ||
import zhTW from '~/i18n/locales/zh-TW'; | ||
import en from '~/i18n/locales/en'; | ||
import getLocale from '@/utils/getLocale'; | ||
|
||
const dictionaries = { | ||
'zh-TW': zhTW, | ||
en, | ||
}; | ||
|
||
/** | ||
* This hook retrieves the language setting based on the current pathname. | ||
*/ | ||
function useI18n() { | ||
const pathname = usePathname(); | ||
const lang = getLocale(pathname, defaultLocale); | ||
const dict = dictionaries[lang]; | ||
|
||
return { lang, dict }; | ||
return { lang }; | ||
} | ||
|
||
export default useI18n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters