Fix followers pagination UI

This commit is contained in:
Gabe Kangas 2022-07-05 12:53:21 -07:00
parent e443188cbc
commit 14f405bf58
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA

View File

@ -22,27 +22,28 @@ export default class FollowerList extends Component {
} }
} }
async getFollowers() { async getFollowers(requestedPage) {
const { currentPage } = this.state;
const limit = 24; const limit = 24;
const offset = currentPage * limit; const offset = requestedPage * limit;
const u = `${URL_FOLLOWERS}?offset=${offset}&limit=${limit}`; const u = `${URL_FOLLOWERS}?offset=${offset || 0}&limit=${limit}`;
const response = await fetch(u); const response = await fetch(u);
const followers = await response.json(); const followers = await response.json();
const pages = Math.ceil(followers.total / limit);
this.setState({ this.setState({
followers: followers.results, followers: followers.results,
total: response.total, total: followers.total,
pages: pages,
}); });
} }
changeFollowersPage(page) { changeFollowersPage(requestedPage) {
this.setState({ currentPage: page }); this.setState({ currentPage: requestedPage });
this.getFollowers(); this.getFollowers(requestedPage);
} }
render() { render() {
const { followers, total, currentPage } = this.state; const { followers, total, pages, currentPage } = this.state;
if (!followers) { if (!followers) {
return null; return null;
} }
@ -62,8 +63,8 @@ export default class FollowerList extends Component {
</div>`; </div>`;
const paginationControls = const paginationControls =
total > 1 && pages > 1 &&
Array(total) Array(pages)
.fill() .fill()
.map((x, n) => { .map((x, n) => {
const activePageClass = const activePageClass =