From cc8804672c4f05defe70856dfc308bfd7966047e Mon Sep 17 00:00:00 2001 From: fede Date: Wed, 2 Apr 2025 21:42:46 -0300 Subject: [PATCH] feat: title y artist edit --- app/Http/Controllers/SongController.php | 31 +++++++--- .../js/Components/ModalEditarCancion.tsx | 56 +++++++++++++++++++ resources/js/Pages/Songs/Index.tsx | 39 ++++++++++++- resources/js/types/types.d.ts | 2 +- 4 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 resources/js/Components/ModalEditarCancion.tsx diff --git a/app/Http/Controllers/SongController.php b/app/Http/Controllers/SongController.php index dcfca2c..e8d08f9 100644 --- a/app/Http/Controllers/SongController.php +++ b/app/Http/Controllers/SongController.php @@ -43,14 +43,29 @@ class SongController extends Controller 'data' => $ret ? "Guardado " . $song->title : 'sin archivo' ]); } - /** - * @return void - * @param mixed $id - */ - public function update(Request $request, $id):void - { - // Update specified song - } + /** + * @param int $id + */ + public function update(Request $request, $id) + { + // Actualizar cancion + $song = Song::find($id); + $this->authorize("update", $song); + + $validated = $request->validate([ + 'title' => 'required|string', + 'artist' => 'required|string', + ]); + + $song->title = $validated['title']; + $song->artist = $validated['artist']; + $song->save(); + + return response()->json([ + 'data' => 'Updated successfully' + ]); + } + /** * @param int $id */ diff --git a/resources/js/Components/ModalEditarCancion.tsx b/resources/js/Components/ModalEditarCancion.tsx new file mode 100644 index 0000000..6fccedd --- /dev/null +++ b/resources/js/Components/ModalEditarCancion.tsx @@ -0,0 +1,56 @@ +import { Cancion } from '@/types/types'; +import { Input } from '@headlessui/react'; +import { FormEvent } from 'react'; +import InputLabel from './InputLabel'; +import Modal from './Modal'; +import PrimaryButton from './PrimaryButton'; + +export default function ModalEditarCancion({ + showEditModal, + UpdateCallback, + setShowEditModal, + song, +}: { + song: Cancion; + showEditModal: boolean; + UpdateCallback: (event: FormEvent) => void; + setShowEditModal: (arg0: boolean) => void; +}) { + return ( + setShowEditModal(false)} + closeable={true} + maxWidth="md" + > +
+

+ Editando Datos Cancion +

+
+
+ Titulo + + Artista + +
+
+ + Subir Cambios + +
+
+
+
+ ); +} diff --git a/resources/js/Pages/Songs/Index.tsx b/resources/js/Pages/Songs/Index.tsx index 4aa4212..ded25f6 100644 --- a/resources/js/Pages/Songs/Index.tsx +++ b/resources/js/Pages/Songs/Index.tsx @@ -1,15 +1,17 @@ import BotonAdd from '@/Components/BotonAdd'; import ModalAñadirCancion from '@/Components/ModalAñadirCancion'; +import ModalEditarCancion from '@/Components/ModalEditarCancion'; import ModalRemoveCancion from '@/Components/ModalRemoveCancion'; import Authenticated from '@/Layouts/AuthenticatedLayout'; import { Cancion } from '@/types/types'; -import { Head } from '@inertiajs/react'; +import { Head, router } from '@inertiajs/react'; import axios from 'axios'; import { FormEvent, useEffect, useState } from 'react'; export default function Index({ songs }: { songs: Cancion[] }) { const [showaddmodal, setshowmodal] = useState(false); const [showRemove, setRemove] = useState(false); + const [showEdit, setEdit] = useState(false); const [selcan, setcan] = useState({} as Cancion); useEffect(() => { @@ -62,6 +64,27 @@ export default function Index({ songs }: { songs: Cancion[] }) { }); } + function handleUpdate(event: FormEvent) { + event.preventDefault(); + const formData = new FormData(event.currentTarget); + const title = formData.get('title'); + const artist = formData.get('artist'); + + axios + .put(`/canciones/${selcan.id}`, { + title: title, + artist: artist, + }) + .then(() => { + setEdit(false); + router.reload(); + }) + .catch((error) => { + console.error('Error actualizando datos de la cancion', error); + setEdit(false); + }); + } + return ( -