-
Notifications
You must be signed in to change notification settings - Fork 19
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
Showing
6 changed files
with
86 additions
and
29,401 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client' | ||
|
||
import { useInfiniteQuery } from '@tanstack/react-query' | ||
import { fetchNewsArticles } from '../../../contentful/newsArticle' | ||
import { ARTICLE_LIMIT } from '../../utils/constants' | ||
import NewsArticleCard from './NewsArticleCard' | ||
import { useMemo } from 'react' | ||
|
||
const fetchNewArticles = async (category, skip): Promise<Array<any>> => { | ||
const newArticles = await fetchNewsArticles({ | ||
preview: false, | ||
category: category, | ||
limit: ARTICLE_LIMIT, | ||
skip: skip, | ||
}) | ||
|
||
return newArticles ?? [] | ||
} | ||
|
||
const MoreArticles = ({ category }: { category: string }) => { | ||
const { data: nextArticles, fetchNextPage: fetchNextArticles } = | ||
useInfiniteQuery({ | ||
queryKey: ['next-articles', category], | ||
initialPageParam: 0, | ||
queryFn: ({ pageParam }) => fetchNewArticles(category, pageParam), | ||
getNextPageParam: (lastPage, allPages) => allPages.length * ARTICLE_LIMIT, | ||
}) | ||
|
||
const handleLoadMore = () => { | ||
fetchNextArticles() | ||
} | ||
|
||
const newArticles = useMemo(() => { | ||
const articles = nextArticles?.pages.slice(1) | ||
return articles?.flat() | ||
}, [nextArticles]) | ||
|
||
const showLoadMore = useMemo(() => { | ||
const lastArticles = nextArticles?.pages?.[nextArticles?.pages?.length - 1] | ||
if (lastArticles?.length === ARTICLE_LIMIT || !lastArticles) return true | ||
return false | ||
}, [nextArticles]) | ||
|
||
return ( | ||
<> | ||
{newArticles?.length | ||
? newArticles.map((article) => ( | ||
<NewsArticleCard article={article} key={article.articleUrl} /> | ||
)) | ||
: null} | ||
<div className="mt-4 col-span-3 flex justify-center"> | ||
{showLoadMore ? ( | ||
<button | ||
className="text-th-fgd-2 md:hover:text-th-fgd-4 font-bold" | ||
onClick={handleLoadMore} | ||
> | ||
Load more | ||
</button> | ||
) : ( | ||
<p className="text-sm">No news to show...</p> | ||
)} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default MoreArticles |
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
Oops, something went wrong.
f9b09ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
mango-web – ./
mango-web-git-main-blockworks-foundation.vercel.app
mango-web.vercel.app
mango-web-blockworks-foundation.vercel.app
www.mango.markets
mango.markets