mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 19:14:16 +00:00
load more posts on feed
This commit is contained in:
parent
fbba723c2c
commit
fb99301a3c
@ -1,15 +1,29 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import * as antd from "antd"
|
import * as antd from "antd"
|
||||||
import { User } from "models"
|
import { User } from "models"
|
||||||
import { PostCard } from "components"
|
import { PostCard, LoadMore } from "components"
|
||||||
|
|
||||||
import "./index.less"
|
import "./index.less"
|
||||||
|
|
||||||
|
const LoadingComponent = () => {
|
||||||
|
return <antd.Skeleton active />
|
||||||
|
}
|
||||||
|
|
||||||
|
const NoResultComponent = () => {
|
||||||
|
return <antd.Result
|
||||||
|
status="info"
|
||||||
|
title="This is the end"
|
||||||
|
subTitle="We dont have more posts for you"
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
|
||||||
export default class PostsFeed extends React.Component {
|
export default class PostsFeed extends React.Component {
|
||||||
state = {
|
state = {
|
||||||
selfId: null,
|
selfId: null,
|
||||||
initialLoading: true,
|
initialLoading: true,
|
||||||
renderList: [],
|
renderList: [],
|
||||||
|
fetchingData: false,
|
||||||
|
hasMorePosts: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
api = window.app.request
|
api = window.app.request
|
||||||
@ -54,23 +68,52 @@ export default class PostsFeed extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadPosts = async ({
|
loadPosts = async ({
|
||||||
startIndex,
|
trim,
|
||||||
stopIndex,
|
replace = false
|
||||||
} = {}) => {
|
} = {}) => {
|
||||||
|
// toogle fetching flag
|
||||||
|
await this.setState({
|
||||||
|
fetchingData: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
// get posts from api
|
||||||
const result = await this.api.get.feed(undefined, {
|
const result = await this.api.get.feed(undefined, {
|
||||||
startIndex,
|
trim: trim ?? this.state.renderList.length,
|
||||||
stopIndex,
|
limit: this.props.feedLength ?? window.app.settings.get("feed_max_fetch"),
|
||||||
feedLength: this.props.feedLength,
|
|
||||||
user_id: this.props.fromUserId,
|
user_id: this.props.fromUserId,
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(result)
|
console.log(result)
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
this.setState({
|
// if result is empty, its mean there is no more posts, so set hasMorePosts to false
|
||||||
renderList: result.map((item, index) => this.getPostRender(item, index))
|
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) => {
|
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 <PostCard
|
return <PostCard
|
||||||
key={index ?? this.state.renderList.findIndex((i) => i._id === item._id)}
|
key={index}
|
||||||
data={item}
|
data={item}
|
||||||
selfId={this.state.selfId}
|
selfId={this.state.selfId}
|
||||||
events={{
|
events={{
|
||||||
@ -134,8 +177,19 @@ export default class PostsFeed extends React.Component {
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
return <div className="postsFeed" ref={this.listRef}>
|
return <div id="postsFeed" className="postsFeed" ref={this.listRef}>
|
||||||
{this.state.renderList}
|
<LoadMore
|
||||||
|
onBottom={() => {
|
||||||
|
this.loadPosts()
|
||||||
|
}}
|
||||||
|
loadingComponent={LoadingComponent}
|
||||||
|
noResultComponent={NoResultComponent}
|
||||||
|
fetching={this.state.fetchingData}
|
||||||
|
hasMore={this.state.hasMorePosts}
|
||||||
|
className="posts"
|
||||||
|
>
|
||||||
|
{this.state.renderList}
|
||||||
|
</LoadMore>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,40 +6,22 @@
|
|||||||
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|
||||||
> div {
|
.infinite-scroll {
|
||||||
margin-bottom: 20px;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content {
|
.posts {
|
||||||
overflow: visible;
|
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 40vw;
|
|
||||||
|
|
||||||
.rc-virtual-list-holder {
|
overflow: overlay;
|
||||||
overflow-y: visible !important;
|
|
||||||
width: 100%;
|
|
||||||
|
|
||||||
}
|
>div {
|
||||||
|
margin-bottom: 20px;
|
||||||
.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user