aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/PostsList/PostsList.tsx
blob: 31cdbf65543028d12d3e6508cd971e61fa8fc697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import useSWRInfinite from 'swr/infinite';
import { t } from '@lingui/macro';
import { config } from '@config/website';
import { getPublishedPosts } from '@services/graphql/blog';
import { PostsList as PostsListData } from '@ts/types/blog';
import styles from './PostsList.module.scss';
import PostPreview from '@components/PostPreview/PostPreview';
import { Button } from '@components/Buttons';

type TitleLevel = 2 | 3 | 4 | 5 | 6;

const PostsList = ({ titleLevel }: { titleLevel: TitleLevel }) => {
  const TitleTag = `h${titleLevel}` as keyof JSX.IntrinsicElements;

  const getKey = (pageIndex: number, previousData: PostsListData) => {
    if (previousData && !previousData.posts) return null;

    const args =
      pageIndex === 0
        ? { first: config.postsPerPage }
        : {
            first: config.postsPerPage,
            after: previousData.pageInfo.endCursor,
          };

    return args;
  };

  const { data, error, size, setSize } = useSWRInfinite(
    getKey,
    getPublishedPosts
  );

  const isLoadingInitialData = !data && !error;
  const isLoadingMore: boolean =
    isLoadingInitialData ||
    (size > 0 && data !== undefined && typeof data[size - 1] === 'undefined');

  if (error) return <div>{t`Failed to load.`}</div>;
  if (!data) return <div>{t`Loading...`}</div>;

  const getPostsList = () => {
    return data.map((page) => {
      if (page.posts.length === 0) {
        return t`No results found.`;
      } else {
        return page.posts.map((post) => {
          return (
            <li key={post.id} className={styles.item}>
              <PostPreview post={post} TitleTag={TitleTag} />
            </li>
          );
        });
      }
    });
  };

  const hasNextPage = data && data[data.length - 1].pageInfo.hasNextPage;

  return (
    <>
      <ol className={styles.wrapper}>{getPostsList()}</ol>
      {hasNextPage && (
        <Button
          isDisabled={isLoadingMore}
          clickHandler={() => setSize(size + 1)}
        >{t`Load more?`}</Button>
      )}
    </>
  );
};

export default PostsList;