diff --git a/client/src/components/MovieCard.jsx b/client/src/components/MovieCard.jsx index 85d7145..cc25e64 100644 --- a/client/src/components/MovieCard.jsx +++ b/client/src/components/MovieCard.jsx @@ -2,111 +2,130 @@ import { TiHeartFullOutline, TiBookmark, TiEject, TiArrowRight, TiStarFullOutlin import { LuClock } from "react-icons/lu"; import { MdDateRange } from "react-icons/md"; import { MdLanguage } from "react-icons/md"; -import {useState, useCallback, useEffect} from 'react'; +import { useState } from 'react'; import axios from 'axios'; -export default function MovieCard({userId, movie_id, title, poster, year, rate, popularity, trailer, lang }) { +export default function MovieCard({userId, movie_id, title, poster, year, rate, popularity, trailer, language }) { const [liked, setLiked] = useState(false) const [save, setSave] = useState(false); - const [order, setOrder] = useState(false); - const [userData, setUserData] = useState({}); - const handleLike = useCallback(async () => { - setLiked(prevState => !prevState); - await toggleLike(userId, movie_id, 'liked'); - }, [userId, movie_id]); - const handleSave = useCallback(async () => { - setSave(prevState => !prevState); - await toggleSave(userId, movie_id, true); - }, [userId, movie_id]); - - const handleOrder = useCallback(async () => { - setOrder(prevState => !prevState); - // Implement order logic here - }, []); - - const toggleLike = async (userId, movie_id, liked) => { + const toggleLike = async (movie_id, liked) => { try { const apiUrl = process.env.REACT_APP_API_URL; const token = localStorage.getItem('_token'); - const dataToSend = { - user_id: userId, - movie_id: movie_id, - like: true - }; - - if (token) { - const userLikedResponse = await axios.get(`${apiUrl}/${userId}/liked`, { + const userId = localStorage.getItem('_user_id'); + + // Check if user already liked this movie + const userLikedResponse = await axios.get(`${apiUrl}/${userId}/user_movies`, { + headers: { + Authorization: `Bearer ${token}`, + } + }); + console.log("9bel:", userLikedResponse); + const likedMovie = userLikedResponse.data.find(movie => movie && movie.movie_id && movie.movie_id === movie_id); + console.log("likedMovie", likedMovie); + + if (likedMovie) { + // Update existing like record + const dataToSend = { + user_id: userId, + movie_id: movie_id, + like: !liked // Update like value to the opposite + }; + + const updateResponse = await axios.put(`${apiUrl}/${userId}/liked/${likedMovie.id}`, dataToSend, { headers: { Authorization: `Bearer ${token}`, } - }) - console.log("liked movies:", userLikedResponse); + }); + + if (updateResponse.status === 200) { + + console.log("Like status updated (PUT):", updateResponse); + setLiked(!liked); // Toggle the local state + } else { + console.error("Error updating like:", updateResponse.data); + } + } else { + // Send new like request + const dataToSend = { + user_id: userId, + movie_id: movie_id, + like: true + }; const response = await axios.post(`${apiUrl}/${userId}/liked`, dataToSend, { headers: { Authorization: `Bearer ${token}`, } }); - console.log("Like response:", response.data); + console.log("New like added (POST):", response.data); + setLiked(true); } } catch (error) { console.error("Error toggling like:", error.response?.data || error.message); } }; - const toggleSave = async (userId, movie_id, save) => { + const toggleSave = async (movie_id, saved) => { try { const apiUrl = process.env.REACT_APP_API_URL; const token = localStorage.getItem('_token'); - const dataToSend = { - user_id: userId, - movie_id: movie_id, - save: true - }; + const userId = localStorage.getItem('_user_id'); + + // Check if user already liked this movie + const userSavedResponse = await axios.get(`${apiUrl}/${userId}/user_movies`, { + headers: { + Authorization: `Bearer ${token}`, + } + }); + console.log("9bel:", userSavedResponse); + const savedMovie = userSavedResponse.data.find(movie => movie && movie.movie_id && movie.movie_id === movie_id); + console.log("savedMovie", savedMovie); + + if (savedMovie) { + // Update existing like record + const dataToSend = { + user_id: userId, + movie_id: movie_id, + save: !save // Update like value to the opposite + }; + + const updateResponse = await axios.put(`${apiUrl}/${userId}/save/${savedMovie.id}`, dataToSend, { + headers: { + Authorization: `Bearer ${token}`, + } + }); + + if (updateResponse.status === 200) { + + console.log("Save status updated (PUT):", updateResponse); + setSave(!save); // Toggle the local state + } else { + console.error("Error updating save:", updateResponse.data); + } + } else { + // Send new like request + const dataToSend = { + user_id: userId, + movie_id: movie_id, + save: true + }; - if (token) { const response = await axios.post(`${apiUrl}/${userId}/save`, dataToSend, { headers: { Authorization: `Bearer ${token}`, } }); - console.log("Save response:", response.data); + console.log("New like added (POST):", response.data); + setSave(true); } } catch (error) { console.error("Error toggling save:", error.response?.data || error.message); } }; - - - // const toggleUserMovie = async (userId, movie_id, action) => { - // try { - // const apiUrl = process.env.REACT_APP_API_URL; - // const token = localStorage.getItem('_token'); - // const dataToSend = { - // user_id: userId, - // movie_id: movie_id, - // save: action === true ? save : false, - // like: action === true ? liked : false - // }; - // - // if (token) { - // const response = await axios.post(`${apiUrl}/${userId}/${action}`, dataToSend, { - // headers: { - // Authorization: `Bearer ${token}`, - // } - // }); - // } - // console.log("data", dataToSend); - // } catch (error) { - // console.error(error.response?.data || error.message); - // } - // }; - - - return
Language
-{lang || 'Nolang'}
+{language || 'Nolang'}