🚧 prøver å gjøre queryparams på nytt
Co-authored-by: haraldnilsen <harald_998@hotmail.com>
This commit is contained in:
		
							parent
							
								
									6341339174
								
							
						
					
					
						commit
						18c85a662a
					
				
					 5 changed files with 78 additions and 34 deletions
				
			
		
							
								
								
									
										55
									
								
								src/App.tsx
									
										
									
									
									
								
							
							
						
						
									
										55
									
								
								src/App.tsx
									
										
									
									
									
								
							|  | @ -3,35 +3,39 @@ import './App.css' | ||||||
| import q from 'qjuul' | import q from 'qjuul' | ||||||
| import { MovieForm, Pagination, MovieTable, MovieModal } from './components' | import { MovieForm, Pagination, MovieTable, MovieModal } from './components' | ||||||
| import type { movieObject } from './types/movie' | import type { movieObject } from './types/movie' | ||||||
|  | import { fetchMovie } from './api/fetchMovie' | ||||||
|  | import { useLocation } from 'react-router' | ||||||
| 
 | 
 | ||||||
| function App() { | function App() { | ||||||
|   const API_MOVIE_KEY = 'd92949d8' |  | ||||||
|   const [movies, setMovies] = useState<movieObject[]>([]) |   const [movies, setMovies] = useState<movieObject[]>([]) | ||||||
|   const [loading, setLoading] = useState(true) |   const [loading, setLoading] = useState(true) | ||||||
|   const [currentPage, setCurrentPage] = useState(1) |   const [currentPage, setCurrentPage] = useState(1) | ||||||
|   const [totalPages, setTotalPages] = useState(0) |   const [totalPages, setTotalPages] = useState(0) | ||||||
|   const [modalOpen, setModalOpen] = useState(false) |   const [modalOpen, setModalOpen] = useState(false) | ||||||
|   const [modalMovie, setModalMovie] = useState<movieObject | null>(null) |   const [modalMovie, setModalMovie] = useState<movieObject | null>(null) | ||||||
|   const [movieType, setMovieType] = useState('') |  | ||||||
|   const [movieYear, setMovieYear] = useState('') |  | ||||||
|   const [movieTitle, setMovieTitle] = useState('') |  | ||||||
|   const [sortAscending, setSortAscending] = useState(true) |   const [sortAscending, setSortAscending] = useState(true) | ||||||
| 
 |   const location = useLocation() | ||||||
|   console.log('App mounted') |  | ||||||
| 
 | 
 | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     fetch( |     const searchParams = new URLSearchParams(location.search) | ||||||
|       `http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=${movieTitle}&page=${currentPage}` |     const title = searchParams.get('title') || '' | ||||||
|     ) |     const page = searchParams.get('page') || '' | ||||||
|       .then((response) => response.json()) |     const type = searchParams.get('type') || '' | ||||||
|       .then((data) => { |     const year = searchParams.get('y') || '' | ||||||
|         setMovies(data.Search) | 
 | ||||||
|         console.log(data) |     const handleFetchMovie = async () => { | ||||||
|         setTotalPages(data.totalResults) |       const response = await fetchMovie(title, page, type, year) | ||||||
|       }) |       if (response.Response == 'true') { | ||||||
|       .then(() => setLoading(false)) |         setMovies(response.Search) | ||||||
|       .catch((error) => console.log(error)) |         setTotalPages(Number(response.totalResults)) | ||||||
|   }, [currentPage]) |         setLoading(false) | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (title) { | ||||||
|  |       handleFetchMovie() | ||||||
|  |     } | ||||||
|  |   }, []) | ||||||
| 
 | 
 | ||||||
|   const calculatePages = (totalResults: number): number => { |   const calculatePages = (totalResults: number): number => { | ||||||
|     return Math.round(totalResults / 10) |     return Math.round(totalResults / 10) | ||||||
|  | @ -46,21 +50,6 @@ function App() { | ||||||
|     setModalMovie(movie) |     setModalMovie(movie) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   const handleMovieSubmit = (event: any) => { |  | ||||||
|     event.preventDefault() |  | ||||||
|     fetch( |  | ||||||
|       `http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=${movieTitle}&type=${movieType}&y=${movieYear}` |  | ||||||
|     ) |  | ||||||
|       .then((response) => response.json()) |  | ||||||
|       .then((data) => { |  | ||||||
|         setMovies(data.Search) |  | ||||||
|         console.log(data) |  | ||||||
|         setTotalPages(data.totalResults) |  | ||||||
|       }) |  | ||||||
|       .then(() => setLoading(false)) |  | ||||||
|       .catch((error) => console.log(error)) |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   const sortHandler = (sortType: string) => { |   const sortHandler = (sortType: string) => { | ||||||
|     let sortedMovies: movieObject[] = [] |     let sortedMovies: movieObject[] = [] | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										26
									
								
								src/api/fetchMovie.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/api/fetchMovie.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,26 @@ | ||||||
|  | import { movieResponse } from '../types/movie' | ||||||
|  | 
 | ||||||
|  | const API_MOVIE_KEY = 'd92949d8' | ||||||
|  | 
 | ||||||
|  | export const fetchMovie = async ( | ||||||
|  |   title: string, | ||||||
|  |   page: string = '1', | ||||||
|  |   type?: string, | ||||||
|  |   year?: string | ||||||
|  | ): Promise<movieResponse> => { | ||||||
|  |   let query = `http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}s=${title}&page=${page}` | ||||||
|  | 
 | ||||||
|  |   if (type) query += `&type=${type}` | ||||||
|  |   if (year) query += `&year=${year}` | ||||||
|  | 
 | ||||||
|  |   const response = await fetch(query) | ||||||
|  |     .then((response) => response.json()) | ||||||
|  |     .then((data) => { | ||||||
|  |       return data | ||||||
|  |     }) | ||||||
|  |     .catch((error) => { | ||||||
|  |       console.log('Error:', error) | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |   return response | ||||||
|  | } | ||||||
|  | @ -1,9 +1,12 @@ | ||||||
| import React from 'react' | import React from 'react' | ||||||
| import ReactDOM from 'react-dom/client' | import ReactDOM from 'react-dom/client' | ||||||
| import App from './App.tsx' | import App from './App.tsx' | ||||||
|  | import { BrowserRouter as Router } from 'react-router-dom' | ||||||
| 
 | 
 | ||||||
| ReactDOM.createRoot(document.getElementById('root')!).render( | ReactDOM.createRoot(document.getElementById('root')!).render( | ||||||
|   <React.StrictMode> |   <React.StrictMode> | ||||||
|     <App /> |     <Router> | ||||||
|  |       <App /> | ||||||
|  |     </Router> | ||||||
|   </React.StrictMode> |   </React.StrictMode> | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -7,3 +7,9 @@ export type movieObject = { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export type modalMovieType = movieObject | null | export type modalMovieType = movieObject | null | ||||||
|  | 
 | ||||||
|  | export type movieResponse = { | ||||||
|  |   Response: string | ||||||
|  |   totalResults: string | ||||||
|  |   Search: movieObject[] | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										20
									
								
								src/util/navigate.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/util/navigate.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | import { useNavigate } from 'react-router' | ||||||
|  | 
 | ||||||
|  | const navigate = useNavigate() | ||||||
|  | 
 | ||||||
|  | export const navigateToPage = ( | ||||||
|  |   title?: string, | ||||||
|  |   page: string = '1', | ||||||
|  |   type?: string, | ||||||
|  |   year?: string | ||||||
|  | ) => { | ||||||
|  |   if (!title) navigate('') | ||||||
|  |   else { | ||||||
|  |     let query = `?title=${title}&page=${page}` | ||||||
|  | 
 | ||||||
|  |     if (type) query += `&type=${type}` | ||||||
|  |     if (year) query += `&year=${year}` | ||||||
|  | 
 | ||||||
|  |     navigate(query) | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in a new issue
	
	 Sindre Kjelsrud
						Sindre Kjelsrud