// © XlXi 2021 // Graphictoria 5 import { Component } from 'react'; import axios from 'axios'; import Twemoji from 'react-twemoji'; import { buildGenericApiUrl } from '../util/HTTP.js'; import Loader from './Loader'; axios.defaults.withCredentials = true; class Feed extends Component { constructor(props) { super(props); this.state = { feedLoaded: false, loadingCursor: false, feedPosts: [], mouseHover: -1 }; this.loadMore = this.loadMore.bind(this); } componentWillMount() { window.addEventListener('scroll', this.loadMore); } componentWillUnmount() { window.removeEventListener('scroll', this.loadMore); } componentDidMount() { axios.get(buildGenericApiUrl('api', 'feed/v1/list-json')) .then(res => { const posts = res.data; this.nextCursor = posts.next_cursor; this.setState({ feedPosts: posts.data, feedLoaded: true }); }); } // XlXi: https://stackoverflow.com/questions/57778950/how-to-load-more-search-results-when-scrolling-down-the-page-in-react-js loadMore() { // XlXi: Taking the height of the footer into account. if (window.innerHeight + document.documentElement.scrollTop >= document.scrollingElement.scrollHeight-200) { if (!!(this.nextCursor) && !this.state.loadingCursor) { this.setState({ loadingCursor: true }); axios.get(buildGenericApiUrl('api', `feed/v1/list-json?cursor=${this.nextCursor}`)) .then(res => { const posts = res.data; this.nextCursor = posts.next_cursor; this.setState({ feedPosts: this.state.feedPosts.concat(posts.data), loadingCursor: false }); }); } } } render() { return ( <>

My Feed

{ this.state.feedLoaded ? ( this.state.feedPosts.length > 0 ? <>
{ this.state.feedPosts.map(({ postId, poster, time, content }, index) => <>
this.setState({ mouseHover: index }) } onMouseLeave={ () => this.setState({ mouseHover: -1 }) }>
{ poster.type == 'User' ? : }
{ poster.name }{ poster.icon ? <>  : null } { this.state.mouseHover == index ? Report : null }

{ time }

{ content }

{ this.state.feedPosts.length != (index+1) ?
: null } ) }
{ this.state.loadingCursor ?
: null } :

No posts were found. You could be the first!

) :
} ); } } export default Feed;