From f4c7ab4e306d2f04324853e67032d370abd65d0c Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 20 May 2022 15:37:08 +0200 Subject: chore: handle blog pagination when JS is disabled --- src/components/organisms/layout/posts-list.tsx | 95 ++++++++++++++++++-------- 1 file changed, 68 insertions(+), 27 deletions(-) (limited to 'src/components/organisms') diff --git a/src/components/organisms/layout/posts-list.tsx b/src/components/organisms/layout/posts-list.tsx index 9dfe254..50192dd 100644 --- a/src/components/organisms/layout/posts-list.tsx +++ b/src/components/organisms/layout/posts-list.tsx @@ -2,6 +2,11 @@ import Button from '@components/atoms/buttons/button'; import Heading, { type HeadingLevel } from '@components/atoms/headings/heading'; import ProgressBar from '@components/atoms/loaders/progress-bar'; import Spinner from '@components/atoms/loaders/spinner'; +import Pagination, { + PaginationProps, +} from '@components/molecules/nav/pagination'; +import useIsMounted from '@utils/hooks/use-is-mounted'; +import useSettings from '@utils/hooks/use-settings'; import { FC, Fragment, useRef } from 'react'; import { useIntl } from 'react-intl'; import styles from './posts-list.module.scss'; @@ -18,7 +23,7 @@ export type YearCollection = { [key: string]: Post[]; }; -export type PostsListProps = { +export type PostsListProps = Pick & { /** * True to display the posts by year. Default: false. */ @@ -31,6 +36,10 @@ export type PostsListProps = { * Load more button handler. */ loadMore?: () => void; + /** + * The current page number. Default: 1. + */ + pageNumber?: number; /** * The posts data. */ @@ -74,16 +83,22 @@ const sortPostsByYear = (data: Post[]): YearCollection => { * Render a list of post summaries. */ const PostsList: FC = ({ + baseUrl, byYear = false, isLoading = false, loadMore, + pageNumber = 1, posts, showLoadMoreBtn = false, + siblings, titleLevel, total, }) => { const intl = useIntl(); + const listRef = useRef(null); const lastPostRef = useRef(null); + const isMounted = useIsMounted(listRef); + const { blog } = useSettings(); /** * Retrieve the list of posts. @@ -99,7 +114,7 @@ const PostsList: FC = ({ const lastPostId = allPosts[allPosts.length - 1].id; return ( -
    +
      {allPosts.map(({ id, ...post }) => (
    1. @@ -168,34 +183,60 @@ const PostsList: FC = ({ loadMore && loadMore(); }; - return posts.length === 0 ? ( -

      - {intl.formatMessage({ - defaultMessage: 'No results found.', - description: 'PostsList: no results', - id: 'vK7Sxv', - })} -

      - ) : ( + const getProgressBar = () => { + return ( + <> + + {showLoadMoreBtn && ( + + )} + + ); + }; + + const getPagination = () => { + return posts.length <= blog.postsPerPage ? ( + + ) : ( + <> + ); + }; + + if (posts.length === 0) { + return ( +

      + {intl.formatMessage({ + defaultMessage: 'No results found.', + description: 'PostsList: no results', + id: 'vK7Sxv', + })} +

      + ); + } + + return ( <> {getPosts()} {isLoading && } - - {showLoadMoreBtn && ( - - )} + {isMounted ? getProgressBar() : getPagination()} ); }; -- cgit v1.2.3