This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
/
PostCardList.js
98 lines (88 loc) · 2.32 KB
/
PostCardList.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// @flow
import React, { Fragment } from 'react';
import PostCard from 'components/common/PostCard';
import type { PostItem } from 'store/modules/listing';
import FakePostCard from 'components/common/FakePostCard';
import './PostCardList.scss';
type Props = {
posts?: ?(PostItem[]),
loading: boolean,
prefetching: boolean,
width?: number,
hasEnded: boolean,
oneColumn?: boolean,
placeholderCount?: number,
hideUsername?: boolean,
};
const createArray = length => Array.from(Array(length).keys());
const getColumnCount = (width) => {
const xWide = 1920;
const wide = 1600;
const xLarge = 1200;
const large = 1024;
if (!width) return 1;
if (width < large) return 1;
if (width < xLarge) return 2;
if (width < wide) return 3;
if (width < xWide) return 4;
return 5;
};
const PostCardList = ({
posts,
loading,
prefetching,
width,
hasEnded,
oneColumn,
placeholderCount,
hideUsername,
}: Props) => {
if (loading) {
return (
<div className="PostCardList">
{createArray(placeholderCount).map(num => <FakePostCard key={num} oneColumn={oneColumn} />)}
</div>
);
}
const columnCount = oneColumn ? 1 : getColumnCount(width);
if (!posts) return null;
const postList = (hasEnded || posts.length <= 20
? posts
: posts.slice(0, posts.length - posts.length % columnCount)
).map(post => (
<PostCard
key={post.id}
id={post.id}
thumbnail={post.thumbnail}
username={post.user.username}
userThumbnail={post.user.thumbnail}
title={post.title}
body={(post.meta && post.meta.short_description) || post.body}
date={post.released_at}
urlSlug={post.url_slug}
oneColumn={oneColumn}
commentsCount={post.comments_count}
isPrivate={post.is_private}
hideUsername={hideUsername}
/>
));
return (
<Fragment>
<div className="PostCardList">
{postList &&
postList.length === 0 && <div className="empty-list">아직 작성한 포스트가 없습니다.</div>}
{postList}
{prefetching &&
createArray(columnCount).map(num => <FakePostCard key={num} oneColumn={oneColumn} />)}
</div>
</Fragment>
);
};
PostCardList.defaultProps = {
posts: [],
oneColumn: false,
width: 0,
placeholderCount: 10,
hideUsername: false,
};
export default PostCardList;