Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added top-song working in Home page music-player #479

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions frontend/src/components/Home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import { fetchTopSongs, fetchSonsgByName, secIntoMinSec } from "../../Utils";
import { db } from "../Auth/firebase";
import { doc, getDoc } from "firebase/firestore";

function Main() {
function Main({ topSongs }) {
const [currentArtist, setCurrentArtist] = useState(null);
const [currentSong, setCurrentSong] = useState([]);
const [recentlyPlayedSongs, setRecentlyPlayedSongs] = useState([]);

useEffect(() => {
setCurrentSong(topSongs.length > 0 ? topSongs[0] : topSongs)
}, [topSongs])

useEffect(() => {
const fetchRecentlyPlayedSongs = async () => {
try {
Expand Down Expand Up @@ -46,9 +50,8 @@ function Main() {
} else {
console.log("User document does not exist");
}
} else {
console.log("User details not available");
}
console.log("User details not available");
} catch (error) {
console.error("Error fetching recently played songs:", error);
}
Expand Down Expand Up @@ -80,6 +83,7 @@ function Main() {
<div className="flex h-[60%] gap-4 w-full">
<Genres />
<TopSongs
topSongs={topSongs}
currentArtist={currentArtist}
setCurrentSong={setCurrentSong}
/>
Expand Down Expand Up @@ -222,8 +226,7 @@ export function TopSongsElement({
);
}

function TopSongs({ currentArtist, setCurrentSong }) {
const [topSongs, setTopSongs] = useState([]);
function TopSongs({ topSongs, currentArtist, setCurrentSong }) {
const [title, setTitle] = useState("Top Songs");

useEffect(() => {
Expand All @@ -232,7 +235,6 @@ function TopSongs({ currentArtist, setCurrentSong }) {
await fetchSonsgByName(currentArtist, setTopSongs);
setTitle(currentArtist);
} else {
await fetchTopSongs(setTopSongs);
// setTopSongs(topSongsData);
setTitle("Top Songs");
}
Expand Down Expand Up @@ -280,7 +282,6 @@ function TopArtistElement({
}) {
return (
<div
key={id}
className="flex flex-1 p-2 flex-col gap-2 hover:cursor-pointer"
onClick={() => onClick(name)}
>
Expand Down Expand Up @@ -321,9 +322,9 @@ function TopArtists({ setCurrentArtist }) {
{artists &&
artists.songs
.slice(0, 8)
.map((artist) => (
.map((artist, index) => (
<TopArtistElement
key={artist.rank}
key={index}
id={artist.id}
name={artist.artist}
weeks_on_chart={artist.position.weeksOnChart}
Expand All @@ -339,11 +340,20 @@ function TopArtists({ setCurrentArtist }) {
}

function Home({ setCurrPage }) {
const [topSongs, setTopSongs] = useState([]);

useEffect(() => {
const fetchTopSongsData = async () => {
await fetchTopSongs(setTopSongs);
};
fetchTopSongsData();
}, [])

return (
<div className="w-screen h-screen p-4 text-center">
<div className="w-full h-full flex gap-4">
<Navbar setCurrPage={setCurrPage} />
<Main />
<Main topSongs={topSongs} />
</div>
<Footer />
</div>
Expand Down
Loading