Added: Favourite list feature
This commit is contained in:
parent
7d91c7998c
commit
40ed26f5af
|
@ -2,10 +2,10 @@ import React from "react";
|
||||||
import Star from "./Star";
|
import Star from "./Star";
|
||||||
|
|
||||||
const Card = ({ movie, favouriteList, setFavouriteList }) => {
|
const Card = ({ movie, favouriteList, setFavouriteList }) => {
|
||||||
const { Title, Year, imdbID, Poster } = movie;
|
const { Title, Year, Poster } = movie;
|
||||||
return (
|
return (
|
||||||
<div className="relative h-[30vh] w-[10vw] overflow-hidden border rounded-lg border-quadnary bg-secondary">
|
<div className="relative h-[30vh] w-[10vw] overflow-hidden border rounded-lg border-quadnary bg-secondary">
|
||||||
{favouriteList?.includes(imdbID) ? (
|
{favouriteList?.includes(movie) ? (
|
||||||
<Star
|
<Star
|
||||||
fill="#FFD700"
|
fill="#FFD700"
|
||||||
favouriteList={favouriteList}
|
favouriteList={favouriteList}
|
||||||
|
|
|
@ -22,14 +22,12 @@ const Star = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFavouriteList = () => {
|
const handleFavouriteList = () => {
|
||||||
favouriteList?.includes(movie?.imdbID)
|
favouriteList?.includes(movie)
|
||||||
? setFavouriteList((prevList) =>
|
? setFavouriteList((prevList) =>
|
||||||
prevList?.filter((id) => id !== movie?.imdbID)
|
prevList?.filter((movieItem) => movieItem !== movie)
|
||||||
)
|
)
|
||||||
: setFavouriteList((prevList) => [...prevList, movie?.imdbID]);
|
: setFavouriteList((prevList) => [...prevList, movie]);
|
||||||
let localStorageData = JSON.parse(window.localStorage.getItem("favouriteMovies"));
|
|
||||||
console.log(localStorageData)
|
|
||||||
window.localStorage.setItem("favouriteMovies", JSON.stringify({movies: [...localStorageData, movie]}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import useFetch from "../hooks/useFetch";
|
import useFetch from "../hooks/useFetch";
|
||||||
|
|
||||||
import { Pagination } from "@mui/material";
|
import { Pagination } from "@mui/material";
|
||||||
|
@ -11,8 +11,16 @@ const HomePage = () => {
|
||||||
const [inputValue, setInputValue] = useState("");
|
const [inputValue, setInputValue] = useState("");
|
||||||
const [searchValue, setSearchValue] = useState("");
|
const [searchValue, setSearchValue] = useState("");
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const [favouriteList, setFavouriteList] = useState([]);
|
const [favouriteList, setFavouriteList] = useState(
|
||||||
|
JSON.parse(window.localStorage.getItem("favouriteMovies")) ?? []
|
||||||
|
);
|
||||||
const [showFavourite, setShowFavourite] = useState(false);
|
const [showFavourite, setShowFavourite] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.localStorage.setItem("favouriteMovies", JSON.stringify(favouriteList))
|
||||||
|
console.log(favouriteList);
|
||||||
|
}, [favouriteList]);
|
||||||
|
|
||||||
const { data, loading, error } = useFetch(
|
const { data, loading, error } = useFetch(
|
||||||
url + "&s=" + searchValue + "&page=" + page
|
url + "&s=" + searchValue + "&page=" + page
|
||||||
);
|
);
|
||||||
|
@ -101,3 +109,45 @@ const HomePage = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export default HomePage;
|
export default HomePage;
|
||||||
|
|
||||||
|
function SearchBar({
|
||||||
|
inputValue,
|
||||||
|
event,
|
||||||
|
setInputValue,
|
||||||
|
target,
|
||||||
|
value,
|
||||||
|
handleSearch,
|
||||||
|
setShowFavourite,
|
||||||
|
prev,
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-10">
|
||||||
|
<form className="flex gap-3 items-center">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="search"
|
||||||
|
id="search"
|
||||||
|
value={inputValue}
|
||||||
|
onChange={(event) => setInputValue(event?.target?.value)}
|
||||||
|
className="p-1 text-quadnary bg-secondary focus:bg-secondary rounded outline-none border border-ternary"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="px-4 py-1 font-semibold h-full rounded border border-quadnary text-quadnary bg-secondary"
|
||||||
|
onClick={handleSearch}
|
||||||
|
>
|
||||||
|
Search
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<div className="">
|
||||||
|
<label htmlFor="favourite">Favourites</label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="favourite"
|
||||||
|
id="favourite"
|
||||||
|
onClick={() => setShowFavourite((prev) => !prev)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue