Skip to content

Commit

Permalink
add loader
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirrMahmoudi committed Jan 3, 2024
1 parent b557aa1 commit 80676eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
30 changes: 24 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";

const tempMovieData = [
{
Expand Down Expand Up @@ -50,10 +50,27 @@ const tempWatchedData = [
const average = (arr) =>
arr.reduce((acc, cur, i, arr) => acc + cur / arr.length, 0);

const KEY = "b06865a3";

export default function App() {
const [movies, setMovies] = useState(tempMovieData);
const [movies, setMovies] = useState([]);
const [watched, setWatched] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const query = "interstellar";

const [watched, setWatched] = useState(tempWatchedData);
useEffect(function () {
async function fetchMovies() {
setIsLoading(true);

const res = await fetch(
`http://www.omdbapi.com/?apikey=${KEY}&s=${query}`
);
const data = await res.json();
setMovies(data.Search);
setIsLoading(false);
}
fetchMovies();
}, []);

return (
<>
Expand All @@ -62,9 +79,7 @@ export default function App() {
<NumResults movies={movies} />
</NavBar>
<Main>
<Box>
<MovieList movies={movies} />
</Box>
<Box>{isLoading ? <Loader /> : <MovieList movies={movies} />}</Box>
<Box>
<WatchedSummary watched={watched} />
<WatchedMoviesList watched={watched} />
Expand All @@ -74,6 +89,9 @@ export default function App() {
);
}

function Loader() {
return <p className="loader">Loading...</p>;
}
const NavBar = ({ children }) => {
return (
<nav className="nav-bar">
Expand Down
56 changes: 36 additions & 20 deletions src/components/StarRating.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ const starContainerStyle = {
display: "flex",
};

const textStyle = {
lineHeight: "1",
margin: "0",
};

const StarRating = ({ maxRating = 5 }) => {
const [raiting, setRating] = useState(0);
const StarRating = ({
maxRating = 5,
color = "#fcc419",
size = 48,
className = "",
messages = [],
defaultRating = 0,
// onSetRating,
}) => {
const [raiting, setRating] = useState(defaultRating);
const [tempRating, setTempRating] = useState(0);

function handleRating(rating) {
setRating(rating);
// onSetRating(rating)
}

const textStyle = {
lineHeight: "1",
margin: "0",
color,
fontSize: `${size / 1.5}px`,
};
return (
<div style={containerStyle}>
<div style={containerStyle} className={className}>
<div style={starContainerStyle}>
{Array.from({ length: maxRating }, (_, i) => (
<Star
Expand All @@ -33,23 +42,30 @@ const StarRating = ({ maxRating = 5 }) => {
onRate={() => handleRating(i + 1)}
onHoverIn={() => setTempRating(i + 1)}
onHoverOut={() => setTempRating(0)}
color={color}
size={size}
/>
))}
</div>
<p style={textStyle}>{tempRating || raiting || ""}</p>
<p style={textStyle}>
{messages.length === maxRating
? messages[tempRating ? tempRating - 1 : raiting - 1]
: tempRating || raiting || ""}
</p>
</div>
);
};

export default StarRating;

const StarStyle = {
width: "48px",
height: "48px",
display: "block",
cursor: "pointer",
};
function Star({ onRate, full, onHoverIn, onHoverOut }) {
function Star({ onRate, full, onHoverIn, onHoverOut, size, color }) {
const StarStyle = {
width: `${size}px`,
height: `${size}px`,
display: "block",
cursor: "pointer",
};

return (
<span
role="button"
Expand All @@ -62,8 +78,8 @@ function Star({ onRate, full, onHoverIn, onHoverOut }) {
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="#000"
stroke="#000"
fill={color}
stroke={color}
>
<path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z" />
</svg>
Expand All @@ -72,7 +88,7 @@ function Star({ onRate, full, onHoverIn, onHoverOut }) {
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="#000"
stroke={color}
>
<path
strokeLinecap="round"
Expand Down

0 comments on commit 80676eb

Please sign in to comment.