From 431e1bd472ab50413abb36ef3486282b25cfbacf Mon Sep 17 00:00:00 2001 From: haraldnilsen Date: Tue, 5 Sep 2023 15:54:36 +0200 Subject: [PATCH] :sparkles: modal showing for each movie Co-authored-by: Sindre Kjelsrud --- package.json | 4 +- src/App.tsx | 67 ++++++++++++++++++++------ src/components/MovieTableRow/index.tsx | 27 +++++++++++ src/components/Pagination/index.tsx | 3 +- src/components/index.ts | 1 + src/types/movie.ts | 7 +++ 6 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 src/components/MovieTableRow/index.tsx diff --git a/package.json b/package.json index 6025f3c..338a3ea 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,13 @@ "git-mob": "^2.4.0", "qjuul": "^1.0.7", "react": "^18.2.0", - "react-dom": "^18.2.0" + "react-dom": "^18.2.0", + "react-modal": "^3.16.1" }, "devDependencies": { "@types/react": "^18.2.15", "@types/react-dom": "^18.2.7", + "@types/react-modal": "^3.16.0", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "@vitejs/plugin-react": "^4.0.3", diff --git a/src/App.tsx b/src/App.tsx index 4c9307d..824c1cb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,10 @@ import { useEffect, useState } from 'react' import './App.css' import q from 'qjuul' -import { PaginationButton } from './components' import Pagination from './components/Pagination' +import { MovieTableRow } from './components' +import type { movieObject } from './types/movie' +import Modal from 'react-modal' function App() { const API_MOVIE_KEY = 'd92949d8' @@ -10,6 +12,8 @@ function App() { const [loading, setLoading] = useState(true) const [currentPage, setCurrentPage] = useState(1) const [totalPages, setTotalPages] = useState(0) + const [modalOpen, setModalOpen] = useState(false) + const [modalMovie, setModalMovie] = useState(null) useEffect(() => { fetch( @@ -18,6 +22,7 @@ function App() { .then((response) => response.json()) .then((data) => { setMovies(data.Search) + console.log(data) setTotalPages(data.totalResults) }) .then(() => setLoading(false)) @@ -32,9 +37,41 @@ function App() { setCurrentPage(pageNumber) } + const handleModalOpen = (movie: movieObject) => { + setModalOpen(true) + setModalMovie(movie) + } + return ( <> + setModalOpen(false)} + > + + + + + {modalMovie?.Title} + + Year: {modalMovie?.Year} + Type: {modalMovie?.Type} + + {`https://www.imdb.com/title/${modalMovie?.imdbID}`} + + + + All movies @@ -50,20 +87,22 @@ function App() { {!loading && movies ? ( - - Poster - Title - Year - - {movies.map((movie: any) => ( - - - - - {movie.Title} - {movie.Year} + + + Poster + Title + Year - ))} + + + {movies.map((movie: any) => ( + handleModalOpen(movie)} + /> + ))} + ) : ( diff --git a/src/components/MovieTableRow/index.tsx b/src/components/MovieTableRow/index.tsx new file mode 100644 index 0000000..2ad7fab --- /dev/null +++ b/src/components/MovieTableRow/index.tsx @@ -0,0 +1,27 @@ +import type { movieObject } from '../../types/movie' +import Modal from 'react-modal' +import q from 'qjuul' +import { useState } from 'react' + +interface MovieTableRowProps { + movie: movieObject + onClick: () => void +} + +const mainAppElement = document.getElementById('root') + +Modal.setAppElement(mainAppElement as HTMLElement) + +const MovieTableRow: React.FC = ({ movie, onClick }) => { + return ( + + + + + {movie.Title} + {movie.Year} + + ) +} + +export default MovieTableRow diff --git a/src/components/Pagination/index.tsx b/src/components/Pagination/index.tsx index 468c448..42f1e41 100644 --- a/src/components/Pagination/index.tsx +++ b/src/components/Pagination/index.tsx @@ -22,7 +22,6 @@ const Pagination: React.FC = ({ setShowLeftDots(currentPage > 4 ? true : false) setShowRightDots(currentPage < totalPages - 3 ? true : false) } - console.log('hei') if (!showLeftDots && !showRightDots) { setShowingNumbers(Array.from(Array(totalPages).keys())) } @@ -60,7 +59,7 @@ const Pagination: React.FC = ({ )} {showingNumbers.map((page) => ( - +