From 14f405bf586e39d8b683714059f24fc09b992f11 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 5 Jul 2022 12:53:21 -0700 Subject: [PATCH] Fix followers pagination UI --- webroot/js/components/federation/followers.js | 229 +++++++++--------- 1 file changed, 115 insertions(+), 114 deletions(-) diff --git a/webroot/js/components/federation/followers.js b/webroot/js/components/federation/followers.js index 64ad8713d..e2c06f5be 100644 --- a/webroot/js/components/federation/followers.js +++ b/webroot/js/components/federation/followers.js @@ -3,132 +3,133 @@ import htm from '/js/web_modules/htm.js'; import { URL_FOLLOWERS } from '/js/utils/constants.js'; const html = htm.bind(h); export default class FollowerList extends Component { - constructor(props) { - super(props); + constructor(props) { + super(props); - this.state = { - followers: [], - followersPage: 0, - currentPage: 0, - total: 0, - }; - } + this.state = { + followers: [], + followersPage: 0, + currentPage: 0, + total: 0, + }; + } - componentDidMount() { - try { - this.getFollowers(); - } catch (e) { - console.error('followers error: ', e); - } - } + componentDidMount() { + try { + this.getFollowers(); + } catch (e) { + console.error('followers error: ', e); + } + } - async getFollowers() { - const { currentPage } = this.state; - const limit = 24; - const offset = currentPage * limit; - const u = `${URL_FOLLOWERS}?offset=${offset}&limit=${limit}`; - const response = await fetch(u); - const followers = await response.json(); + async getFollowers(requestedPage) { + const limit = 24; + const offset = requestedPage * limit; + const u = `${URL_FOLLOWERS}?offset=${offset || 0}&limit=${limit}`; + const response = await fetch(u); + const followers = await response.json(); + const pages = Math.ceil(followers.total / limit); - this.setState({ - followers: followers.results, - total: response.total, - }); - } + this.setState({ + followers: followers.results, + total: followers.total, + pages: pages, + }); + } - changeFollowersPage(page) { - this.setState({ currentPage: page }); - this.getFollowers(); - } + changeFollowersPage(requestedPage) { + this.setState({ currentPage: requestedPage }); + this.getFollowers(requestedPage); + } - render() { - const { followers, total, currentPage } = this.state; - if (!followers) { - return null; - } + render() { + const { followers, total, pages, currentPage } = this.state; + if (!followers) { + return null; + } - const noFollowersInfo = html`
-

Be the first to follow this live stream.

-

- By following this stream you'll get updates when it goes live, receive - posts from the streamer, and be featured here as a follower. -

-

- Learn more about ${' '} - The Fediverse, where you can follow this server as well as so much more. -

-
`; + const noFollowersInfo = html`
+

Be the first to follow this live stream.

+

+ By following this stream you'll get updates when it goes live, receive + posts from the streamer, and be featured here as a follower. +

+

+ Learn more about ${' '} + The Fediverse, where you can follow this server as well as so much more. +

+
`; - const paginationControls = - total > 1 && - Array(total) - .fill() - .map((x, n) => { - const activePageClass = - n === currentPage && - 'bg-indigo-600 rounded-full shadow-md focus:shadow-md text-white'; - return html`
  • - this.changeFollowersPage(n)} - > - ${n + 1} - -
  • `; - }); + const paginationControls = + pages > 1 && + Array(pages) + .fill() + .map((x, n) => { + const activePageClass = + n === currentPage && + 'bg-indigo-600 rounded-full shadow-md focus:shadow-md text-white'; + return html`
  • + this.changeFollowersPage(n)} + > + ${n + 1} + +
  • `; + }); - return html` -
    -
    - ${followers.length === 0 && noFollowersInfo} - ${followers.map((follower) => { - return html` <${SingleFollower} user=${follower} /> `; - })} -
    -
    - -
    -
    - `; - } + return html` +
    +
    + ${followers.length === 0 && noFollowersInfo} + ${followers.map((follower) => { + return html` <${SingleFollower} user=${follower} /> `; + })} +
    +
    + +
    +
    + `; + } } function SingleFollower(props) { - const { user } = props; - const { name, username, link, image } = user; + const { user } = props; + const { name, username, link, image } = user; - var displayName = name; - var displayUsername = username; + var displayName = name; + var displayUsername = username; - if (!displayName) { - displayName = displayUsername.split('@', 1)[0]; - } - return html` - - { - currentTarget.onerror = null; - currentTarget.src = '/img/logo.svg'; - }} - /> -
    -

    ${displayName}

    -

    ${displayUsername}

    -
    -
    - `; + if (!displayName) { + displayName = displayUsername.split('@', 1)[0]; + } + return html` + + { + currentTarget.onerror = null; + currentTarget.src = '/img/logo.svg'; + }} + /> +
    +

    ${displayName}

    +

    ${displayUsername}

    +
    +
    + `; }