diff --git a/src/components/BookmarkButton/BookMarkButton.style.ts b/src/components/BookmarkButton/BookMarkButton.style.ts new file mode 100644 index 0000000..45f8324 --- /dev/null +++ b/src/components/BookmarkButton/BookMarkButton.style.ts @@ -0,0 +1,10 @@ +import styled from "@emotion/styled"; + +export const StyledBookmarkButton = styled.button` + position: relative; + left: 4px; + color: white; + cursor: pointer; + background: none; + border: none; +`; diff --git a/src/components/BookmarkButton/BookmarkButton.tsx b/src/components/BookmarkButton/BookmarkButton.tsx new file mode 100644 index 0000000..aa8c736 --- /dev/null +++ b/src/components/BookmarkButton/BookmarkButton.tsx @@ -0,0 +1,55 @@ +import { Tooltip } from "@mantine/core"; +import { useContext, useRef, useState } from "react"; +import { FaBookmark, FaRegBookmark } from "react-icons/fa"; +import { Movie, MovieContext } from "../../contexts/MovieContext"; +import { StyledBookmarkButton } from "./BookMarkButton.style"; + +function BookmarkButton(movie: Movie) { + const { bookmarkedMovies, setBookmarkedMovies } = useContext(MovieContext); + const [opened, setOpened] = useState(false); + const [tooltipText, setTooltipText] = useState(""); + const tooltipTimer = useRef(null); + + const handleBookmarkClick = (e: React.MouseEvent) => { + e.preventDefault(); + // Check if the movie is already bookmarked + const isBookmarked = bookmarkedMovies.some(m => m.title === movie.title); + if (!isBookmarked) { + setTooltipText("Added to bookmarks!"); + } else { + setTooltipText("Removed from bookmarks!"); + } + + setOpened(true); + + if (tooltipTimer.current) { + clearTimeout(tooltipTimer.current); + } + tooltipTimer.current = setTimeout(() => { + setOpened(false); + }, 1200); + + // If it's bookmarked, remove it; otherwise, add it + const updatedBookmarkedMovies = isBookmarked + ? bookmarkedMovies.filter(m => m.title !== movie.title) + : [...bookmarkedMovies, movie]; + + // Update the bookmarkedMovies state with the new array + setBookmarkedMovies(updatedBookmarkedMovies); + }; + + return ( + + + {/* If movie is bookmarked, show solid icon, else show outlined icon. */} + {bookmarkedMovies.some(m => m.title === movie.title) ? ( + + ) : ( + + )} + + + ); +} + +export default BookmarkButton; diff --git a/src/components/Header/Header.style.tsx b/src/components/Header/Header.style.ts similarity index 100% rename from src/components/Header/Header.style.tsx rename to src/components/Header/Header.style.ts diff --git a/src/components/MovieCard/MovieCard.style.ts b/src/components/MovieCard/MovieCard.style.ts index 45364a3..15b1445 100644 --- a/src/components/MovieCard/MovieCard.style.ts +++ b/src/components/MovieCard/MovieCard.style.ts @@ -22,11 +22,4 @@ export const StyledMovieCard = styled.div` margin-top: 10px; gap: 0.3rem; } - - .bookmark-box { - position: relative; - left: 4px; - color: white; - cursor: pointer; - } `; diff --git a/src/components/MovieCard/MovieCard.tsx b/src/components/MovieCard/MovieCard.tsx index dbf265c..ab82e1b 100644 --- a/src/components/MovieCard/MovieCard.tsx +++ b/src/components/MovieCard/MovieCard.tsx @@ -1,61 +1,16 @@ -import { Box, Image, Text, Tooltip } from "@mantine/core"; -import { useContext, useRef, useState } from "react"; -import { FaBookmark, FaRegBookmark } from "react-icons/fa"; +import { Box, Image, Text } from "@mantine/core"; import { Link } from "react-router-dom"; -import { MovieContext } from "../../contexts/MovieContext"; +import { Movie } from "../../contexts/MovieContext"; import { titleToSlug } from "../../pages/MovieViewPage"; +import BookmarkButton from "../BookmarkButton/BookmarkButton"; import { StyledMovieCard } from "./MovieCard.style"; -export interface MovieProps { - title: string; - year: number; - rating: string; - actors: string[]; - genre: string; - synopsis: string; - thumbnail: string; - isTrending?: boolean; -} - -function MovieCard(movie: MovieProps) { - const { bookmarkedMovies, setBookmarkedMovies } = useContext(MovieContext); - const [opened, setOpened] = useState(false); - const [tooltipText, setTooltipText] = useState(""); - const tooltipTimer = useRef(null); - +function MovieCard(movie: Movie) { const handleImageError = (event: React.SyntheticEvent) => { // Replace the failed image with the placeholder image event.currentTarget.src = "./404.png"; }; - const handleBookmarkClick = (e: React.SyntheticEvent) => { - e.preventDefault(); - // Check if the movie is already bookmarked - const isBookmarked = bookmarkedMovies.some(m => m.title === movie.title); - if (!isBookmarked) { - setTooltipText("Added to bookmarks!"); - } else { - setTooltipText("Removed from bookmarks!"); - } - - setOpened(true); - - if (tooltipTimer.current) { - clearTimeout(tooltipTimer.current); - } - tooltipTimer.current = setTimeout(() => { - setOpened(false); - }, 1500); - - // If it's bookmarked, remove it; otherwise, add it - const updatedBookmarkedMovies = isBookmarked - ? bookmarkedMovies.filter(m => m.title !== movie.title) - : [...bookmarkedMovies, movie]; - - // Update the bookmarkedMovies state with the new array - setBookmarkedMovies(updatedBookmarkedMovies); - }; - return ( @@ -76,20 +31,7 @@ function MovieCard(movie: MovieProps) { {movie.rating} - - - {/* If movie is bookmarked, show a text with text "OO" else show FaRegBookmark icon. */} - {bookmarkedMovies.some(m => m.title === movie.title) ? ( - - ) : ( - - )} - - + ); diff --git a/src/components/MovieView/MovieView.tsx b/src/components/MovieView/MovieView.tsx index bcf3809..2192b05 100644 --- a/src/components/MovieView/MovieView.tsx +++ b/src/components/MovieView/MovieView.tsx @@ -1,8 +1,7 @@ -import { useContext } from "react"; -import { FaBookmark, FaRegBookmark } from "react-icons/fa"; -import { Movie, MovieContext } from "../../contexts/MovieContext"; import { Box, Image, Text, Title } from "@mantine/core"; import { useState } from "react"; +import { Movie } from "../../contexts/MovieContext"; +import BookmarkButton from "../BookmarkButton/BookmarkButton"; import { StyledMovieView } from "./MovieView.style"; interface Props { @@ -17,23 +16,6 @@ function MovieView({ movie }: Props) { }; const { title, genre, synopsis, year, rating, actors } = movie; - const { bookmarkedMovies, setBookmarkedMovies } = useContext(MovieContext); - - const handleBookmarkClick = (e: React.SyntheticEvent) => { - e.preventDefault(); - - // Check if the movie is already bookmarked - const isBookmarked = bookmarkedMovies.some(m => m.title === movie.title); - - // If it's bookmarked, remove it; otherwise, add it - const updatedBookmarkedMovies = isBookmarked - ? bookmarkedMovies.filter(m => m.title !== movie.title) - : [...bookmarkedMovies, movie]; - - // Update the bookmarkedMovies state with the new array - setBookmarkedMovies(updatedBookmarkedMovies); - }; - return ( {title} @@ -49,13 +31,7 @@ function MovieView({ movie }: Props) { {rating} - - {bookmarkedMovies.some(m => m.title === movie.title) ? ( - - ) : ( - - )} - + {title} @@ -70,7 +46,6 @@ function MovieView({ movie }: Props) { ))} - Genre: {genre}