diff --git a/client/src/api/history.api.js b/client/src/api/history.api.js
index c1c34c2..4134769 100644
--- a/client/src/api/history.api.js
+++ b/client/src/api/history.api.js
@@ -15,4 +15,14 @@ export const getMovieHistory = async (movieId) => {
export const getHistoryBanner = async () => {
const res = await axiosInstance.get('/history');
return res.data;
+}
+
+export const addWatchedMovie = async (title) => {
+ const res = await axiosInstance.post('/history', { title });
+ return res;
+}
+
+export const removeMovieFromHistory = async (movieId) => {
+ const res = await axiosInstance.delete(`/history/movie/${movieId}`);
+ return res;
}
\ No newline at end of file
diff --git a/client/src/api/watchList.api.js b/client/src/api/watchList.api.js
index eb17523..86e35ab 100644
--- a/client/src/api/watchList.api.js
+++ b/client/src/api/watchList.api.js
@@ -3,4 +3,14 @@ import axiosInstance from "./axiosInstance"
export const getIsMovieWatchListed = async (movieId) => {
const res = await axiosInstance.get(`/watchlist/movie/${movieId}`);
return res.data;
+}
+
+export const addMovieToWatchList = async (title) => {
+ const res = await axiosInstance.post('/watchlist', { title });
+ return res;
+}
+
+export const removeMovieFromWatchList = async (movieId) => {
+ const res = await axiosInstance.delete(`/watchlist/movie/${movieId}`);
+ return res;
}
\ No newline at end of file
diff --git a/client/src/pages/MoviePage.jsx b/client/src/pages/MoviePage.jsx
index b5c48ed..27f1c6e 100644
--- a/client/src/pages/MoviePage.jsx
+++ b/client/src/pages/MoviePage.jsx
@@ -1,9 +1,11 @@
import { useEffect, useState } from "react";
+import { useParams } from "react-router-dom";
import { motion } from "framer-motion";
import { getMovieById } from "../api/movie.api";
import Loader from "../components/ui/Loader";
-import { getMovieHistory } from "../api/history.api";
-import { getIsMovieWatchListed } from "../api/watchList.api";
+import { addWatchedMovie, getMovieHistory, removeMovieFromHistory } from "../api/history.api";
+import { addMovieToWatchList, getIsMovieWatchListed, removeMovieFromWatchList } from "../api/watchList.api";
+import { BookmarkIcon, CheckCircleIcon, ClockIcon } from "@heroicons/react/24/solid";
export default function MoviePage() {
const [movieData, setMovieData] = useState(null);
@@ -12,7 +14,7 @@ export default function MoviePage() {
const [inList, setInList] = useState(false);
const [reviewOpen, setReviewOpen] = useState(false);
- const movieId = window.location.pathname.split("/movies/")[1];
+ const { movieId } = useParams();
useEffect(() => {
if (!movieId) return;
@@ -23,9 +25,11 @@ export default function MoviePage() {
getMovieHistory(movieId),
getIsMovieWatchListed(movieId)
]);
+
setMovieData(res[0]);
setWatched(res[1].data != null);
setInList(res[2].data.watchListed);
+
} catch (error) {
console.error("Error fetching movie: ", error);
} finally {
@@ -35,6 +39,50 @@ export default function MoviePage() {
fetchMovie();
}, [movieId]);
+ const handleWatchedToggle = async () => {
+ try {
+ if (watched) {
+ const res = await removeMovieFromHistory(movieData._id);
+ if (res.status === 200) {
+ setWatched(false);
+ }
+ } else {
+ const res = await addWatchedMovie(movieData.title);
+
+ if (res.status === 201) {
+ if (inList) setInList(false);
+
+ setWatched(true);
+
+ setMovieData((prev) => ({
+ ...prev,
+ ...res.data
+ }));
+ }
+ }
+ } catch (error) {
+ console.error("Error updating watch status: ", error);
+ }
+ }
+
+ const handleWatchListToggle = async () => {
+ try {
+ if (inList) {
+ const res = await removeMovieFromWatchList(movieData._id);
+ if (res.status === 200) {
+ setInList(false);
+ }
+ } else {
+ const res = await addMovieToWatchList(movieData.title);
+ if (res.status === 201) {
+ setInList(true);
+ }
+ }
+ } catch (error) {
+ console.error("Error updating watchlist status: ", error);
+ }
+ }
+
if (loading) return ;
const releaseYear = movieData ? new Date(movieData.releaseDate).getFullYear() : "";
@@ -108,7 +156,6 @@ export default function MoviePage() {
- {/* Action Buttons */}
setWatched(!watched)}
- activeClass="bg-(--interaction-color) text-white border-(--interaction-color)"
+ onClick={handleWatchedToggle}
+ activeClass="bg-amber-400 text-white border-amber-400"
label={watched ? "Watched" : "Mark as watched"}
- icon={watched ? "✓" : "○"}
+ trueIcon={}
+ falseIcon={}
/>
setInList(!inList)}
+ onClick={handleWatchListToggle}
activeClass="bg-(--secondary-color) text-white border-(--secondary-color)"
label={inList ? "In Watchlist" : "Add to Watchlist"}
- icon="+"
+ trueIcon={}
+ falseIcon={}
/>