diff --git a/frontend/src/app/album/[id]/page.tsx b/frontend/src/app/album/[id]/page.tsx index 272b41b..ebb11d0 100644 --- a/frontend/src/app/album/[id]/page.tsx +++ b/frontend/src/app/album/[id]/page.tsx @@ -1,19 +1,28 @@ 'use client' -import { FormEvent, useState, useEffect } from 'react' +import { FormEvent, MouseEvent, useState, useEffect } from 'react'; import { useParams } from 'next/navigation' import { Album } from '@/entities/album.entity'; import { Song } from '@/entities/song.entity'; import { TimeUtils } from '@/utils/time.util'; -import { createSong, getAlbum } from '@/app/actions'; +import { createSong, deleteSong, getAlbum, updateSong } from '@/app/actions'; import Button from 'react-bootstrap/Button'; import Modal from 'react-bootstrap/Modal'; +import { IconButton } from '@mui/material'; +import { AddCircleOutline, Delete, Edit } from '@mui/icons-material'; export default function Page() { const [album, setAlbum] = useState(); const [show, setShow] = useState(false); const handleClose = () => setShow(false); const handleShow = () => setShow(true); + const [formAlbumId, setFormAlbumId] = useState(""); + const [formSongId, setFormSongId] = useState(""); + const [formSongTitle, setFormSongTitle] = useState(""); + const [formSongDuration, setFormSongDuration] = useState(""); + const [formSongTrackNumber, setFormSongTrackNumber] = useState(""); + const [formModalTitle, setFormModalTitle] = useState("Add Song"); + const [formModalButtonLabel, setFormModalButtonLabel] = useState("Add"); const params = useParams<{ id: string }>(); const id = params.id; @@ -36,10 +45,60 @@ export default function Page() { const formData = new FormData(event.currentTarget); try { - const data = await createSong(formData); - console.log(data); + if (!formData.get('id')) { + const data = await createSong(formData); + console.log(data); + } else { + const data = await updateSong(formData); + console.log(data); + } } catch (error) { - console.error('Error creating Song:', error); + console.error("Error creating Song: ", error); + } + } + + const handleCreate = async () => { + setFormModalTitle("Add Song"); + setFormModalButtonLabel("Add"); + setFormAlbumId(""); + setFormSongId(""); + setFormSongTitle(""); + setFormSongDuration(""); + setFormSongTrackNumber(""); + handleShow(); + } + + const handleEdit = async (event: MouseEvent) => { + const id = event.currentTarget.getAttribute('song-id'); + if (id) { + try { + const data = await getAlbum(parseInt(id)); + setFormModalTitle("Edit Song"); + setFormModalButtonLabel("Save"); + setFormSongId(data.id) + setFormAlbumId(data.albumId); + setFormSongTitle(data.title); + setFormSongDuration(data.duration); + setFormSongTrackNumber(data.trackNumber); + handleShow(); + } catch (error) { + console.error(`Error getting song with ID ${id}:`, error); + } + } else { + console.error("Couldn't get ID of clicked element"); + } + } + + const handleDelete = async (event: MouseEvent) => { + const id = event.currentTarget.getAttribute('song-id'); + if (id) { + try { + await deleteSong(parseInt(id)); + } catch (error) { + console.error(`Error deleting song with ID ${id}:`, error); + } + } else { + console.error("Couldn't get ID of clicked element"); } } @@ -52,15 +111,16 @@ export default function Page() {
{album.title} by {album.artist} ({album.genre})
- + + + + @@ -69,6 +129,14 @@ export default function Page() { + ))} @@ -83,25 +151,27 @@ export default function Page() { keyboard={false} > - Create Song + {formModalTitle}
+ +
- +
- +
- +
- +
# Title DurationControls
{song.trackNumber} {song.title} {TimeUtils.fancyTimeFormat(song.duration)} + + + + + + +