diff options
author | Renard | 2019-09-17 01:24:06 -0300 |
---|---|---|
committer | Renard | 2019-09-17 01:24:06 -0300 |
commit | 07f8514ee09dbe4154166e3f8c4189e2953293df (patch) | |
tree | 51b9e9526127ab62651fdbc2cdf9440f7c369efa | |
parent | 1af53424e2826b96092dd56d6fca59b938617979 (diff) | |
download | bai-client-07f8514ee09dbe4154166e3f8c4189e2953293df.tar.gz bai-client-07f8514ee09dbe4154166e3f8c4189e2953293df.tar.xz bai-client-07f8514ee09dbe4154166e3f8c4189e2953293df.zip |
Infinite scroll
-rw-r--r-- | src/Board.js | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/Board.js b/src/Board.js index 958fb29..55df603 100644 --- a/src/Board.js +++ b/src/Board.js @@ -19,8 +19,11 @@ class Board extends Component { this.state = { isLoaded: false, threadList: [], - error: null + error: null, + loadingMore: false }; + this.handleScroll = this.handleScroll.bind(this); + this.threadOffset = 10; } componentDidMount() { @@ -37,6 +40,37 @@ class Board extends Component { this.setState({ isLoaded: true, threadList: resource["threads"] }); }); window.scrollTo(0, 0); + window.addEventListener("scroll", this.handleScroll); + } + + componentWillUnmount() { + window.removeEventListener("scroll", this.handleScroll); + } + + handleScroll() { + const { scrollHeight, scrollTop, clientHeight } = document.documentElement; + if (scrollHeight - scrollTop === clientHeight && !this.state.loadingMore) { + this.setState({ loadingMore: true }); + this.fetchMoreThreads(); + } + } + + fetchMoreThreads() { + fetch( + `https://bienvenidoainternet.org/cgi/api/list?dir=${this.props.dir}&replies=5&limit=10&offset${this.threadOffset}` + ) + .then(response => { + return response.json(); + }) + .then(resource => { + if (resource.state === "success") { + if (resource.threads.length > 0) { + const moreThreads = this.state.threadList.concat(resource.threads); + this.setState({ threadList: moreThreads, loadingMore: false }); + } + } + return; + }); } componentDidUpdate(prevProps) { @@ -52,7 +86,7 @@ class Board extends Component { } render() { - const { isLoaded, error, threadList } = this.state; + const { isLoaded, error, threadList, loadingMore } = this.state; if (error != null) { return ( @@ -121,6 +155,12 @@ class Board extends Component { </Segment> </Segment.Group> ))} + + {loadingMore ? ( + <Loader active inline="centered" style={{ marginTop: "3em" }}> + Cargando más hilos ... + </Loader> + ) : null} </React.Fragment> ); } |