Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit b389171

Browse files
committed
Prepare pagination
1 parent 0c69f06 commit b389171

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

velog-frontend/src/components/user/UserHistory/UserHistory.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
22
import React, { Component, Fragment } from 'react';
3+
import { Link } from 'react-router-dom';
34
import CommentIcon from 'react-icons/lib/fa/comment';
45
import HeartIcon from 'react-icons/lib/fa/heart';
6+
import FakeLink from 'components/common/FakeLink';
57
import { type UserHistoryItem } from 'store/modules/profile';
68
import './UserHistory.scss';
79

@@ -11,9 +13,10 @@ type HistoryItemProps = {
1113
};
1214

1315
const HistoryItem = ({ item, username }: HistoryItemProps) => {
16+
const url = `/@${item.post.user.username}/${item.post.url_slug}`;
1417
const { type } = item;
1518
return (
16-
<div className="HistoryItem">
19+
<FakeLink className="HistoryItem" to={url}>
1720
{type === 'comment' ? (
1821
<div className="message">
1922
<CommentIcon className="comment" />@{username}님이 댓글을 남기셨습니다.
@@ -34,7 +37,9 @@ const HistoryItem = ({ item, username }: HistoryItemProps) => {
3437
</Fragment>
3538
)}
3639
<div className="post-info">
37-
<h4>{item.post.title}</h4>
40+
<h4>
41+
<Link to={url}>{item.post.title}</Link>
42+
</h4>
3843
<p>
3944
{item.post.short_description.slice(0, 150)}
4045
{item.post.short_description.length >= 150 && '...'}
@@ -47,7 +52,7 @@ const HistoryItem = ({ item, username }: HistoryItemProps) => {
4752
<div className="comment-text">{item.text}</div>
4853
</div>
4954
)}
50-
</div>
55+
</FakeLink>
5156
);
5257
};
5358

velog-frontend/src/components/user/UserHistory/UserHistory.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
width: 768px;
44
margin: 0 auto;
55
.HistoryItem {
6+
cursor: pointer;
67
padding-top: 2rem;
78
padding-bottom: 2rem;
8-
& + .HistoryItem {
9+
&+.HistoryItem {
910
border-top: 1px solid $oc-gray-2;
1011
}
1112
.message {
@@ -80,4 +81,4 @@
8081
}
8182
}
8283
}
83-
}
84+
}

velog-frontend/src/containers/user/UserHistoryContainer.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,40 @@ import { type UserHistoryItem } from 'store/modules/profile';
1010

1111
type Props = {
1212
userHistory: ?(UserHistoryItem[]),
13+
prefetched: ?(UserHistoryItem[]),
14+
prefetching: boolean,
15+
loading: boolean,
1316
} & ContextRouter;
1417
class UserHistoryContainer extends Component<Props> {
18+
lastOffset: ?number;
1519
initialize = async () => {
1620
const { match } = this.props;
1721
const { username } = match.params;
1822
try {
1923
if (!username) return;
20-
await ProfileActions.getUserHistory(username);
24+
await ProfileActions.getUserHistory({ username });
2125
} catch (e) {
2226
console.log(e);
2327
}
2428
};
29+
30+
prefetch = async () => {
31+
const { userHistory, prefetched, loading, prefetching, match } = this.props;
32+
const { username } = match.params;
33+
if (!username || !userHistory || loading || prefetching) return;
34+
// REVEAL
35+
ProfileActions.revealPrefetchedHistory();
36+
await Promise.resolve();
37+
try {
38+
await ProfileActions.prefetchUserHistory({
39+
username,
40+
offset: userHistory.length || 0,
41+
});
42+
} catch (e) {
43+
console.log(e);
44+
}
45+
};
46+
2547
componentDidMount() {
2648
ProfileActions.setSideVisibility(false);
2749
this.initialize();
@@ -40,8 +62,11 @@ class UserHistoryContainer extends Component<Props> {
4062
const enhance = compose(
4163
withRouter,
4264
connect(
43-
({ profile }: State) => ({
65+
({ profile, pender }: State) => ({
4466
userHistory: profile.userHistory,
67+
prefetched: profile.prefetchedHistory,
68+
loading: pender.pending['profile/GET_USER_HISTORY'],
69+
prefetching: pender.pending['profile/PREFETCH_USER_HISTORY'],
4570
}),
4671
() => ({}),
4772
),

velog-frontend/src/lib/api/users.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ import axios from 'lib/defaultClient';
33

44
export const listUserTags = (username: string) => axios.get(`/users/@${username}/tags`);
55
export const getProfile = (username: string) => axios.get(`/users/@${username}`);
6-
export const getHistory = (username: string) => axios.get(`/users/@${username}/history`);
6+
7+
type GetHistoryParams = {
8+
username: string,
9+
offset?: number,
10+
};
11+
export const getHistory = ({ username, offset }: GetHistoryParams) =>
12+
axios.get(`/users/@${username}/history?${offset ? `offset=${offset}` : ''}`);

velog-frontend/src/store/modules/profile.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const GET_TAG_INFO = 'profile/GET_TAG_INFO';
1212
const INITIALIZE = 'profile/INITIALIZE';
1313
const SET_SIDE_VISIBILITY = 'profile/SET_SIDE_VISIBILITY';
1414
const GET_USER_HISTORY = 'profile/GET_USER_HISTORY';
15+
const REVEAL_PREFETCHED_HISTORY = 'profile/REVEAL_PREFETCHED_HISTORY';
16+
const PREFETCH_USER_HISTORY = 'profile/PREFETCH_USER_HISTORY';
1517

1618
export const actionCreators = {
1719
initialize: createAction(INITIALIZE),
@@ -21,6 +23,8 @@ export const actionCreators = {
2123
getTagInfo: createAction(GET_TAG_INFO, CommonAPI.getTagInfo),
2224
setSideVisibility: createAction(SET_SIDE_VISIBILITY, (visible: boolean) => visible),
2325
getUserHistory: createAction(GET_USER_HISTORY, UsersAPI.getHistory),
26+
revealPrefetchedHistory: createAction(REVEAL_PREFETCHED_HISTORY),
27+
prefetchUserHistory: createAction(PREFETCH_USER_HISTORY, UsersAPI.getHistory),
2428
};
2529

2630
export type TagCountInfo = {
@@ -82,6 +86,7 @@ export type ProfileState = {
8286
tagCounts: ?(TagCountInfo[]),
8387
profile: ?Profile,
8488
userHistory: ?(UserHistoryItem[]),
89+
prefetchedHistory: ?(UserHistoryItem[]),
8590
rawTagName: ?string,
8691
side: boolean,
8792
};
@@ -91,6 +96,7 @@ const initialState = {
9196
profile: null,
9297
rawTagName: null,
9398
userHistory: null,
99+
prefetchedHistory: null,
94100
side: true,
95101
};
96102

@@ -109,6 +115,13 @@ const reducer = handleActions(
109115
side: payload,
110116
};
111117
},
118+
[REVEAL_PREFETCHED_HISTORY]: (state) => {
119+
return produce(state, (draft) => {
120+
if (!draft.userHistory || !draft.prefetchedHistory) return;
121+
draft.userHistory = draft.userHistory.concat(draft.prefetchedHistory);
122+
draft.prefetchedHistory = null;
123+
});
124+
},
112125
},
113126
initialState,
114127
);
@@ -149,11 +162,28 @@ export default applyPenders(reducer, [
149162
},
150163
{
151164
type: GET_USER_HISTORY,
165+
onPending: (state: ProfileState) => {
166+
return {
167+
...state,
168+
prefetchedHistory: null,
169+
};
170+
},
152171
onSuccess: (state: ProfileState, { payload }: GetUserHistoryResponseAction) => {
153172
return {
154173
...state,
155174
userHistory: payload.data,
156175
};
157176
},
158177
},
178+
{
179+
type: PREFETCH_USER_HISTORY,
180+
onSuccess: (state: ProfileState, { payload }: GetUserHistoryResponseAction) => {
181+
const currentIdList = (state.userHistory || []).map(item => item.id);
182+
const filtered = payload.data.filter(item => currentIdList.indexOf(item.id) === -1);
183+
return {
184+
...state,
185+
prefetchedHistory: filtered,
186+
};
187+
},
188+
},
159189
]);

0 commit comments

Comments
 (0)