index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { actionOnePost } from '../../actions/query/postQuery'
  2. import { actionFullExplorePostsTypeSaga } from '../../actions/typeSaga/exploreTypesSaga'
  3. import { Row, Col } from 'antd'
  4. import { Card } from '../../components/post/PostCard'
  5. import React, { useEffect, useState } from 'react'
  6. import { connect } from 'react-redux'
  7. import { actionClearExplorePostsType } from '../../actions/types/exploreTypes'
  8. import load from '../../materials/load.gif'
  9. const ExplorePosts = ({
  10. explorePosts = [],
  11. onPost,
  12. onClearExplore,
  13. onExlorePosts,
  14. explorePostsPromise,
  15. }) => {
  16. const [checkScroll, setScroll] = useState(true)
  17. useEffect(() => {
  18. if (checkScroll) {
  19. onExlorePosts()
  20. }
  21. setScroll(false)
  22. }, [checkScroll])
  23. useEffect(() => {
  24. document.addEventListener('scroll', scrollHandler)
  25. return () => {
  26. document.removeEventListener('scroll', scrollHandler)
  27. onClearExplore()
  28. }
  29. }, [])
  30. useEffect(() => {
  31. document.addEventListener('scroll', scrollHandler)
  32. }, [explorePosts.length])
  33. const scrollHandler = (e) => {
  34. if (
  35. e.target.documentElement.scrollHeight -
  36. (e.target.documentElement.scrollTop + window.innerHeight) <
  37. 200
  38. ) {
  39. setScroll(true)
  40. document.removeEventListener('scroll', scrollHandler)
  41. }
  42. }
  43. return (
  44. <>
  45. <Row>
  46. <Col
  47. xl={{ offset: 4, span: 18 }}
  48. lg={{ offset: 2, span: 20 }}
  49. md={{ offset: 2, span: 20 }}
  50. sm={{ offset: 2, span: 22 }}
  51. xs={{offset:1, span: 22 }}
  52. // span={18} offset={4}
  53. >
  54. <div className="Explore">
  55. {(explorePosts || [])?.map((item) => (
  56. <Card post={item} onPost={onPost} />
  57. ))}
  58. </div>
  59. </Col>
  60. </Row>
  61. {explorePostsPromise?.status == 'PENDING' && (
  62. <img className="Preloader" src={load} width="100" height="100" />
  63. )}
  64. </>
  65. )
  66. }
  67. export const CExplorePosts = connect(
  68. (state) => ({
  69. my_Id: state.auth?.payload?.sub?.id || '',
  70. countAllPostsUser: state.promise?.countAllPostsUser?.payload,
  71. explorePosts: state.explore?.explorePosts,
  72. explorePostsPromise: state.promise?.explorePosts,
  73. explorePostsCount: state.explore?.explorePostsCount,
  74. }),
  75. {
  76. onExlorePosts: actionFullExplorePostsTypeSaga,
  77. onPost: actionOnePost,
  78. onClearExplore: actionClearExplorePostsType,
  79. },
  80. )(ExplorePosts)