-
Notifications
You must be signed in to change notification settings - Fork 0
/
LikeReview.jsx
75 lines (67 loc) · 2.01 KB
/
LikeReview.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useContext, useState } from 'react'
import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder'
import FavoriteIcon from '@material-ui/icons/Favorite'
import IconButton from '@material-ui/core/IconButton'
import restaurantFinder from '../API/restaurantFinder'
import { RestaurantContext } from '../context/RestaurantsContext'
const LikeReview = ({ reviewId, id, bool }) => {
const { userId, likes, setLikes, setAllLikesByUser } =
useContext(RestaurantContext)
const [likedCurr, setLikedCurr] = useState(false)
const handleLikeClick = async (e, reviewId) => {
if (!likedCurr) {
try {
const likesRes = await restaurantFinder.post('/review/likes', {
userId,
reviewId,
restaurantId: id,
})
// console.log(response)
const response = likesRes.data.data.count
setLikes(response)
} catch (err) {
console.error(err.message)
}
setLikedCurr(!likedCurr)
} else {
try {
const likesRes = await restaurantFinder.post('/review/dislikes', {
userId,
reviewId,
restaurantId: id,
})
// console.log(response)
const response = likesRes.data.data.count
setLikes(response)
const likesByUserId = await restaurantFinder.get(
`/review/all-likes-by-user/${userId}`
)
const likesByReview = likesByUserId.data.data.likes.map((ele) => {
return ele.review_id
})
setAllLikesByUser(likesByReview)
bool = false
} catch (err) {
console.error(err.message)
}
setLikedCurr(!likedCurr)
}
}
return (
<>
<span className="col">
<IconButton
aria-label="delete"
onClick={(e) => handleLikeClick(e, reviewId)}
>
{likedCurr || bool ? (
<FavoriteIcon color="secondary" />
) : (
<FavoriteBorderIcon />
)}
</IconButton>
</span>
</>
)
}
export default LikeReview