From fb99301a3caac40a7566186699fb2aa314af1b8f Mon Sep 17 00:00:00 2001 From: srgooglo Date: Mon, 6 Jun 2022 10:00:32 +0200 Subject: [PATCH] load more posts on feed --- .../app/src/components/PostsFeed/index.jsx | 80 ++++++++++++++++--- .../app/src/components/PostsFeed/index.less | 30 ++----- 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/packages/app/src/components/PostsFeed/index.jsx b/packages/app/src/components/PostsFeed/index.jsx index 92457b4a..6fe3622a 100644 --- a/packages/app/src/components/PostsFeed/index.jsx +++ b/packages/app/src/components/PostsFeed/index.jsx @@ -1,15 +1,29 @@ import React from "react" import * as antd from "antd" import { User } from "models" -import { PostCard } from "components" +import { PostCard, LoadMore } from "components" import "./index.less" +const LoadingComponent = () => { + return +} + +const NoResultComponent = () => { + return +} + export default class PostsFeed extends React.Component { state = { selfId: null, initialLoading: true, renderList: [], + fetchingData: false, + hasMorePosts: true, } api = window.app.request @@ -54,23 +68,52 @@ export default class PostsFeed extends React.Component { } loadPosts = async ({ - startIndex, - stopIndex, + trim, + replace = false } = {}) => { + // toogle fetching flag + await this.setState({ + fetchingData: true, + }) + + // get posts from api const result = await this.api.get.feed(undefined, { - startIndex, - stopIndex, - feedLength: this.props.feedLength, + trim: trim ?? this.state.renderList.length, + limit: this.props.feedLength ?? window.app.settings.get("feed_max_fetch"), user_id: this.props.fromUserId, }) console.log(result) if (result) { - this.setState({ - renderList: result.map((item, index) => this.getPostRender(item, index)) - }) + // if result is empty, its mean there is no more posts, so set hasMorePosts to false + if (result.length === 0) { + await this.setState({ + hasMorePosts: false, + }) + return false + } + + if (replace) { + // replace all posts render list + await this.setState({ + renderList: result.map((item) => this.getPostRender(item, item.key)) + }) + } else { + // else append posts to render list + await this.setState({ + renderList: [ + ...this.state.renderList, + ...result.map((item) => this.getPostRender(item, item.key)) + ] + }) + } } + + // toogle fetching flag + await this.setState({ + fetchingData: false, + }) } onLikePost = async (data) => { @@ -110,9 +153,9 @@ export default class PostsFeed extends React.Component { }) } - getPostRender = (item, index) => { + getPostRender = (item, index = this.state.renderList.length) => { return i._id === item._id)} + key={index} data={item} selfId={this.state.selfId} events={{ @@ -134,8 +177,19 @@ export default class PostsFeed extends React.Component { } - return
- {this.state.renderList} + return
+ { + this.loadPosts() + }} + loadingComponent={LoadingComponent} + noResultComponent={NoResultComponent} + fetching={this.state.fetchingData} + hasMore={this.state.hasMorePosts} + className="posts" + > + {this.state.renderList} +
} } \ No newline at end of file diff --git a/packages/app/src/components/PostsFeed/index.less b/packages/app/src/components/PostsFeed/index.less index 718a1b71..c36da957 100644 --- a/packages/app/src/components/PostsFeed/index.less +++ b/packages/app/src/components/PostsFeed/index.less @@ -6,40 +6,22 @@ width: 100%; - > div { - margin-bottom: 20px; + .infinite-scroll { + width: 100%; } - .content { - overflow: visible; - + .posts { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%; - max-width: 40vw; - .rc-virtual-list-holder { - overflow-y: visible !important; - width: 100%; + overflow: overlay; - } - - .rc-virtual-list-holder-inner { - width: 100%; - overflow: visible; - - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - - >div { - width: 100%; - margin-bottom: 15px; - } + >div { + margin-bottom: 20px; } } } \ No newline at end of file