set state when no similar movies available
This commit is contained in:
parent
a70ddebc8a
commit
cacaa80d11
31
src/App.css
31
src/App.css
|
@ -32,13 +32,17 @@ body {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
main {
|
main {
|
||||||
|
height: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
gap: 1em;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,34 +53,42 @@ main {
|
||||||
flex-basis: 60%;
|
flex-basis: 60%;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 1em;
|
gap: 1em;
|
||||||
|
height: fit-content;
|
||||||
}
|
}
|
||||||
|
|
||||||
img {
|
img {
|
||||||
aspect-ratio: 2/3;
|
aspect-ratio: 2/3;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#sidebar {
|
#sidebar {
|
||||||
flex-basis: calc(var(--poster-width)*2);
|
flex-basis: calc(var(--poster-width)*2);
|
||||||
font-size: 1.6rem;
|
font-size: 1.6rem;
|
||||||
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 2em;
|
font-size: 2em;
|
||||||
margin-bottom: 0.2em;
|
margin: 0;
|
||||||
/* line-height: 0.8em; */
|
/* line-height: 0.8em; */
|
||||||
font-family: RobotoBold;
|
font-family: RobotoBold;
|
||||||
padding: 0.1em 0.2em 0 0.2em;
|
padding: 0.1em 0.2em 0.1em 0.2em;
|
||||||
}
|
}
|
||||||
|
|
||||||
p {
|
p {
|
||||||
font-family: Jura;
|
font-family: Jura;
|
||||||
|
padding: 0.2em;
|
||||||
|
overflow: scroll;
|
||||||
|
height: 10em;
|
||||||
|
margin: 0.1em 0.2em 0.1em 0.2em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
figure {
|
||||||
|
width: 100%;
|
||||||
|
float: left;
|
||||||
|
|
||||||
img {
|
img {
|
||||||
width: 90%;
|
|
||||||
aspect-ratio: 2/3;
|
aspect-ratio: 2/3;
|
||||||
box-shadow: 1em 1em 1em rgba(0, 0, 0, 0.5);
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -84,6 +96,9 @@ img {
|
||||||
.poster {
|
.poster {
|
||||||
width: var(--poster-width);
|
width: var(--poster-width);
|
||||||
display: grid;
|
display: grid;
|
||||||
|
box-shadow: 1em 1em 1em rgba(0, 0, 0, 0.5);
|
||||||
|
border-radius: 1em;
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
img {
|
img {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
@ -109,3 +124,7 @@ img {
|
||||||
.overlay:hover {
|
.overlay:hover {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.highlighted {
|
||||||
|
border: 0.1em solid gold;
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ class MovieClass implements Movie {
|
||||||
backdrop_path = null
|
backdrop_path = null
|
||||||
title = null
|
title = null
|
||||||
overview = null
|
overview = null
|
||||||
|
vote_average = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
@ -29,20 +30,23 @@ function App() {
|
||||||
const [backdrop, setBackdrop] = useState("")
|
const [backdrop, setBackdrop] = useState("")
|
||||||
const [chosenMovie, setChosenMovie] = useState(new MovieClass())
|
const [chosenMovie, setChosenMovie] = useState(new MovieClass())
|
||||||
const [loadedMovies, setLoadedMovies] = useState(0)
|
const [loadedMovies, setLoadedMovies] = useState(0)
|
||||||
|
const [similarMoviesAvailable, setSimilarMoviesAvailable] = useState(true)
|
||||||
|
|
||||||
useEffect(() => { tmdb.getPopular(config, setMovies) }, [])
|
useEffect(() => { tmdb.getPopular(config, setMovies) }, [])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLoadedMovies(movies.length)
|
setLoadedMovies(movies.length)
|
||||||
if (movies[0].backdrop_path) {
|
if (movies[0].backdrop_path) {
|
||||||
const bgString = "linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0, 0.5)), " + tmdb.makeBgImgUrl(movies[0].backdrop_path)
|
const bgString = "linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0, 0.5)), " + tmdb.makeBgImgUrl(chosenMovie.backdrop_path ? chosenMovie.backdrop_path : movies[0].backdrop_path)
|
||||||
setBackdrop(bgString)
|
setBackdrop(bgString)
|
||||||
}
|
}
|
||||||
}, [movies])
|
}, [movies])
|
||||||
|
useEffect(() => { console.log("SIMILAR AVAILABLE: " + similarMoviesAvailable) }, [similarMoviesAvailable])
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<div id="loader"></div>
|
||||||
<div className='main-container' style={{ backgroundImage: backdrop }}>
|
<div className='main-container' style={{ backgroundImage: backdrop }}>
|
||||||
<main >
|
<main >
|
||||||
<Sidebar movie={chosenMovie} />
|
<Sidebar movie={chosenMovie} similarMoviesAvailable={similarMoviesAvailable} />
|
||||||
<div id="movie-wall-container">
|
<div id="movie-wall-container">
|
||||||
{loadedMovies === movies.length ?
|
{loadedMovies === movies.length ?
|
||||||
<MovieWall movies={movies}
|
<MovieWall movies={movies}
|
||||||
|
@ -50,6 +54,7 @@ function App() {
|
||||||
config={config}
|
config={config}
|
||||||
setChosenMovie={setChosenMovie}
|
setChosenMovie={setChosenMovie}
|
||||||
setLoadedMovies={setLoadedMovies}
|
setLoadedMovies={setLoadedMovies}
|
||||||
|
setSimilarMoviesAvailable={setSimilarMoviesAvailable}
|
||||||
/>
|
/>
|
||||||
: "LOADING " + loadedMovies} </div>
|
: "LOADING " + loadedMovies} </div>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
@ -8,6 +8,7 @@ export interface Movie {
|
||||||
backdrop_path: string | null
|
backdrop_path: string | null
|
||||||
title: string | null
|
title: string | null
|
||||||
overview: string | null
|
overview: string | null
|
||||||
|
vote_average: number | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,15 +34,22 @@ interface MovieWallProps extends React.ComponentPropsWithRef<"div"> {
|
||||||
setMovies: Function;
|
setMovies: Function;
|
||||||
setChosenMovie: Function;
|
setChosenMovie: Function;
|
||||||
setLoadedMovies: Function;
|
setLoadedMovies: Function;
|
||||||
|
setSimilarMoviesAvailable: Function;
|
||||||
config: Config;
|
config: Config;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function MovieWall({ movies, setMovies, config, setChosenMovie, setLoadedMovies }: MovieWallProps) {
|
export function MovieWall({ movies, setMovies, config, setChosenMovie, setLoadedMovies, setSimilarMoviesAvailable }: MovieWallProps) {
|
||||||
|
|
||||||
const posters: Array<React.Component> = []
|
const posters: Array<React.Component> = []
|
||||||
for (let i = 0; i < movies.length; i++) {
|
for (let i = 0; i < movies.length; i++) {
|
||||||
|
const movie = movies[i]
|
||||||
|
console.log("VOTE AVG: " + movie.vote_average)
|
||||||
|
const isHighlighted = movie.vote_average ? movie.vote_average > 6 : false
|
||||||
|
console.log("IS HIGHLIGHTED: " + isHighlighted)
|
||||||
posters.push(
|
posters.push(
|
||||||
<Poster movie={movies[i]} key={movies[i].id} index={i} listSimilar={tmdb.getSimilar} config={config} setMovies={setMovies} setChosenMovie={setChosenMovie} setLoadedMovies={setLoadedMovies} />
|
<Poster isHighlighted={isHighlighted} movie={movie} key={movie.id} index={i} listSimilar={tmdb.getSimilar} config={config} setMovies={setMovies} setChosenMovie={setChosenMovie} setLoadedMovies={setLoadedMovies}
|
||||||
|
setSimilarMoviesAvailable={setSimilarMoviesAvailable}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return <>
|
return <>
|
||||||
|
@ -57,18 +65,21 @@ interface PosterProps extends React.ComponentPropsWithRef<"div"> {
|
||||||
setMovies: Function;
|
setMovies: Function;
|
||||||
setChosenMovie: Function;
|
setChosenMovie: Function;
|
||||||
setLoadedMovies: Function;
|
setLoadedMovies: Function;
|
||||||
|
setSimilarMoviesAvailable: Function;
|
||||||
|
isHighlighted: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function Poster({ movie, config, listSimilar, setMovies, setChosenMovie, setLoadedMovies }: PosterProps) {
|
function Poster({ movie, config, listSimilar, setMovies, setChosenMovie, setSimilarMoviesAvailable, isHighlighted }: PosterProps) {
|
||||||
function clickHandler() {
|
function clickHandler() {
|
||||||
setChosenMovie(movie)
|
setChosenMovie(movie)
|
||||||
listSimilar(config, movie, setMovies)
|
listSimilar(config, movie, setMovies, setSimilarMoviesAvailable)
|
||||||
}
|
}
|
||||||
const style = {
|
const classes = [
|
||||||
// backgroundImage: img.src
|
"poster",
|
||||||
}
|
isHighlighted ? "highlighted" : ""
|
||||||
return <div className="poster" style={style}>
|
].join(" ")
|
||||||
|
return <div className={classes} >
|
||||||
<div className="overlay" onClick={() => clickHandler()}>
|
<div className="overlay" onClick={() => clickHandler()}>
|
||||||
</div>
|
</div>
|
||||||
<img
|
<img
|
||||||
|
|
|
@ -3,12 +3,23 @@ import { Movie } from "./movie-wall";
|
||||||
import tmdb from "./tmdb";
|
import tmdb from "./tmdb";
|
||||||
export interface SidebarProps extends React.ComponentPropsWithRef<"div"> {
|
export interface SidebarProps extends React.ComponentPropsWithRef<"div"> {
|
||||||
movie: Movie;
|
movie: Movie;
|
||||||
|
similarMoviesAvailable: boolean;
|
||||||
}
|
}
|
||||||
export function Sidebar({ movie }: SidebarProps) {
|
export function Sidebar({ movie, similarMoviesAvailable }: SidebarProps) {
|
||||||
return <div id="sidebar">
|
return <div id="sidebar">
|
||||||
<h1>{movie?.title ?? "loading"}</h1>
|
<h1>{movie?.title ?? "loading"}</h1>
|
||||||
|
<figure>
|
||||||
|
<a href={tmdb.makeMovieLink(movie)} target="_blank" rel="noopener noreferrer">
|
||||||
<img src={tmdb.makeImgUrl(movie.poster_path ?? "")} />
|
<img src={tmdb.makeImgUrl(movie.poster_path ?? "")} />
|
||||||
<p>{movie?.overview ?? "loading"}</p>
|
</a>
|
||||||
|
<figcaption>
|
||||||
|
{similarMoviesAvailable ? "" : "No hay data suficiente para darte pelílculas relacionadas."}
|
||||||
|
</figcaption>
|
||||||
|
</figure>
|
||||||
|
<p>{movie.overview === null ? "Loading"
|
||||||
|
: movie.overview === "" ? "Resumen no disponible."
|
||||||
|
: movie.overview}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ export default {
|
||||||
/**
|
/**
|
||||||
* Calls TMDB/similar, then fires the callback with res.data.results.unshift(movie) as argument
|
* Calls TMDB/similar, then fires the callback with res.data.results.unshift(movie) as argument
|
||||||
*/
|
*/
|
||||||
getSimilar: async function({ language, page }: Config, movie: Movie, callback: Function) {
|
getSimilar: async function({ language, page }: Config, movie: Movie, setMovies: Function, setSimilarMoviesAvailable: Function) {
|
||||||
let res = await tmdb.get(movie.id + '/similar', {
|
let res = await tmdb.get(movie.id + '/similar', {
|
||||||
params: {
|
params: {
|
||||||
language,
|
language,
|
||||||
|
@ -50,9 +50,14 @@ export default {
|
||||||
.filter((e: Movie) => {
|
.filter((e: Movie) => {
|
||||||
return e.poster_path
|
return e.poster_path
|
||||||
})
|
})
|
||||||
array.splice(19)
|
// array.splice(19)
|
||||||
// array.unshift(movie)
|
if (array.length > 0) {
|
||||||
callback(array)
|
setMovies(array)
|
||||||
|
setSimilarMoviesAvailable(true)
|
||||||
|
} else {
|
||||||
|
setSimilarMoviesAvailable(false)
|
||||||
|
console.log("SIMILAR NOT AVAILABLE")
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
throw Error("API call failed! Response: " + JSON.stringify(res))
|
throw Error("API call failed! Response: " + JSON.stringify(res))
|
||||||
|
@ -91,5 +96,12 @@ export default {
|
||||||
*/
|
*/
|
||||||
function(path: string) {
|
function(path: string) {
|
||||||
return "https://image.tmdb.org/t/p/w500" + path
|
return "https://image.tmdb.org/t/p/w500" + path
|
||||||
|
},
|
||||||
|
makeMovieLink:
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function(movie: Movie) {
|
||||||
|
return "https://www.themoviedb.org/movie/" + movie.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue