Feed.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import OwnerHeader from "./OwnerHeader";
  2. import {useEffect, useState} from "react";
  3. import styled from "styled-components";
  4. import {gqlPostsFeed} from "../shared/services&utilits/gqlRequest";
  5. import PostFeed from "./PostFeed";
  6. const FeedContainer = styled.div`
  7. margin-top: 30px;
  8. display: flex;
  9. flex-direction: column;
  10. align-items: center;
  11. `
  12. const FeedPostWrapper = styled.div`
  13. margin-bottom: 20px;
  14. width: 400px;
  15. box-shadow: rgba(67, 71, 85, 0.27) 0px 0px 0.25em, rgba(90, 125, 188, 0.05) 0px 0.25em 1em;
  16. `
  17. function Feed() {
  18. const [listIdPosts, setListIdPosts] = useState([]);
  19. const [shouldLoadMore, setShoulLoadMore] = useState(false);
  20. useEffect(() => {
  21. getData();
  22. window.addEventListener('scroll', scrollHandler);
  23. return () => {
  24. window.removeEventListener('scroll', scrollHandler);
  25. };
  26. }, [])
  27. useEffect(()=>{
  28. if (shouldLoadMore){
  29. getData();
  30. setShoulLoadMore(false)
  31. }
  32. })
  33. function getData() {
  34. gqlPostsFeed(listIdPosts.length).then((res)=>res.json()).then((response) => {
  35. setListIdPosts([...listIdPosts, ...response.data.PostFind])
  36. })
  37. }
  38. function scrollHandler() {
  39. const documentHeight = document.body.offsetHeight;
  40. const screenHeight = window.innerHeight;
  41. const scroll = window.scrollY;
  42. const currentScrollPosition = screenHeight + scroll;
  43. if (currentScrollPosition >= documentHeight - 200) {
  44. setShoulLoadMore(true)
  45. }
  46. }
  47. return (<>
  48. <OwnerHeader/>
  49. <FeedContainer>
  50. {listIdPosts && listIdPosts.map((post, index) =>
  51. <FeedPostWrapper key={index}>
  52. <PostFeed post={post} key={index}/>
  53. </FeedPostWrapper>
  54. )}
  55. </FeedContainer>
  56. </>
  57. )
  58. }
  59. export default Feed;