list similar to clicked movie
This commit is contained in:
parent
da61b72402
commit
3e3feacbc1
|
@ -6,7 +6,8 @@ import tmdb from './objects/tmdb'
|
||||||
|
|
||||||
export type Config = {
|
export type Config = {
|
||||||
language: string,
|
language: string,
|
||||||
region: string
|
region: string,
|
||||||
|
page: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
@ -26,7 +27,7 @@ function App() {
|
||||||
<main>
|
<main>
|
||||||
Let's explore!
|
Let's explore!
|
||||||
<div className='flow-container'>
|
<div className='flow-container'>
|
||||||
<MovieWall movies={movies} setMovies={setMovies} />
|
<MovieWall movies={movies} setMovies={setMovies} config={config} />
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import React, { useState } from "react"
|
import React, { useState } from "react"
|
||||||
import { relocateArrayItem } from "../functions"
|
import { relocateArrayItem } from "../functions"
|
||||||
|
import { Config } from "../App"
|
||||||
|
import tmdb from "./tmdb"
|
||||||
|
|
||||||
type Movie = {
|
export type Movie = {
|
||||||
id: string
|
id: number
|
||||||
poster_path: string
|
poster_path: string
|
||||||
title: string
|
title: string
|
||||||
overview: string
|
overview: string
|
||||||
|
@ -28,24 +30,15 @@ const sampleData = {
|
||||||
interface MovieWallProps extends React.ComponentPropsWithRef<"div"> {
|
interface MovieWallProps extends React.ComponentPropsWithRef<"div"> {
|
||||||
movies: Array<Movie>;
|
movies: Array<Movie>;
|
||||||
setMovies: Function;
|
setMovies: Function;
|
||||||
|
config: Config;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function MovieWall({ movies, setMovies }: MovieWallProps) {
|
export function MovieWall({ movies, setMovies, config }: MovieWallProps) {
|
||||||
|
|
||||||
function moveMovieToFront(index: number) {
|
|
||||||
setMovies((prev: Array<Movie>) => { return relocateArrayItem(prev, index, 0) })
|
|
||||||
}
|
|
||||||
|
|
||||||
function listSimilarMovies(movie: Movie) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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]
|
|
||||||
posters.push(
|
posters.push(
|
||||||
<Poster movie={movie} key={movie.id} index={i} moveMovieToFront={moveMovieToFront} />
|
<Poster movie={movies[i]} key={movies[i].id} index={i} listSimilar={tmdb.getSimilar} config={config} setMovies={setMovies} />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return <>
|
return <>
|
||||||
|
@ -55,12 +48,14 @@ export function MovieWall({ movies, setMovies }: MovieWallProps) {
|
||||||
|
|
||||||
interface PosterProps extends React.ComponentPropsWithRef<"div"> {
|
interface PosterProps extends React.ComponentPropsWithRef<"div"> {
|
||||||
movie: Movie;
|
movie: Movie;
|
||||||
moveMovieToFront: Function;
|
listSimilar: Function;
|
||||||
index: number;
|
index: number;
|
||||||
|
config: Config;
|
||||||
|
setMovies: Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function Poster({ movie, index, moveMovieToFront: onclick }: PosterProps) {
|
function Poster({ movie, config, listSimilar, setMovies }: PosterProps) {
|
||||||
const [style, setStyle] = useState({
|
const [style, setStyle] = useState({
|
||||||
backgroundImage: "url(https://image.tmdb.org/t/p/w500/" + movie.poster_path + ")",
|
backgroundImage: "url(https://image.tmdb.org/t/p/w500/" + movie.poster_path + ")",
|
||||||
backgroundSize: "cover",
|
backgroundSize: "cover",
|
||||||
|
@ -68,7 +63,7 @@ function Poster({ movie, index, moveMovieToFront: onclick }: PosterProps) {
|
||||||
color: "inherit"
|
color: "inherit"
|
||||||
})
|
})
|
||||||
return <div className="poster" style={style}>
|
return <div className="poster" style={style}>
|
||||||
<div className="overlay" onClick={() => { onclick(index) }}>
|
<div className="overlay" onClick={() => { listSimilar(config, movie, setMovies) }}>
|
||||||
<h1>{movie.title}</h1>
|
<h1>{movie.title}</h1>
|
||||||
<p>{movie.overview}</p>
|
<p>{movie.overview}</p>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import * as auth from '../auth.json';
|
import * as auth from '../auth.json';
|
||||||
|
import { Movie } from './movie-wall';
|
||||||
import { Config } from '../App';
|
import { Config } from '../App';
|
||||||
|
|
||||||
const tmdb = axios.create({
|
const tmdb = axios.create({
|
||||||
|
@ -34,10 +35,10 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Calls TMDB/similar
|
* Calls TMDB/similar, then fires the callback with res.data.results.unshift(movie) as argument
|
||||||
*/
|
*/
|
||||||
getSimilar: async function({ language, page }: Config, id: number, callback: Function) {
|
getSimilar: async function({ language, page }: Config, movie: Movie, callback: Function) {
|
||||||
let res = await tmdb.get(id + '/similar', {
|
let res = await tmdb.get(movie.id + '/similar', {
|
||||||
params: {
|
params: {
|
||||||
language,
|
language,
|
||||||
page
|
page
|
||||||
|
@ -45,8 +46,9 @@ export default {
|
||||||
})
|
})
|
||||||
console.log(res)
|
console.log(res)
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
res = res.data.results
|
const array: Array<Movie> = res.data.results
|
||||||
callback(res)
|
array.unshift(movie)
|
||||||
|
callback(array)
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
throw Error("API call failed! Response: " + JSON.stringify(res))
|
throw Error("API call failed! Response: " + JSON.stringify(res))
|
||||||
|
|
Loading…
Reference in New Issue