mirror of
https://github.com/tjsga/tj-sga-website-react.git
synced 2025-04-09 22:50:17 -04:00
Added article pagination
This commit is contained in:
parent
4435307996
commit
f5c0a4196e
File diff suppressed because one or more lines are too long
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"typescript.tsdk": "node_modules\\typescript\\lib"
|
||||||
|
}
|
|
@ -1,23 +1,64 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import useQuery from '../hooks/useInitiatives';
|
|
||||||
import ArticleRow from './ArticleRow';
|
import ArticleRow from './ArticleRow';
|
||||||
|
import sanity from '../sanity';
|
||||||
|
|
||||||
export default function ArticleList() {
|
export default function ArticleList() {
|
||||||
let articles = useQuery<SGA.ArticleDocument[]>(
|
let [articles, setArticles] = React.useState<SGA.ArticleDocument[]>([]);
|
||||||
'*[_type == "article"] | order(publish_date desc) [0...3]'
|
let [reachedEnd, setReachedEnd] = React.useState<boolean>(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<SGA.ArticleDocument[]>(query, params);
|
||||||
|
|
||||||
|
console.log(articles.length);
|
||||||
|
|
||||||
|
if (articles.length < 3) {
|
||||||
|
setReachedEnd(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the next articles
|
||||||
|
setArticles((oldArticles) => oldArticles.concat(articles));
|
||||||
|
};
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
addArticles('3000-01-01', '');
|
||||||
|
}, []);
|
||||||
|
|
||||||
if (!articles) {
|
if (!articles) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(articles);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
{articles.map((article) => {
|
{articles.map((article) => {
|
||||||
return <ArticleRow key={article._id} article={article} />;
|
return <ArticleRow key={article._id} article={article} />;
|
||||||
})}
|
})}
|
||||||
|
|
||||||
|
{!reachedEnd ? (
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
let lastArticle = articles[articles.length - 1];
|
||||||
|
addArticles(lastArticle.publish_date, lastArticle.title);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Load more articles
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<div>No more articles to show</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user