Skip to content

Commit

Permalink
front and backend work!
Browse files Browse the repository at this point in the history
  • Loading branch information
abecerrilsalas committed Aug 1, 2024
1 parent c009c5d commit d29290b
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 113 deletions.
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"axios": "^1.7.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.25.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Expand Down
240 changes: 132 additions & 108 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,142 @@
import React, { useState, useEffect } from "react";
import axios from "axios";
import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Home from "./Home";
import Playlist from "./Playlist";
import Header from "./Header";
import TextInput from "./TextInput";
import Button from "./Button";
import Rectangle1 from "./Rectangle1";
import "./App.css";

function App() {
const [description, setDescription] = useState("");
const [recommendations, setRecommendations] = useState([]);
const [spotifyLink, setSpotifyLink] = useState(null);
const [error, setError] = useState(null);
const [authorized, setAuthorized] = useState(true);
const [sessionId, setSessionId] = useState(null);
const [loading, setLoading] = useState(false);
return (
<Router>
<div className="App">
<Header />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/playlist" element={<Playlist />} />
</Routes>
</div>
</Router>
);
}

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const sessionIdParam = urlParams.get("session_id");
if (sessionIdParam) {
setSessionId(sessionIdParam);
}
}, []);
export default App;

const handleInputChange = (e) => {
setDescription(e.target.value);
};
// import React, { useState, useEffect } from "react";
// import axios from "axios";
// import Header from "./Header";
// // import Home from "./Home";
// import TextInput from "./TextInput";
// import Button from "./Button";
// import Rectangle1 from "./Rectangle1";
// import "./App.css";

const handleGetRecommendations = async () => {
setLoading(true);
try {
const response = await axios.post(
`http://localhost:5000/recommend`,
{ description },
{ params: { session_id: sessionId } }
);
if (response && response.data) {
if (response.data.authorized) {
setRecommendations(response.data.recommendation);
setSpotifyLink(response.data.spotify_link);
setError(null);
setAuthorized(true);
} else {
setAuthorized(false);
}
} else {
console.error("Unexpected response structure:", response);
}
} catch (error) {
console.error("Error fetching recommendation:", error);
setError("Error fetching recommendation. Please try again.");
if (error.response && error.response.status === 401) {
setAuthorized(false);
}
} finally {
setLoading(false); // End loading
}
};
// function App() {
// const [description, setDescription] = useState("");
// const [recommendations, setRecommendations] = useState([]);
// const [spotifyLink, setSpotifyLink] = useState(null);
// const [error, setError] = useState(null);
// const [authorized, setAuthorized] = useState(true);
// const [sessionId, setSessionId] = useState(null);
// const [loading, setLoading] = useState(false);

return (
<div className="App">
<Header />
<Rectangle1
title="Mood Melody"
description="Discover music that fits your mood. + add description"
/>
// useEffect(() => {
// const urlParams = new URLSearchParams(window.location.search);
// const sessionIdParam = urlParams.get("session_id");
// if (sessionIdParam) {
// setSessionId(sessionIdParam);
// }
// }, []);

<TextInput
value={description}
onChange={handleInputChange}
placeholder="Enter the desired song qualities or mood..."
/>
<br />
<Button
type="button"
label={loading ? "Loading..." : "Get Recommendations"}
onClick={handleGetRecommendations}
disabled={loading}
/>
{error && <p style={{ color: "red" }}>{error}</p>}
{!authorized && (
<div>
<p style={{ color: "red" }}>You are not authorized in Spotify.</p>
<Button
type="button"
label="Authorize"
onClick={() =>
(window.location.href = "http://localhost:5000/auth/login")
}
/>
</div>
)}
{authorized && recommendations.length > 0 && (
<div>
<h2>Recommendations</h2>
<ul>
{recommendations.map((rec, index) => (
<li key={index}>{rec}</li>
))}
</ul>
{spotifyLink && (
<div>
<h2>Spotify Playlist</h2>
<p>
<a href={spotifyLink} target="_blank" rel="noopener noreferrer">
Open Playlist on Spotify
</a>
</p>
</div>
)}
</div>
)}
</div>
);
}
// const handleInputChange = (e) => {
// setDescription(e.target.value);
// };

export default App;
// const handleGetRecommendations = async () => {
// setLoading(true);
// try {
// const response = await axios.post(
// `http://127.0.0.1:5000/recommend`,
// { description },
// { params: { session_id: sessionId } }
// );
// if (response && response.data) {
// if (response.data.authorized) {
// setRecommendations(response.data.recommendation);
// setSpotifyLink(response.data.spotify_link);
// setError(null);
// setAuthorized(true);
// } else {
// setAuthorized(false);
// }
// } else {
// console.error("Unexpected response structure:", response);
// }
// } catch (error) {
// console.error("Error fetching recommendation:", error);
// setError("Error fetching recommendation. Please try again.");
// if (error.response && error.response.status === 401) {
// setAuthorized(false);
// }
// } finally {
// setLoading(false); // End loading
// }
// };

// return (
// <div className="App">
// <Header />
// <Rectangle1
// title="Mood Melody"
// description="Discover music that fits your mood. + add description"
// />

// <TextInput
// value={description}
// onChange={handleInputChange}
// placeholder="Enter the desired song qualities or mood..."
// />
// <br />
// <Button
// type="button"
// label={loading ? "Loading..." : "Get Recommendations"}
// onClick={handleGetRecommendations}
// disabled={loading}
// />
// {error && <p style={{ color: "red" }}>{error}</p>}
// {!authorized && (
// <div>
// <p style={{ color: "red" }}>You are not authorized in Spotify.</p>
// <Button
// type="button"
// label="Authorize"
// onClick={() =>
// (window.location.href = "http://127.0.0.1:5000/auth/login")
// }
// />
// </div>
// )}
// {authorized && recommendations.length > 0 && (
// <div>
// <h2>Recommendations</h2>
// <ul>
// {recommendations.map((rec, index) => (
// <li key={index}>{rec}</li>
// ))}
// </ul>
// {spotifyLink && (
// <div>
// <h2>Spotify Playlist</h2>
// <p>
// <a href={spotifyLink} target="_blank" rel="noopener noreferrer">
// Open Playlist on Spotify
// </a>
// </p>
// </div>
// )}
// </div>
// )}
// </div>
// );
// }

// export default App;
18 changes: 18 additions & 0 deletions src/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ function Header() {
}

export default Header;

// import React from "react";
// import "./Header.css";

// function Header() {
// return (
// <header className="header">
// <img
// src="/images/moodMelody_logo.jpg"
// alt="Mood Melody logo"
// style={{ height: "50px", width: "auto" }}
// />
// <h1>Mood Melody</h1>
// </header>
// );
// }

// export default Header;
Loading

0 comments on commit d29290b

Please sign in to comment.