From f293734490aeb6431c33c6d3b3c31321c8d3b12f Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Wed, 6 Sep 2023 07:56:46 +0200 Subject: [PATCH] :recycle: componentization of objects Co-authored-by: haraldnilsen --- src/App.tsx | 58 +++++------------------------ src/components/MovieModal/index.tsx | 45 ++++++++++++++++++++++ src/components/MovieTable/index.tsx | 40 ++++++++++++++++++++ src/components/index.ts | 2 + src/types/movie.ts | 2 + 5 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 src/components/MovieModal/index.tsx create mode 100644 src/components/MovieTable/index.tsx diff --git a/src/App.tsx b/src/App.tsx index 5b32157..dc0fe44 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,13 @@ import { useEffect, useState } from 'react' import './App.css' import q from 'qjuul' -import { MovieTableRow, MovieForm, Pagination } from './components' +import { + MovieTableRow, + MovieForm, + Pagination, + MovieTable, + MovieModal, +} from './components' import type { movieObject } from './types/movie' import Modal from 'react-modal' @@ -87,36 +93,9 @@ function App() { return ( <> - setModalOpen(false)} - > - - - - - {modalMovie?.Title} - - Year: {modalMovie?.Year} - Type: {modalMovie?.Type} - - {`https://www.imdb.com/title/${modalMovie?.imdbID}`} - - - - + All movies - {!loading && movies ? ( - - - - - Poster - sortHandler('title')}>Title - sortHandler('year')}>Year - - - - {movies.map((movie: movieObject) => ( - handleModalOpen(movie)} - /> - ))} - - - + ) : ( Loading... )} diff --git a/src/components/MovieModal/index.tsx b/src/components/MovieModal/index.tsx new file mode 100644 index 0000000..75b6c4d --- /dev/null +++ b/src/components/MovieModal/index.tsx @@ -0,0 +1,45 @@ +import type { modalMovieType } from '../../types/movie' +import q from 'qjuul' +import Modal from 'react-modal' + +interface MovieModalProps { + setModalOpen: (status: boolean) => void + modalMovie: modalMovieType + modalOpen: boolean +} + +const MovieModal: React.FC = ({ + setModalOpen, + modalMovie, + modalOpen, +}) => { + return ( + setModalOpen(false)} + > + + + + {modalMovie?.Title} + Year: {modalMovie?.Year} + Type: {modalMovie?.Type} + + {`https://www.imdb.com/title/${modalMovie?.imdbID}`} + + + + + ) +} + +export default MovieModal diff --git a/src/components/MovieTable/index.tsx b/src/components/MovieTable/index.tsx new file mode 100644 index 0000000..e769b5d --- /dev/null +++ b/src/components/MovieTable/index.tsx @@ -0,0 +1,40 @@ +import type { movieObject } from '../../types/movie' +import q from 'qjuul' +import MovieTableRow from '../MovieTableRow' + +interface MovieTableProps { + handleModalOpen: (movie: movieObject) => void + sortHandler: (sortType: string) => void + movies: movieObject[] +} + +const MovieTable: React.FC = ({ + handleModalOpen, + sortHandler, + movies, +}) => { + return ( + + + + + Poster + sortHandler('title')}>Title + sortHandler('year')}>Year + + + + {movies.map((movie: movieObject) => ( + handleModalOpen(movie)} + /> + ))} + + + + ) +} + +export default MovieTable diff --git a/src/components/index.ts b/src/components/index.ts index 2860fa7..673fdd5 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,3 +1,5 @@ export { default as Pagination } from './Pagination' export { default as MovieTableRow } from './MovieTableRow' export { default as MovieForm } from './MovieForm' +export { default as MovieTable } from './MovieTable' +export { default as MovieModal } from './MovieModal' diff --git a/src/types/movie.ts b/src/types/movie.ts index bba9dd2..25c5ae4 100644 --- a/src/types/movie.ts +++ b/src/types/movie.ts @@ -5,3 +5,5 @@ export type movieObject = { Year: string imdbID: string } + +export type modalMovieType = movieObject | null