import React from 'react'; import '../css/article.css'; import sanity from '../sanity'; import ArticleRow from './ArticleRow'; import BlueButton from './BlueButton'; import Centered from './Centered'; export default function ArticleList() { let [articles, setArticles] = React.useState([]); let [reachedEnd, setReachedEnd] = React.useState(false); // Fetch the next three articles const addArticles = async (last_date: string, last_title: string) => { const query = `*[_type == "article" && ( publish_date < $last_date || ( publish_date == $last_date && title > $last_title ) )] | order(publish_date desc, title) [0...3]`; const params = { last_date, last_title, }; let articles = await sanity.fetch(query, params); if (articles.length < 3) { setReachedEnd(true); } // Add the next articles setArticles((oldArticles) => oldArticles.concat(articles)); }; React.useEffect(() => { addArticles('3000-01-01', ''); }, []); if (!articles) { return null; } let bottomComponent: any; if (reachedEnd) { bottomComponent =
No more articles to show
; } else { bottomComponent = ( { let { publish_date, title } = articles[articles.length - 1]; addArticles(publish_date, title); }} > Load more articles ); } const articleList = articles.map((article) => ( )); return (
{articleList} {bottomComponent}
); }