✨ fetches movie from api
Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
		
							parent
							
								
									48c24a2ff0
								
							
						
					
					
						commit
						df2787406b
					
				
					 3 changed files with 23 additions and 20 deletions
				
			
		|  | @ -10,6 +10,7 @@ | ||||||
|     "preview": "vite preview" |     "preview": "vite preview" | ||||||
|   }, |   }, | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|  |     "git-mob": "^2.4.0", | ||||||
|     "react": "^18.2.0", |     "react": "^18.2.0", | ||||||
|     "react-dom": "^18.2.0" |     "react-dom": "^18.2.0" | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
							
								
								
									
										42
									
								
								src/App.tsx
									
										
									
									
									
								
							
							
						
						
									
										42
									
								
								src/App.tsx
									
										
									
									
									
								
							|  | @ -1,33 +1,35 @@ | ||||||
| import { useState } from 'react' | import { useEffect, useState } from 'react' | ||||||
| import reactLogo from './assets/react.svg' | import reactLogo from './assets/react.svg' | ||||||
| import viteLogo from '/vite.svg' | import viteLogo from '/vite.svg' | ||||||
| import './App.css' | import './App.css' | ||||||
| 
 | 
 | ||||||
| function App() { | function App() { | ||||||
|   const [count, setCount] = useState(0) | 
 | ||||||
|  |   const API_MOVIE_KEY = import.meta.env.VITE_MOVIE_API_KEY; | ||||||
|  |   const [movie, setMovie] = useState(null) | ||||||
|  |   const [loading, setLoading] = useState(true) | ||||||
|  | 
 | ||||||
|  |   useEffect(() => { | ||||||
|  |     fetch(`http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&t=batman`) | ||||||
|  |       .then((response) => response.json()) | ||||||
|  |       .then((data) => { | ||||||
|  |         setMovie(data) | ||||||
|  |       } | ||||||
|  |       ) | ||||||
|  |       .then(() => setLoading(false)) | ||||||
|  |       .catch((error) => console.log(error)) | ||||||
|  |   }, []) | ||||||
| 
 | 
 | ||||||
|   return ( |   return ( | ||||||
|     <> |     <> | ||||||
|       <div> |       <div> | ||||||
|         <a href="https://vitejs.dev" target="_blank"> |         {!loading && movie ? ( | ||||||
|           <img src={viteLogo} className="logo" alt="Vite logo" /> |           <div> | ||||||
|         </a> |             <h1>{movie.Title}</h1> | ||||||
|         <a href="https://react.dev" target="_blank"> |             <img src={movie.Poster} alt={movie.Title} /> | ||||||
|           <img src={reactLogo} className="logo react" alt="React logo" /> |             </div> | ||||||
|         </a> |         ):(<h1>Loading...</h1>)} | ||||||
|       </div> |       </div> | ||||||
|       <h1>Vite + React</h1> |  | ||||||
|       <div className="card"> |  | ||||||
|         <button onClick={() => setCount((count) => count + 1)}> |  | ||||||
|           count is {count} |  | ||||||
|         </button> |  | ||||||
|         <p> |  | ||||||
|           Edit <code>src/App.tsx</code> and save to test HMR |  | ||||||
|         </p> |  | ||||||
|       </div> |  | ||||||
|       <p className="read-the-docs"> |  | ||||||
|         Click on the Vite and React logos to learn more |  | ||||||
|       </p> |  | ||||||
|     </> |     </> | ||||||
|   ) |   ) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										0
									
								
								src/types/movie.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/types/movie.ts
									
										
									
									
									
										Normal file
									
								
							
		Reference in a new issue
	
	 haraldnilsen
						haraldnilsen