@@ -6,52 +6,24 @@ import PostHeader from './PostHeader';
6
6
import useFetch from '../../../CustomHooks/useFetch' ; // Import useFetch
7
7
import { getOptions } from '../../../options' ; // Import getOptions
8
8
import CommentSection from './PostActions/comment/CommentSection' ;
9
+ import { useDispatch , useSelector } from 'react-redux' ;
10
+ import { fetchLikesForPost } from '../../../store/posts/fetchLikesForPost' ;
9
11
10
12
function Post ( { post } ) {
11
- const [ usersLikesThisPost , setUsersLikesThisPost ] = useState ( [ ] ) ;
12
- const [ usersCommentsOnPost , setUsersCommentsOnPost ] = useState ( [ ] ) ;
13
-
14
- const { fetchApi : getLikesData } = useFetch ( `http://localhost:8000/api/posts/1/comments/likes` , getOptions ) ;
15
- const { fetchApi : getCommentsData } = useFetch ( `http://localhost:8000/api/posts/1/comments` , getOptions ) ;
16
-
17
-
18
- const fetchComments = async ( ) => {
19
- try {
20
- const resData = await getCommentsData ( ) ;
21
- console . log ( resData )
22
- if ( resData . ok ) {
23
- setUsersCommentsOnPost ( resData . data . comments ) ;
24
- }
25
-
26
- } catch ( error ) {
27
- console . error ( 'Error fetching comments:' , error ) ;
28
- }
29
- } ;
30
- useEffect ( ( ) => {
31
- const fetchLikes = async ( ) => {
32
- try {
33
- const resData = await getLikesData ( ) ;
34
- if ( resData . ok ) {
35
- setUsersLikesThisPost ( resData . data . likeData ) ;
36
- }
37
- } catch ( error ) {
38
- console . error ( 'Error fetching likes:' , error ) ;
39
- }
40
- } ;
41
-
42
-
43
-
44
- fetchLikes ( ) ;
45
- fetchComments ( ) ;
46
- } , [ ] ) ;
47
-
13
+ const dispatch = useDispatch ( )
14
+ const likesDataForPost = useSelector ( state => state . posts ) . postsLikesData
15
+ const existedPost = likesDataForPost . find ( postData => {
16
+ return post . id == postData . postid } )
17
+ useEffect ( ( ) => {
18
+ dispatch ( fetchLikesForPost ( post . id ) )
19
+ } , [ ] )
48
20
return (
49
21
< div className = 'py-[30px] border-t-[6px] border-t-[rgba(235,235,235,1)] font-medium' >
50
22
< PostHeader post = { post } />
51
23
< div >
52
24
< PostContent post = { post } />
53
- < PostDetails post = { post } />
54
- < PostActions post = { post } usersLikesThisPost = { usersLikesThisPost } />
25
+ < PostDetails post = { post } />
26
+ < PostActions post = { post } usersLikesThisPost = { existedPost ? existedPost . likesData : [ ] } />
55
27
</ div >
56
28
57
29
{ /* {usersCommentsOnPost.length>0&& <CommentSection fetchedComments={usersCommentsOnPost} /> } */ }
0 commit comments