From 45d0585c5d5945c041f04337c92cc49a93e0a081 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 23 Mar 2026 14:04:55 +0100 Subject: [PATCH] add: implementada peticion post hacia el endpoint del backend --- frontend/src/App.jsx | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index e1c8458..e34c01b 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,10 +1,55 @@ import { useState } from 'react'; function App() { + const [criterionName, setCriterionName] = useState(''); const [levels, setLevels] = useState(['', '', '']); const [blankCards, setBlankCards] = useState([0, 0]); + const [isLoading, setIsLoading] = useState(false); + const [result, setResult] = useState(null); + + const handleCalculate = async () => { + setIsLoading(true); + setResult(null); + + const currentReferences = [levels[0], levels[levels.length - 1]]; + + const payload = { + name: criterionName || "Criterio sin nombre", + labels: levels, + blank_cards: blankCards, + references: currentReferences + }; + + console.log("Enviando JSON al backend:", payload); + + try { + const response = await fetch('http://localhost:8000/api/criteria/doc/value-function', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + if (!response.ok) { + throw new Error(`Error del servidor: ${response.status}`); + } + + const data = await response.json(); + console.log("Respuesta del backend:", data); + setResult(data); + + } catch (error) { + console.error("Error al hacer la petición:", error); + alert("No se ha podido conectar con el backend."); + } finally { + setIsLoading(false); + } + }; + + const handleLevelChange = (index, newValue) => { const newLevels = [...levels]; newLevels[index] = newValue; @@ -138,6 +183,30 @@ function App() { + + {/* --- NUEVO: BOTÓN DE CALCULAR --- */} +
+ +
+ + {/* --- NUEVO: CAJA PARA VER EL RESULTADO (TEMPORAL PARA DEBUG) --- */} + {result && ( +
+

Respuesta del Backend:

+
+              {JSON.stringify(result, null, 2)}
+            
+
+ )} + ); }