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

front end complete #8

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
songs index route complete
  • Loading branch information
OscarQ5 committed Nov 6, 2023
commit e0a6d08c28c6c28e0df776d5560488d77a110c60
2 changes: 2 additions & 0 deletions vite-front-end/src/App.jsx
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import { useState } from 'react'
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'
import NavBar from './components/NavBar.jsx'
import Home from './pages/Home.jsx'
import Index from './pages/Index.jsx'
import './App.css'

function App() {
@@ -13,6 +14,7 @@ function App() {
<main>
<Routes>
<Route path='/' element={<Home />}/>
<Route path='/songs' element={<Index />}/>
</Routes>
</main>
</Router>
26 changes: 26 additions & 0 deletions vite-front-end/src/components/Song.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Link } from "react-router-dom";

export default function Song({ song }) {
return (
<tr>
<td>
{song.is_favorite ? (
<span>⭐️</span>
) : (
<span>&nbsp; &nbsp; &nbsp;</span>
)}
</td>
<td>
<Link to={`/songs/${song.id}`}> {song.name}</Link>
</td>
<td>
{song.artist}
</td>
<td>
<span>
{song.time}
</span>
</td>
</tr>
)
}
42 changes: 42 additions & 0 deletions vite-front-end/src/components/Songs.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from "react";
import Song from './Song.jsx'

const API = import.meta.env.VITE_API_URL;

export default function Songs() {
const [songs, setSongs] = useState([]);

const fetchSongs = async () => {
try {
fetch(`${API}/songs`)
.then(res => res.json())
.then(data => setSongs(data))
} catch (err) {
return err;
}
};

useEffect(() => {
fetchSongs()
}, [])

return (
<div>
<section>
<table>
<thead>
<tr>
<th>Favorite</th>
<th>Song</th>
<th>Artist</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{songs.map(song => <Song key={song.id} song={song} />)}
</tbody>
</table>
</section>
</div>
)
}
10 changes: 10 additions & 0 deletions vite-front-end/src/pages/Index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Songs from '../components/Songs.jsx';

export default function Index() {
return(
<div>
<h2>Index</h2>
<Songs />
</div>
)
}