Skip to content

Commit

Permalink
fix(cowfi): fix pages crashes (#5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Dec 16, 2024
1 parent db89e12 commit 525d079
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Article, getArticles, getCategories } from '../../../../../services/cms'
import { ArticlesPageComponents } from '@/components/ArticlesPageComponents'
import { redirect } from 'next/navigation'

const ITEMS_PER_PAGE = 24

Expand All @@ -28,7 +29,14 @@ export async function generateStaticParams() {

export default async function Page({ params }: Props) {
const pageParam = (await params)?.pageIndex
const page = pageParam && pageParam.length > 0 ? parseInt(pageParam[0], 10) : 1
const paramsAreSet = Boolean(pageParam && pageParam.length > 0)
const pageIndexIsValid = Boolean(pageParam && /^\d+$/.test(pageParam))

if (paramsAreSet && !pageIndexIsValid) {
return redirect('/learn/articles')
}

const page = pageParam && pageIndexIsValid ? parseInt(pageParam, 10) : 1

const articlesResponse = (await getArticles({ page, pageSize: ITEMS_PER_PAGE })) as ArticlesResponse

Expand Down
5 changes: 5 additions & 0 deletions apps/cow-fi/app/(main)/tokens/[tokenId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TokenPageComponent } from '@/components/TokenPageComponent'
import type { Metadata } from 'next'
import type { TokenDetails } from '../../../../types'
import { getPageMetadata } from '@/util/getPageMetadata'
import { redirect } from 'next/navigation'

type Props = {
params: Promise<{ tokenId: string }>
Expand All @@ -35,6 +36,8 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {

const token = await getTokenDetails(tokenId)

if (!token) return {}

return getPageMetadata(getTokenMetaData(token))
}

Expand All @@ -51,5 +54,7 @@ export default async function Page({ params }: Props) {

const token = await getTokenDetails(tokenId)

if (!token) return redirect('/tokens')

return <TokenPageComponent token={token} />
}
4 changes: 2 additions & 2 deletions apps/cow-fi/services/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export async function getTokensInfo(): Promise<TokenInfo[]> {
*
* @returns token details for the given token id
*/
export async function getTokenDetails(coingeckoId: string): Promise<TokenDetails> {
export async function getTokenDetails(coingeckoId: string): Promise<TokenDetails | undefined> {
const id = coingeckoId.toLowerCase()
const tokensRaw = await _getAllTokensData()
return tokensRaw.find(({ id: _id }) => _id === id) as TokenDetails
return tokensRaw.find(({ id: _id }) => _id === id) as TokenDetails | undefined
}

function _getDescriptionFilePaths(): string[] {
Expand Down

0 comments on commit 525d079

Please sign in to comment.