Like.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Avatar, Col, Row } from 'antd'
  2. import { ConstructorModal } from '../helpers'
  3. import { Link } from 'react-router-dom'
  4. import user from '../materials/user.png'
  5. import { HeartOutlined, HeartFilled } from '@ant-design/icons'
  6. import React, { useState } from 'react'
  7. import { LinkToUser } from './LinkToUser'
  8. import {
  9. actionChangeLike
  10. } from '../redux/saga'
  11. import { connect } from 'react-redux'
  12. export const Likes = ({ likes }) => {
  13. return (
  14. <>
  15. <div className="Modal">
  16. {likes &&
  17. likes?.map(({owner:{_id,login, avatar}}) => (
  18. <LinkToUser _id={_id} login={login} avatar={avatar} size={50} padding={'0px'} />
  19. ))}
  20. </div>
  21. </>
  22. )
  23. }
  24. export const Like = ({
  25. my_Id,
  26. postId,
  27. addLike,
  28. deleteLike,
  29. likes = [],
  30. changeLike,
  31. children,
  32. }) => {
  33. const likeId = likes.find((like) => like?.owner?._id === my_Id)?._id
  34. // const changeLike = () =>
  35. // likeId ? deleteLike(likeId, postId) : addLike(postId)
  36. console.log('like id in component', likeId)
  37. const [isModalVisible, setIsModalVisible] = useState(false)
  38. const showModal = () => {
  39. setIsModalVisible(true)
  40. }
  41. return (
  42. <>
  43. <div style={{ display: 'flex' }}>
  44. <h3 onClick={() => changeLike(likeId, postId)}>
  45. {likeId ? (
  46. <HeartFilled
  47. style={{ cursor: 'pointer', fontSize: 'xx-large', color: 'red' }}
  48. />
  49. ) : (
  50. <HeartOutlined
  51. style={{ cursor: 'pointer', fontSize: 'xx-large' }}
  52. />
  53. )}
  54. </h3>
  55. {likes.length ? (
  56. <h3 style={{ cursor: 'pointer', paddingLeft: 8 }} onClick={showModal}>
  57. {' '}
  58. {likes.length} likes
  59. </h3>
  60. ) : (
  61. <h3 style={{ cursor: 'pointer', paddingLeft: 8 }}> 0 likes</h3>
  62. )}
  63. </div>
  64. <ConstructorModal
  65. title={'Likes'}
  66. isModalVisible={isModalVisible}
  67. setIsModalVisible={setIsModalVisible}
  68. >
  69. <Likes likes={likes} />
  70. </ConstructorModal>
  71. </>
  72. )
  73. }
  74. const AllLikeComponent = ({ my_Id, likes,changeLike, postId }) => (
  75. <Like
  76. my_Id={my_Id}
  77. likes={likes}
  78. postId={postId}
  79. changeLike={changeLike}
  80. >
  81. <Likes likes={likes} />
  82. </Like>
  83. )
  84. export const CLike = connect(
  85. (state) => ({
  86. my_Id: state.auth?.payload?.sub?.id || '',
  87. }),
  88. {
  89. changeLike: actionChangeLike
  90. },
  91. )(AllLikeComponent)