From 3e3feacbc1b9f000e81bb859d322e9c1498ea1f4 Mon Sep 17 00:00:00 2001 From: andrzej Date: Mon, 6 May 2024 11:09:21 +0200 Subject: [PATCH] list similar to clicked movie --- src/App.tsx | 5 +++-- src/objects/movie-wall.tsx | 29 ++++++++++++----------------- src/objects/tmdb.tsx | 12 +++++++----- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c5ace3b..5de3f70 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,7 +6,8 @@ import tmdb from './objects/tmdb' export type Config = { language: string, - region: string + region: string, + page: number, } function App() { @@ -26,7 +27,7 @@ function App() {
Let's explore!
- +
diff --git a/src/objects/movie-wall.tsx b/src/objects/movie-wall.tsx index 248da65..27c27be 100644 --- a/src/objects/movie-wall.tsx +++ b/src/objects/movie-wall.tsx @@ -1,8 +1,10 @@ import React, { useState } from "react" import { relocateArrayItem } from "../functions" +import { Config } from "../App" +import tmdb from "./tmdb" -type Movie = { - id: string +export type Movie = { + id: number poster_path: string title: string overview: string @@ -28,24 +30,15 @@ const sampleData = { interface MovieWallProps extends React.ComponentPropsWithRef<"div"> { movies: Array; setMovies: Function; + config: Config; } -export function MovieWall({ movies, setMovies }: MovieWallProps) { - - function moveMovieToFront(index: number) { - setMovies((prev: Array) => { return relocateArrayItem(prev, index, 0) }) - } - - function listSimilarMovies(movie: Movie) { - - - } +export function MovieWall({ movies, setMovies, config }: MovieWallProps) { const posters: Array = [] for (let i = 0; i < movies.length; i++) { - const movie = movies[i] posters.push( - + ) } return <> @@ -55,12 +48,14 @@ export function MovieWall({ movies, setMovies }: MovieWallProps) { interface PosterProps extends React.ComponentPropsWithRef<"div"> { movie: Movie; - moveMovieToFront: Function; + listSimilar: Function; index: number; + config: Config; + setMovies: Function; } -function Poster({ movie, index, moveMovieToFront: onclick }: PosterProps) { +function Poster({ movie, config, listSimilar, setMovies }: PosterProps) { const [style, setStyle] = useState({ backgroundImage: "url(https://image.tmdb.org/t/p/w500/" + movie.poster_path + ")", backgroundSize: "cover", @@ -68,7 +63,7 @@ function Poster({ movie, index, moveMovieToFront: onclick }: PosterProps) { color: "inherit" }) return
-
{ onclick(index) }}> +
{ listSimilar(config, movie, setMovies) }}>

{movie.title}

{movie.overview}

diff --git a/src/objects/tmdb.tsx b/src/objects/tmdb.tsx index 7e80d61..94d3797 100644 --- a/src/objects/tmdb.tsx +++ b/src/objects/tmdb.tsx @@ -1,5 +1,6 @@ import axios from 'axios' import * as auth from '../auth.json'; +import { Movie } from './movie-wall'; import { Config } from '../App'; 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) { - let res = await tmdb.get(id + '/similar', { + getSimilar: async function({ language, page }: Config, movie: Movie, callback: Function) { + let res = await tmdb.get(movie.id + '/similar', { params: { language, page @@ -45,8 +46,9 @@ export default { }) console.log(res) if (res.status === 200) { - res = res.data.results - callback(res) + const array: Array = res.data.results + array.unshift(movie) + callback(array) return true } else { throw Error("API call failed! Response: " + JSON.stringify(res))