Skip to content

Commit 5941c40

Browse files
committed
add likes counts
1 parent 44c9ca4 commit 5941c40

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/components/PostForm.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ const PostForm =()=>{
6969
title:dataInputs.title,
7070
country:dataInputs.country,
7171
state:dataInputs.state,
72-
days:dataInputs.day
72+
days:dataInputs.day,
73+
likes:0
7374
})
7475
.then((res)=>{
7576
console.log("data received")

src/components/PostList.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import React from 'react';
2+
import axios from 'axios';
23

34
import history from './../history';
45
const PostList = ({posts})=>{
5-
6+
const like_post=(e,post_id)=>{
7+
e.preventDefault();
8+
9+
console.log("new",posts[0].id,post_id)
10+
for(let i=0; i<posts.length; i++){
11+
if(post_id == posts[i].id){
12+
console.log("yes")
13+
posts[i].likes+=1
14+
axios.patch(`http://localhost:5000/travelposts/${post_id}`,{
15+
likes_count:posts[i].likes
16+
})
17+
}
18+
}
19+
20+
}
21+
622
return (
723
<div>
824
<h1>post</h1>
@@ -15,8 +31,12 @@ const PostList = ({posts})=>{
1531
{post.days.map((days,i)=>{
1632

1733
if(i===0){
18-
return (<img onClick={()=>history.push(`/postCard/${post.id}`)} height="200" width="300" src={days.photosList[0].photo} alt="post cover picture"/>)
34+
return (
35+
<div>
36+
<img onClick={()=>history.push(`/postCard/${post.id}`)} height="200" width="300" src={days.photosList[0].photo} alt="post cover picture"/>
37+
</div>)
1938
}})}
39+
<p onClick={(e)=>like_post(e,post.id)}>💗{post.likes}</p>
2040
<p onClick={()=>history.push(`/postCard/${post.id}`)}>{post.title}</p>
2141
{post.state}-{post.country}
2242
</div>

0 commit comments

Comments
 (0)