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

prueba1 #164

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 163 additions & 68 deletions playbeat/src/components/AddAudio.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,90 @@ import { getAudio } from '@/utils/getAudio.ts';

let mensaje = "";

if (Astro.request.method ==="POST" ) {
try{
interface Params {
titulo?: string;
esPrivada?: boolean;
img?: string;
esAlbum?: boolean;
esPodcast?: boolean;
fechaLanz?: string;
duracionSeg?: number;
cancion?: any;
idsUsuarios?: string;
tipoEtiqueta?: string;
etiquetas?: string;
}
const data = await Astro.request.formData();
const token = Astro.cookies.get(TOKEN);
const file = data.get("photo") as File
let params : Params ={}
if(file.size !== 0){
const img = await uploadImg(file, token?.value)
params["img"] = img.id
}
let can = data.get("cancion") as File
if(can.name != ""){
params['cancion']= can
params["duracionSeg"] = parseFloat(data.get("duracion") as string) || 0
}
params["titulo"] = data.get("titulo") as string
const esPrivada = data.get("esPrivada")
if(esPrivada === null){
params["esPrivada"] = false
}else{
params["esPrivada"] = true
}
// if (Astro.request.method ==="POST" ) {
// console.log("post")
// try{
// interface Params {
// titulo?: string;
// esPrivada?: boolean;
// img?: string;
// esAlbum?: boolean;
// esPodcast?: boolean;
// fechaLanz?: string;
// duracionSeg?: number;
// cancion?: any;
// idsUsuarios?: string;
// tipoEtiqueta?: string;
// etiquetas?: string;
// }
// const data = await Astro.request.formData();
// const token = Astro.cookies.get(TOKEN);
// const file = data.get("photo") as File
// let params : Params ={}
// if(file.size !== 0){
// const img = await uploadImg(file, token?.value)
// params["img"] = img.id
// }
// let can = data.get("cancion") as File
// if(can.name != ""){
// params['cancion']= can
// params["duracionSeg"] = parseFloat(data.get("duracion") as string) || 0
// }
// params["titulo"] = data.get("titulo") as string
// const esPrivada = data.get("esPrivada")
// if(esPrivada === null){
// params["esPrivada"] = false
// }else{
// params["esPrivada"] = true
// }

let features = Astro.cookies.get('artistList')?.value;
if(features=== undefined || features.length === 2){
features = "";
}else{
params["idsUsuarios"] = features.slice(1, -1);
}
// let features = Astro.cookies.get('artistList')?.value;
// if(features=== undefined || features.length === 2){
// features = "";
// }else{
// params["idsUsuarios"] = features.slice(1, -1);
// }


const tags = data.getAll("tags")
let esPodcast = false;
let typeEtiqueta = "Cancion";
typeEtiqueta = data.get("tabsaudio") as string;
// const tags = data.getAll("tags")
// let esPodcast = false;
// let typeEtiqueta = "Cancion";
// typeEtiqueta = data.get("tabsaudio") as string;

if(typeEtiqueta == "Podcast"){
esPodcast = true;
}
params["esPodcast"] = esPodcast
if(tags.length !== 0){
// if(typeEtiqueta == "Podcast"){
// esPodcast = true;
// }
// params["esPodcast"] = esPodcast
// if(tags.length !== 0){


params["tipoEtiqueta"] = typeEtiqueta
params["etiquetas"] = tags.join(",")
}
params["fechaLanz"] = new Date().toISOString()
params["esAlbum"] = false
console.log(params)
const responseCreate = await createAudio(params, token?.value)
if(responseCreate && responseCreate.status === 200){
mensaje = "Audio subido correctamente";
}
} catch (error) {
if (error instanceof TypeError) {
console.error("Error en la petición");
}
// params["tipoEtiqueta"] = typeEtiqueta
// params["etiquetas"] = tags.join(",")
// }
// params["fechaLanz"] = new Date().toISOString()
// params["esAlbum"] = false
// console.log(params)
// const responseCreate = await createAudio(params, token?.value)
// if(responseCreate && responseCreate.status === 200){
// mensaje = "Audio subido correctamente";
// }
// } catch (error) {
// if (error instanceof TypeError) {
// console.error("Error en la petición");
// }

}
// }



}
// }


---

<div class="flex flex-col gap-3">
<h2 class="text-xl font-medium">Agrega un nuevo audio</h2>
<form class="flex flex-col items-center gap-3 pt-4" novalidate method="POST" onsubmit="">
<form class="flex flex-col items-center gap-3 pt-4" >
<div class="flex flex-row items-center w-full gap-3">

<section class="flex flex-col">
Expand Down Expand Up @@ -136,7 +137,7 @@ try{
<audio id="audio" controls class="w-full"></audio>
</div>
<input type="submit" class=" bg-bluePB px-4 py-1 rounded-sm" value="Subir"
onclick="alert('El audio se está subiendo al servidor, por favor espere un momento');"/>
onclick=""/>
<div>
{mensaje &&
<div class="bg-bluePB text-white font-semibold p-2 rounded-md bg-opacity-70 border-2 border-bluePB">
Expand All @@ -160,3 +161,97 @@ try{

</script>

<script>

import { createAudio } from "@/utils/createAudio";
import { uploadImg } from "@/utils/uploadImg";

function getCookie(name: any) {
let cookieArr = document.cookie.split("; ");

for(let i = 0; i < cookieArr.length; i++) {
let cookiePair = cookieArr[i].split("=");

if(name == cookiePair[0]) {
return decodeURIComponent(cookiePair[1]);
}
}
}
document.addEventListener("submit", async (event) => {
event.preventDefault()
let mensaje = ""
console.log("post")
try{
alert('El audio se está subiendo al servidor, por favor espere un momento');
interface Params {
titulo?: string;
esPrivada?: boolean;
img?: string;
esAlbum?: boolean;
esPodcast?: boolean;
fechaLanz?: string;
duracionSeg?: number;
cancion?: any;
idsUsuarios?: string;
tipoEtiqueta?: string;
etiquetas?: string;
}
const data = new FormData(document.querySelector("form") as HTMLFormElement);
const token = getCookie("token");
const file = data.get("photo") as File
let params : Params ={}
if(file.size !== 0){
const img = await uploadImg(file, token)
params["img"] = img.id
}
let can = data.get("cancion") as File
if(can.name != ""){
params['cancion']= can
params["duracionSeg"] = parseFloat(data.get("duracion") as string) || 0
}
params["titulo"] = data.get("titulo") as string
const esPrivada = data.get("esPrivada")
if(esPrivada === null){
params["esPrivada"] = false
}else{
params["esPrivada"] = true
}

let features = Astro.cookies.get('artistList')?.value;
if(features=== undefined || features.length === 2){
features = "";
}else{
params["idsUsuarios"] = features.slice(1, -1);
}


const tags = data.getAll("tags")
let esPodcast = false;
let typeEtiqueta = "Cancion";
typeEtiqueta = data.get("tabsaudio") as string;

if(typeEtiqueta == "Podcast"){
esPodcast = true;
}
params["esPodcast"] = esPodcast
if(tags.length !== 0){


params["tipoEtiqueta"] = typeEtiqueta
params["etiquetas"] = tags.join(",")
}
params["fechaLanz"] = new Date().toISOString()
params["esAlbum"] = false
console.log(params)
const responseCreate = await createAudio(params, token)
if(responseCreate && responseCreate.status === 200){
mensaje = "Audio subido correctamente";
}
} catch (error) {
if (error instanceof TypeError) {
console.error("Error en la petición");
}

}
})
</script>