From 0016074dbadab9ea377ae031759a4bc0bcdf828f Mon Sep 17 00:00:00 2001 From: Chelsea Date: Wed, 15 May 2024 20:17:27 +0530 Subject: [PATCH] playlist --- frontend/src/Playlist.jsx | 119 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 frontend/src/Playlist.jsx diff --git a/frontend/src/Playlist.jsx b/frontend/src/Playlist.jsx new file mode 100644 index 0000000..98ff960 --- /dev/null +++ b/frontend/src/Playlist.jsx @@ -0,0 +1,119 @@ +import React, { useState, useEffect } from 'react'; +import firebase from 'firebase/app'; +import 'firebase/auth'; +import 'firebase/firestore'; + +// Initialize Firebase +const firebaseConfig = { + // Your Firebase config here + apiKey: "", + authDomain: "spring-music-player-e7c5e.firebaseapp.com", + projectId: "", + storageBucket: ".appspot.com", + messagingSenderId: "", + appId: ":web:", + measurementId: "" +}; + +if (!firebase.apps.length) { + firebase.initializeApp(firebaseConfig); +} + +const db = firebase.firestore(); + +const PlaylistCreator = ({ currentUser }) => { + const [playlistName, setPlaylistName] = useState(''); + const [songs, setSongs] = useState([]); + + const handleAddSong = (song) => { + setSongs([...songs, song]); + }; + + const handleSavePlaylist = () => { + db.collection('playlists').add({ + userId: currentUser.uid, + playlistName, + songs, + }) + .then(() => { + console.log('Playlist saved successfully!'); + setPlaylistName(''); + setSongs([]); + }) + .catch((error) => { + console.error('Error saving playlist: ', error); + }); + }; + + return ( +
+

Create Playlist

+ setPlaylistName(e.target.value)} + /> + + {/* Component to add songs to playlist */} +
+ ); +}; + +const PlaylistViewer = () => { + const [playlists, setPlaylists] = useState([]); + + useEffect(() => { + const unsubscribe = db.collection('playlists').onSnapshot((snapshot) => { + const updatedPlaylists = snapshot.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })); + setPlaylists(updatedPlaylists); + }); + + return () => unsubscribe(); + }, []); + + return ( +
+

Other People's Playlists

+ +
+ ); +}; + +const App = () => { + const [currentUser, setCurrentUser] = useState(null); + + useEffect(() => { + const unsubscribe = firebase.auth().onAuthStateChanged((user) => { + setCurrentUser(user); + }); + + return () => unsubscribe(); + }, []); + + return ( +
+ {currentUser ? ( +
+

Welcome, {currentUser.displayName}!

+ + +
+ ) : ( +

Please sign in to use the app

+ )} +
+ ); +}; + +export default App;