Skip to content

Commit

Permalink
Merge pull request #191 from UB-ES-2023-A2/hotfix/not-like
Browse files Browse the repository at this point in the history
feat: dont like if it is your review
  • Loading branch information
ivanmansilla authored Nov 24, 2023
2 parents 5e19f82 + e37a91f commit c8307e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/components/LikesReview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { faThumbsUp, faHeart } from "@fortawesome/free-solid-svg-icons";
import { useAuth } from "./AuthContext";


function LikesReview({ recipeId, reviewId, initialLikes, likedBy, reloadReviews }) {
function LikesReview({ reviewUsername, recipeId, reviewId, initialLikes, likedBy, reloadReviews }) {
const [likes, setLikes] = useState(initialLikes);
const [hasLiked, setHasLiked] = useState(false);
const currentUserUsername = localStorage.getItem('currentUser');
const [hasLikedByUser, setHasLikedByUser] = useState(likedBy.includes(currentUserUsername));
const { token } = useAuth();
const isLogged = window.localStorage.getItem("logged");
const isOwnerReview = currentUserUsername === reviewUsername;

const handleLikeClick = async () => {
if(isLogged === 'true'){
if(isLogged === 'true' && !isOwnerReview){
if (hasLiked || hasLikedByUser) {
likes > 0 ? setLikes(likes - 1) : setLikes(0);
setHasLiked(false);
Expand Down Expand Up @@ -65,7 +66,7 @@ function LikesReview({ recipeId, reviewId, initialLikes, likedBy, reloadReviews
reloadReviews()}
};

const cursorStyle = isLogged === 'true' ? { cursor: "pointer" } : { cursor: "not-allowed" };
const cursorStyle = isLogged === 'true' && !isOwnerReview ? { cursor: "pointer" } : { cursor: "not-allowed" };

return (
<Row>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Reviews.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function Reviews(props) {
const [showModalImage, setShowModalImage] = useState(false);
const [selectedImage, setSelectedImage] = useState(null);
const isLogged = window.localStorage.getItem("logged");
const currentUserUsername = localStorage.getItem('currentUser');


useEffect(() => {
fetch(`${process.env.REACT_APP_API_URL}/review/${id}`)
Expand Down Expand Up @@ -107,12 +109,13 @@ function Reviews(props) {
<Col sm={12}>
<div className="d-flex align-items-cente mx-1">
<LikesReview
reviewUsername={review.username}
recipeId={id}
reviewId={review._id}
initialLikes={review.likes || 0}
likedBy={review.liked_by}
reloadReviews={reloadReviews}
/>
/>
</div>
</Col>
</Row>
Expand Down

0 comments on commit c8307e4

Please sign in to comment.