Skip to content

Commit 0db9de0

Browse files
2.6 Movie Detail
1 parent e86e2fb commit 0db9de0

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module.exports = {
1717
source: "/api/movies",
1818
destination: `https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}`,
1919
},
20+
{
21+
source: "/api/movies/:id",
22+
destination: `https://api.themoviedb.org/3/movie/:id?api_key=${API_KEY}`,
23+
},
2024
];
2125
},
2226
};

pages/index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
import { useEffect, useState } from "react";
1+
import Link from "next/link";
2+
import { useRouter } from "next/router";
23
import Seo from "../components/Seo";
34

45
export default function Home({ results }) {
6+
const router = useRouter();
7+
const onClick = (id, title) => {
8+
router.push(
9+
{
10+
pathname: `/movies/${id}`,
11+
query: {
12+
title,
13+
},
14+
},
15+
`/movies/${id}`
16+
);
17+
};
518
return (
619
<div className="container">
720
<Seo title="Home" />
821
{results?.map((movie) => (
9-
<div className="movie" key={movie.id}>
22+
<div
23+
onClick={() => onClick(movie.id, movie.original_title)}
24+
className="movie"
25+
key={movie.id}
26+
>
1027
<img src={`https://image.tmdb.org/t/p/w500${movie.poster_path}`} />
11-
<h4>{movie.original_title}</h4>
28+
<h4>
29+
<Link
30+
href={{
31+
pathname: `/movies/${movie.id}`,
32+
query: {
33+
title: movie.original_title,
34+
},
35+
}}
36+
as={`/movies/${movie.id}`}
37+
>
38+
<a>{movie.original_title}</a>
39+
</Link>
40+
</h4>
1241
</div>
1342
))}
1443
<style jsx>{`

pages/movies/[id].js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { useRouter } from "next/router";
22

33
export default function Detail() {
44
const router = useRouter();
5-
console.log(router);
6-
return "detail";
5+
return (
6+
<div>
7+
<h4>{router.query.title || "Loading..."}</h4>
8+
</div>
9+
);
710
}

0 commit comments

Comments
 (0)