diff --git a/src/components/Rate.jsx b/src/components/Rate.jsx new file mode 100644 index 0000000..a10fc2c --- /dev/null +++ b/src/components/Rate.jsx @@ -0,0 +1,38 @@ +import { useState } from "react" +import styles from "styles/Rate.module.css" + +const Rate = ({ rating, setRating }) => { + const [hover, setHover] = useState() + + return ( +
+ {[...Array(5)].map((_, index) => { + index++ + return ( + + ) + })} +
+ ) +} + +export default Rate diff --git a/src/hooks/useLocalStorage.jsx b/src/hooks/useLocalStorage.jsx new file mode 100644 index 0000000..428913b --- /dev/null +++ b/src/hooks/useLocalStorage.jsx @@ -0,0 +1,30 @@ +import { useEffect, useState } from "react" + +const useLocalStorage = (key, defaultValue) => { + const [storedValue, setStoredValue] = useState(() => { + try { + const value = window.localStorage.getItem(key) + + if (value) { + return JSON.parse(value) + } else { + window.localStorage.setItem(key, JSON.stringify(defaultValue)) + return defaultValue + } + } catch (err) { + return defaultValue + } + }) + + useEffect(() => { + try { + window.localStorage.setItem(key, JSON.stringify(storedValue)) + } catch (err) { + console.error(err) + } + }, [storedValue]) + + return [storedValue, setStoredValue] +} + +export default useLocalStorage diff --git a/src/pages/Actor.jsx b/src/pages/Actor.jsx index b288fe6..993ebe7 100644 --- a/src/pages/Actor.jsx +++ b/src/pages/Actor.jsx @@ -3,7 +3,6 @@ import config from "config" import styles from "styles/Actor.module.css" import { Link, useParams } from "react-router-dom" import { GENRES } from "../constants" -import Card from "components/Card" const Actor = () => { const { actorId } = useParams() diff --git a/src/pages/Movie.jsx b/src/pages/Movie.jsx index 0eb3068..230868d 100644 --- a/src/pages/Movie.jsx +++ b/src/pages/Movie.jsx @@ -2,11 +2,21 @@ import { useEffect, useState } from "react" import { Link, useParams } from "react-router-dom" import styles from "styles/Movie.module.css" import config from "config" +import useLocalStorage from "hooks/useLocalStorage" +import Rate from "components/Rate" const Movie = () => { const { movieId } = useParams() const [movie, setMovie] = useState() const [cast, setCast] = useState() + const [ratings, setRatings] = useLocalStorage("ratings", []) + const rating = ratings.find(({ id }) => id == movieId)?.rating + + const setRating = (rating) => + setRatings((currentRatings) => [ + ...currentRatings.filter(({ id }) => id != movieId), + { ...movie, rating }, + ]) useEffect(() => { const run = async () => { @@ -63,7 +73,7 @@ const Movie = () => {
{movie.genres.map((genre) => ( -

{genre}

+

{genre}

))}

@@ -79,25 +89,11 @@ const Movie = () => {

- {false ? ( -

- Average Rating: {movie.averageVote} - - - - -

- ) : ( -

Rate this movie

- )}
-
+
+

Your Rating

+ +

Actors

diff --git a/src/styles/Movie.module.css b/src/styles/Movie.module.css index 7ab9f44..f48459f 100644 --- a/src/styles/Movie.module.css +++ b/src/styles/Movie.module.css @@ -62,3 +62,7 @@ display: flex; gap: 5px; } + +.Rate { + color: white; +} diff --git a/src/styles/Rate.module.css b/src/styles/Rate.module.css new file mode 100644 index 0000000..fbce3bc --- /dev/null +++ b/src/styles/Rate.module.css @@ -0,0 +1,14 @@ +.Star { + background-color: transparent; + border: none; + outline: none; + cursor: pointer; +} + +.Off { + color: #ccc; +} + +.On { + color: gold; +}