From 4da1a0b5069a1efb6393cc485622b406b50c79aa Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 11 Mar 2022 22:52:25 +0100 Subject: [PATCH] added `PostsFeed` component --- .../app/src/components/PostsFeed/index.jsx | 85 +++++++++++++++++++ .../app/src/components/PostsFeed/index.less | 22 +++++ packages/app/src/components/index.js | 1 + 3 files changed, 108 insertions(+) create mode 100644 packages/app/src/components/PostsFeed/index.jsx create mode 100644 packages/app/src/components/PostsFeed/index.less diff --git a/packages/app/src/components/PostsFeed/index.jsx b/packages/app/src/components/PostsFeed/index.jsx new file mode 100644 index 00000000..b00b6c83 --- /dev/null +++ b/packages/app/src/components/PostsFeed/index.jsx @@ -0,0 +1,85 @@ +import React from "react" +import * as antd from "antd" +import { PostCard, PostCreator } from "components" +import { InfiniteScroll } from "antd-mobile" + +import "./index.less" + +export default class PostsFeed extends React.Component { + state = { + loading: true, + skipStep: 0, + lastLength: 0, + posts: [], + } + + api = window.app.request + + componentDidMount = async () => { + this.toogleLoading(true) + + await this.fetchPosts() + + window.app.ws.listen(`new.post`, (data) => { + this.addPost(data) + }) + + this.toogleLoading(false) + } + + toogleLoading = (to) => { + this.setState({ loading: to ?? !this.state.loading }) + } + + addPost = (post) => { + this.setState({ + posts: [post, ...this.state.posts], + }) + } + + fetchPosts = async () => { + const posts = await this.api.get.feed(undefined, { + user_id: this.props.fromUserId, + feedSkip: this.state.skipStep, + }).catch(error => { + console.error(error) + antd.message.error(error) + + return false + }) + + if (posts) { + console.log(posts) + this.setState({ posts: [...posts, ...this.state.posts,], lastLength: posts.length }) + } + } + + hasMore = () => { + return this.state.posts.length < this.state.lastLength + } + + loadMore = async () => { + await this.setState({ skipStep: this.state.skipStep + 1 }) + await this.fetchPosts() + } + + render() { + if (this.state.loading) { + return + } + + return
+
+ { + this.state.posts.map(post => { + return + }) + } + + +
Loading more...
+
+
+
+ } +} \ No newline at end of file diff --git a/packages/app/src/components/PostsFeed/index.less b/packages/app/src/components/PostsFeed/index.less new file mode 100644 index 00000000..9fd2f97c --- /dev/null +++ b/packages/app/src/components/PostsFeed/index.less @@ -0,0 +1,22 @@ +.postsFeed { + display : flex; + flex-direction : column; + align-items : center; + justify-content: center; + + width: 100%; + + .wrapper { + display : flex; + flex-direction : column; + align-items : center; + justify-content: center; + + width : 100%; + max-width: 40vw; + + >div { + margin-bottom: 15px; + } + } +} \ No newline at end of file diff --git a/packages/app/src/components/index.js b/packages/app/src/components/index.js index 2109093e..3cf28c69 100644 --- a/packages/app/src/components/index.js +++ b/packages/app/src/components/index.js @@ -22,6 +22,7 @@ export { default as Navigation } from "./Navigation" export { default as ImageUploader } from "./ImageUploader" export { default as ImageViewer } from "./ImageViewer" +export { default as PostsFeed } from "./PostsFeed" export { default as LikeButton } from "./LikeButton" export { default as PostCard } from "./PostCard" export { default as PostCreator } from "./PostCreator"