Update src/App.js
Browse files- src/App.js +31 -18
src/App.js
CHANGED
|
@@ -8,21 +8,25 @@ function App() {
|
|
| 8 |
const [loading, setLoading] = useState(false);
|
| 9 |
const [error, setError] = useState('');
|
| 10 |
|
| 11 |
-
// Fetch
|
| 12 |
useEffect(() => {
|
| 13 |
fetch('/api/movies')
|
| 14 |
.then(response => response.json())
|
| 15 |
.then(data => {
|
|
|
|
| 16 |
if(Array.isArray(data.movies)){
|
| 17 |
setMovies(data.movies);
|
| 18 |
-
if
|
| 19 |
setSelectedMovie(data.movies[0]);
|
| 20 |
}
|
| 21 |
} else {
|
| 22 |
-
console.error("
|
| 23 |
}
|
| 24 |
})
|
| 25 |
-
.catch(err =>
|
|
|
|
|
|
|
|
|
|
| 26 |
}, []);
|
| 27 |
|
| 28 |
const handleRecommend = () => {
|
|
@@ -33,9 +37,7 @@ function App() {
|
|
| 33 |
|
| 34 |
fetch(`/api/recommend?title=${encodeURIComponent(selectedMovie)}`)
|
| 35 |
.then(response => {
|
| 36 |
-
if (!response.ok)
|
| 37 |
-
throw new Error('Network response was not ok');
|
| 38 |
-
}
|
| 39 |
return response.json();
|
| 40 |
})
|
| 41 |
.then(data => {
|
|
@@ -51,13 +53,11 @@ function App() {
|
|
| 51 |
|
| 52 |
// Fetch poster URL helper
|
| 53 |
const fetchPosterUrl = async (movieId) => {
|
| 54 |
-
const apiKey = "4f26810245b768489a195238ffe92a0b"; // Replace with your
|
| 55 |
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}`;
|
| 56 |
try {
|
| 57 |
const response = await fetch(url);
|
| 58 |
-
if (!response.ok)
|
| 59 |
-
throw new Error('Failed fetching poster');
|
| 60 |
-
}
|
| 61 |
const data = await response.json();
|
| 62 |
if (data.poster_path) {
|
| 63 |
return `https://image.tmdb.org/t/p/w500/${data.poster_path}`;
|
|
@@ -68,14 +68,14 @@ function App() {
|
|
| 68 |
return "https://via.placeholder.com/500x750.png?text=No+Poster";
|
| 69 |
};
|
| 70 |
|
| 71 |
-
// Poster component
|
| 72 |
const Poster = ({ movieId, title }) => {
|
| 73 |
const [posterUrl, setPosterUrl] = useState("https://via.placeholder.com/500x750.png?text=Loading...");
|
| 74 |
|
| 75 |
useEffect(() => {
|
| 76 |
let mounted = true;
|
| 77 |
fetchPosterUrl(movieId).then(url => {
|
| 78 |
-
if(mounted) setPosterUrl(url);
|
| 79 |
});
|
| 80 |
return () => { mounted = false; };
|
| 81 |
}, [movieId]);
|
|
@@ -91,22 +91,34 @@ function App() {
|
|
| 91 |
return (
|
| 92 |
<div className="App">
|
| 93 |
<header className="App-header">
|
| 94 |
-
<h1>
|
| 95 |
<p>Select a movie you like to get 10 similar recommendations.</p>
|
| 96 |
</header>
|
|
|
|
| 97 |
<div className="controls">
|
| 98 |
-
<select
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
))}
|
| 102 |
</select>
|
|
|
|
| 103 |
<button onClick={handleRecommend} disabled={loading || !selectedMovie}>
|
| 104 |
{loading ? 'Searching...' : 'Recommend'}
|
| 105 |
</button>
|
| 106 |
</div>
|
|
|
|
| 107 |
{error && <p className="error">{error}</p>}
|
|
|
|
| 108 |
<div className="recommendation-grid">
|
| 109 |
-
{recommendations.map(
|
| 110 |
<Poster key={rec.id} movieId={rec.id} title={rec.title} />
|
| 111 |
))}
|
| 112 |
</div>
|
|
@@ -115,3 +127,4 @@ function App() {
|
|
| 115 |
}
|
| 116 |
|
| 117 |
export default App;
|
|
|
|
|
|
| 8 |
const [loading, setLoading] = useState(false);
|
| 9 |
const [error, setError] = useState('');
|
| 10 |
|
| 11 |
+
// Fetch movie list on mount
|
| 12 |
useEffect(() => {
|
| 13 |
fetch('/api/movies')
|
| 14 |
.then(response => response.json())
|
| 15 |
.then(data => {
|
| 16 |
+
console.log("Movies fetched:", data.movies); // Debug log
|
| 17 |
if(Array.isArray(data.movies)){
|
| 18 |
setMovies(data.movies);
|
| 19 |
+
if(data.movies.length > 0){
|
| 20 |
setSelectedMovie(data.movies[0]);
|
| 21 |
}
|
| 22 |
} else {
|
| 23 |
+
console.error("Movies data is not an array:", data.movies);
|
| 24 |
}
|
| 25 |
})
|
| 26 |
+
.catch(err => {
|
| 27 |
+
console.error("Failed to fetch movies:", err);
|
| 28 |
+
setError('Failed to fetch movie list');
|
| 29 |
+
});
|
| 30 |
}, []);
|
| 31 |
|
| 32 |
const handleRecommend = () => {
|
|
|
|
| 37 |
|
| 38 |
fetch(`/api/recommend?title=${encodeURIComponent(selectedMovie)}`)
|
| 39 |
.then(response => {
|
| 40 |
+
if (!response.ok) throw new Error('Network response was not ok');
|
|
|
|
|
|
|
| 41 |
return response.json();
|
| 42 |
})
|
| 43 |
.then(data => {
|
|
|
|
| 53 |
|
| 54 |
// Fetch poster URL helper
|
| 55 |
const fetchPosterUrl = async (movieId) => {
|
| 56 |
+
const apiKey = "4f26810245b768489a195238ffe92a0b"; // Replace with your TMDB API key
|
| 57 |
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}`;
|
| 58 |
try {
|
| 59 |
const response = await fetch(url);
|
| 60 |
+
if (!response.ok) throw new Error('Failed fetching poster');
|
|
|
|
|
|
|
| 61 |
const data = await response.json();
|
| 62 |
if (data.poster_path) {
|
| 63 |
return `https://image.tmdb.org/t/p/w500/${data.poster_path}`;
|
|
|
|
| 68 |
return "https://via.placeholder.com/500x750.png?text=No+Poster";
|
| 69 |
};
|
| 70 |
|
| 71 |
+
// Poster component for displaying movie posters
|
| 72 |
const Poster = ({ movieId, title }) => {
|
| 73 |
const [posterUrl, setPosterUrl] = useState("https://via.placeholder.com/500x750.png?text=Loading...");
|
| 74 |
|
| 75 |
useEffect(() => {
|
| 76 |
let mounted = true;
|
| 77 |
fetchPosterUrl(movieId).then(url => {
|
| 78 |
+
if (mounted) setPosterUrl(url);
|
| 79 |
});
|
| 80 |
return () => { mounted = false; };
|
| 81 |
}, [movieId]);
|
|
|
|
| 91 |
return (
|
| 92 |
<div className="App">
|
| 93 |
<header className="App-header">
|
| 94 |
+
<h1>PragyanAI - Super 30 Movie Recommender</h1>
|
| 95 |
<p>Select a movie you like to get 10 similar recommendations.</p>
|
| 96 |
</header>
|
| 97 |
+
|
| 98 |
<div className="controls">
|
| 99 |
+
<select
|
| 100 |
+
value={selectedMovie}
|
| 101 |
+
onChange={e => {
|
| 102 |
+
console.log("Selected movie changed to:", e.target.value); // Debug log
|
| 103 |
+
setSelectedMovie(e.target.value);
|
| 104 |
+
}}
|
| 105 |
+
>
|
| 106 |
+
{movies.map(movie => (
|
| 107 |
+
<option key={movie} value={movie}>
|
| 108 |
+
{movie}
|
| 109 |
+
</option>
|
| 110 |
))}
|
| 111 |
</select>
|
| 112 |
+
|
| 113 |
<button onClick={handleRecommend} disabled={loading || !selectedMovie}>
|
| 114 |
{loading ? 'Searching...' : 'Recommend'}
|
| 115 |
</button>
|
| 116 |
</div>
|
| 117 |
+
|
| 118 |
{error && <p className="error">{error}</p>}
|
| 119 |
+
|
| 120 |
<div className="recommendation-grid">
|
| 121 |
+
{recommendations.map(rec => (
|
| 122 |
<Poster key={rec.id} movieId={rec.id} title={rec.title} />
|
| 123 |
))}
|
| 124 |
</div>
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
export default App;
|
| 130 |
+
|