From dc91d52b186d4915cf17776ea019d662934807fd Mon Sep 17 00:00:00 2001 From: Phill Pover Date: Sun, 6 Apr 2025 19:33:33 +0100 Subject: [PATCH] Attempting an edit form --- frontend/src/app/actions.tsx | 43 +++++++++++++ frontend/src/app/album/[id]/page.tsx | 58 +++++++++++++++++- frontend/src/app/album/page.tsx | 21 +++++-- .../app/components/modals/create-album.tsx | 61 ------------------- 4 files changed, 116 insertions(+), 67 deletions(-) delete mode 100644 frontend/src/app/components/modals/create-album.tsx diff --git a/frontend/src/app/actions.tsx b/frontend/src/app/actions.tsx index 709e6f1..8cbf265 100644 --- a/frontend/src/app/actions.tsx +++ b/frontend/src/app/actions.tsx @@ -42,3 +42,46 @@ export async function deleteAlbum(formData: FormData) { }) }); } + +export async function createSong(formData: FormData) { + const albumId = formData.get('album-id'); + const title = formData.get('title'); + const duration = formData.get('duration'); + return await fetch("https://api.anatid.net/song", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + albumId: albumId, + title: title, + duration: duration, + }) + }); +} + +export async function updateSong(formData: FormData) { + const id = formData.get('id'); + const albumId = formData.get('album-id'); + const title = formData.get('title'); + const duration = formData.get('duration'); + return await fetch("https://api.anatid.net/song", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + id: id, + albumId: albumId, + title: title, + duration: duration, + }) + }); +} + +export async function deleteSong(formData: FormData) { + const id = formData.get('id'); + return await fetch("https://api.anatid.net/song", { + method: "DELETE", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + id: id, + }) + }); +} diff --git a/frontend/src/app/album/[id]/page.tsx b/frontend/src/app/album/[id]/page.tsx index 03c8a31..ad15b3a 100644 --- a/frontend/src/app/album/[id]/page.tsx +++ b/frontend/src/app/album/[id]/page.tsx @@ -1,13 +1,19 @@ 'use client' -import { useState, useEffect } from 'react' +import { FormEvent, 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 } from '@/app/actions'; +import Button from 'react-bootstrap/Button'; +import Modal from 'react-bootstrap/Modal'; export default function Page() { const [album, setAlbum] = useState(); + const [show, setShow] = useState(false); + const handleClose = () => setShow(false); + const handleShow = () => setShow(true); const params = useParams<{ id: string }>(); const id = params.id; @@ -21,14 +27,32 @@ export default function Page() { fetchAlbum(id); }, [id]); + const handleSubmit = async (event: FormEvent) => { + event.preventDefault(); + handleClose(); + + const formData = new FormData(event.currentTarget); + try { + const response = await createSong(formData); + const data = response.json(); + console.log(data); + } catch (error) { + console.error('Error creating Song:', error); + } + } + if (!album) return
Loading...
return ( + <>
{album.title} by {album.artist} ({album.genre})
+ @@ -47,5 +71,37 @@ export default function Page() {
+ + + + Create Song + + +
+
+
+
+ +
+
+ +
+
+ +
+
+
+ + + +
+ ); } diff --git a/frontend/src/app/album/page.tsx b/frontend/src/app/album/page.tsx index 8c12e68..c4aa98f 100644 --- a/frontend/src/app/album/page.tsx +++ b/frontend/src/app/album/page.tsx @@ -4,7 +4,6 @@ import { FormEvent, useState, useEffect } from 'react'; import Link from 'next/link'; import { Album } from '@/entities/album.entity'; import { createAlbum } from '@/app/actions'; -// import CreateAlbumModal from "@/app/components/modals/create-album"; import Button from 'react-bootstrap/Button'; import Modal from 'react-bootstrap/Modal'; @@ -13,6 +12,7 @@ export default function Page() { const [show, setShow] = useState(false); const handleClose = () => setShow(false); const handleShow = () => setShow(true); + let [formAlbumTitle, formAlbumArtist, formAlbumGenre] = ''; useEffect(() => { async function fetchAlbums() { @@ -37,6 +37,13 @@ export default function Page() { } } + const handleEdit = async () => { + formAlbumTitle = "Monkey"; + formAlbumArtist = "Donkey"; + formAlbumGenre = "Pop"; + handleShow(); + } + if (!albums) return
Loading...
return ( @@ -70,7 +77,11 @@ export default function Page() { {album.genre} - + + + ))} @@ -92,15 +103,15 @@ export default function Page() {
- +
- +
- +
diff --git a/frontend/src/app/components/modals/create-album.tsx b/frontend/src/app/components/modals/create-album.tsx deleted file mode 100644 index 6d124b2..0000000 --- a/frontend/src/app/components/modals/create-album.tsx +++ /dev/null @@ -1,61 +0,0 @@ -"use client"; - -import { FormEvent } from 'react'; -import {useSearchParams, usePathname} from "next/navigation"; -import Link from "next/link"; -import { createAlbum } from '@/app/actions'; -import { useRouter } from 'next/navigation'; - -function CreateAlbumModal() { - const searchParams = useSearchParams(); - const modal = searchParams.get("modal"); - const pathname = usePathname(); - const router = useRouter(); - - async function onSubmit(event: FormEvent) { - event.preventDefault(); - - const formData = new FormData(event.currentTarget); - const response = await createAlbum(formData); - - await response.json(); - router.refresh(); - } - - return ( - <> - {modal && - -
- -
-
- -
-
- -
-
-
-
- -
-
-
-
- -
-
- - - - -
-
- } - - ); -} - -export default CreateAlbumModal;