Skip to content

Commit 7e60154

Browse files
committed
make pagination logic
1 parent ab2acb3 commit 7e60154

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

services/app/assets/js/widgets/components/Game/CompletedGames.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ const CompletedGames = ({ games, onShowMoreButtonClick = null }) => (
4747
</table>
4848
{ onShowMoreButtonClick
4949
? (
50-
// TODO: use currentPage + 1
51-
<button type="button" className="mr-4 btn btn-light" onClick={() => onShowMoreButtonClick(2)}>
50+
<button type="button" className="mt-2 btn btn-light" onClick={() => onShowMoreButtonClick()}>
5251
Show More
5352
</button>
5453
) : null}

services/app/assets/js/widgets/containers/UserProfile.jsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const getUserAvatarUrl = ({ githubId, discordId, discordAvatar }) => {
2323
const UserProfile = () => {
2424
const [stats, setStats] = useState(null);
2525
const [completedGames, setCompletedGames] = useState([]);
26+
const [currentPage, setCurrenPage] = useState(1);
27+
const [totalPages, setTotalPages] = useState(1);
2628

2729
const dispatch = useDispatch();
2830

@@ -39,34 +41,23 @@ const UserProfile = () => {
3941
});
4042
}, [dispatch]);
4143

42-
useEffect(() => {
43-
// TODO: fetch completed games when you activate completed games tab
44-
// TODO: use loadCompletedGames(0)
45-
const userId = window.location.pathname.split('/').pop();
46-
47-
axios
48-
.get(`/api/v1/user/${userId}/completed_games?page_size=3`)
49-
.then(response => {
50-
setCompletedGames(camelizeKeys(response.data.games));
51-
})
52-
.catch(error => {
53-
dispatch(actions.setError(error));
54-
});
55-
}, [dispatch]);
56-
5744
const dateParse = date => new Date(date).toLocaleString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
5845

59-
const loadCompletedGames = page => {
46+
const loadCompletedGames = () => {
6047
const userId = window.location.pathname.split('/').pop();
6148

62-
axios
63-
.get(`/api/v1/user/${userId}/completed_games?page_size=3&page=${page}`)
49+
if (currentPage <= totalPages) {
50+
axios
51+
.get(`/api/v1/user/${userId}/completed_games?page_size=3&page=${currentPage}`)
6452
.then(response => {
53+
setTotalPages(response.data.page_info.total_pages);
54+
setCurrenPage(currentPage + 1);
6555
setCompletedGames(completedGames.concat(response.data.games));
6656
})
6757
.catch(error => {
6858
dispatch(actions.setError(error));
6959
});
60+
}
7061
};
7162

7263
const renderAchievemnt = achievement => {
@@ -149,9 +140,21 @@ const UserProfile = () => {
149140
<div className="text-left my-5">
150141
{completedGames && completedGames.length > 0 && (
151142
<>
152-
<CompletedGames games={completedGames} onShowMoreButtonClick={loadCompletedGames} />
143+
<CompletedGames
144+
games={completedGames}
145+
onShowMoreButtonClick={
146+
currentPage <= totalPages ? loadCompletedGames : null
147+
}
148+
/>
153149
</>
154150
)}
151+
{completedGames && completedGames.length === 0 && (
152+
<>
153+
<div className="text-center text-muted">
154+
No completed games
155+
</div>
156+
</>
157+
)}
155158
</div>
156159
</div>
157160
</div>
@@ -180,6 +183,7 @@ const UserProfile = () => {
180183
role="tab"
181184
aria-controls="completedGames"
182185
aria-selected="false"
186+
onClick={loadCompletedGames}
183187
>
184188
Completed games
185189
</a>

0 commit comments

Comments
 (0)