/* Graphictoria 5 (https://gtoria.net) Copyright © XlXi 2022 */ import { Component, createRef } 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, feedDisabled: false, loadingCursor: false, error: '', feedPosts: [], mouseHover: -1 }; this.inputBox = createRef(); // XlXi: Thanks, React. this.loadPosts = this.loadPosts.bind(this); this.loadMore = this.loadMore.bind(this); this.sharePost = this.sharePost.bind(this); } componentWillMount() { window.addEventListener('scroll', this.loadMore); } componentWillUnmount() { window.removeEventListener('scroll', this.loadMore); } componentDidMount() { this.loadPosts(); } loadPosts() { 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 }); }); } } } sharePost() { this.setState({ feedDisabled: true }); const postText = this.inputBox.current.value; if (postText == '') { this.setState({ feedDisabled: false, error: 'Your shout cannot be blank.' }); this.inputBox.current.focus(); return; } axios.post(buildGenericApiUrl('api', `feed/v1/share`), { content: postText }) .then(res => { this.inputBox.current.value = ''; this.setState({ feedLoaded: false, loadingCursor: false, feedDisabled: false }); this.loadPosts(); }) .catch(err => { const data = err.response.data; this.setState({ feedDisabled: false, error: data.message }); this.inputBox.current.focus(); }); } render() { return ( <>

My Feed

{ this.state.error != '' ?
{ this.state.error }
: null }
this.setState({ error: '' }) } ref={ this.inputBox } />
{ 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;