From 7da263732c8f61aad8ad5d5752c5648186fcbd1a Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 27 Mar 2026 11:19:11 +0100 Subject: [PATCH] =?UTF-8?q?add:=20preparar=20archivos=20para=20hacer=20la?= =?UTF-8?q?=20petici=C3=B3n=20al=20nuevo=20endpoint=20"build"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/lib/{axios.js => api.js} | 4 +-- frontend/src/pages/DocEditor.jsx | 50 ++++++++++++++++++--------- frontend/src/services/docService.js | 22 +++++++++--- 3 files changed, 52 insertions(+), 24 deletions(-) rename frontend/src/lib/{axios.js => api.js} (80%) diff --git a/frontend/src/lib/axios.js b/frontend/src/lib/api.js similarity index 80% rename from frontend/src/lib/axios.js rename to frontend/src/lib/api.js index 5aa26f5..dae5d65 100644 --- a/frontend/src/lib/axios.js +++ b/frontend/src/lib/api.js @@ -1,7 +1,7 @@ import Axios from 'axios'; import { API_BASE_URL } from '../config'; -const axios = Axios.create({ +const api = Axios.create({ baseURL: API_BASE_URL, headers: { 'Accept': 'application/json', @@ -10,4 +10,4 @@ const axios = Axios.create({ }); -export default axios; \ No newline at end of file +export default api; \ No newline at end of file diff --git a/frontend/src/pages/DocEditor.jsx b/frontend/src/pages/DocEditor.jsx index 88fb4c0..b114498 100644 --- a/frontend/src/pages/DocEditor.jsx +++ b/frontend/src/pages/DocEditor.jsx @@ -2,7 +2,7 @@ import React, { useState } from 'react'; import Step1BaseScale from '../components/editor/Step1BaseScale'; import Step2FuzzyModeling from '../components/editor/Step2FuzzyModeling'; import SubscaleModal from '../components/editor/SubscaleModal'; -import { calculateValueFunction } from '../services/docService'; +import { calculateValueFunction, buildFuzzyGraph } from '../services/docService'; export default function DocEditor() { const [step, setStep] = useState(1); @@ -18,11 +18,12 @@ export default function DocEditor() { const [baseScale, setBaseScale] = useState({}); const [selectedTerm, setSelectedTerm] = useState(null); const [mfDefinitions, setMfDefinitions] = useState({}); - - // ESTADOS: SUBESCALAS (FASE 2.5) const [subscales, setSubscales] = useState({}); const [modalTarget, setModalTarget] = useState(null); + // ESTADO: FASE 3 + const [finalResult, setFinalResult] = useState(null); + // MANEJADORES: FASE 1 const handleCriterionChange = (val) => { setCriterionName(val); if (errors.criterion) setErrors({ ...errors, criterion: false }); }; const handleLevelChange = (index, newValue) => { const newLevels = [...levels]; newLevels[index] = newValue; setLevels(newLevels); if (errors.levels[index]) setErrors({ ...errors, levels: errors.levels.map((e, i) => i === index ? false : e) }); }; @@ -100,7 +101,6 @@ export default function DocEditor() { }); }; - // MANEJADORES: SUBESCALAS const handleOpenSubscale = (term, side, initialData) => { setModalTarget({ term, side, initialData }); }; @@ -116,40 +116,45 @@ export default function DocEditor() { setModalTarget(null); }; + // Petición para el endpoint "build" const handleFinalSubmit = async () => { const scaleKeys = Object.keys(baseScale); - const payload = { criterion_name: criterionName.trim(), levels: scaleKeys.map(term => { const mf = mfDefinitions[term]; const sub = subscales[term] || {}; - return { term: term, - core: [ - Number(mf.coreStart.toFixed(4)), - Number(mf.coreEnd.toFixed(4)) - ], - support: [ - Number(mf.supportStart.toFixed(4)), - Number(mf.supportEnd.toFixed(4)) - ], + core: [ Number(mf.coreStart.toFixed(4)), Number(mf.coreEnd.toFixed(4)) ], + support: [ Number(mf.supportStart.toFixed(4)), Number(mf.supportEnd.toFixed(4)) ], left_blank_cards: sub.left ? sub.left.blankCards : [0], right_blank_cards: sub.right ? sub.right.blankCards : [0], - left_nodes_count: sub.left ? sub.left.cardsCount : 2, right_nodes_count: sub.right ? sub.right.cardsCount : 2 }; }) }; - console.log("PAYLOAD:", JSON.stringify(payload, null, 2)); - // TODO: Llamada a la API + setIsLoading(true); + try { + const result = await buildFuzzyGraph(payload); + console.log("RESPUESTA DEL BACKEND:", result); + + setFinalResult(result); + setStep(3); + + } catch (error) { + console.error(error); + alert("Error del servidor: \n" + JSON.stringify(error, null, 2)); + } finally { + setIsLoading(false); + } }; return (
+ {step === 1 && ( )} + {step === 2 && ( )} + {step === 3 && ( +
+

Paso 3: Espectro Difuso Final

+

Gráfica construida correctamente. ¡Mira la consola!

+ +
+ )} + {modalTarget && ( { try { - const response = await axios.post('/criteria/doc/value-function', payload); - return response.data; + const response = await api.post('/criteria/doc/value-function', payload); + return response.data; } catch (error) { - console.error("Error en calculateValueFunction:", error); - throw error; + console.error('Error calculating value function:', error); + throw error.response?.data?.detail || error.message; } +}; + +export const buildFuzzyGraph = async (payload) => { + try { + const response = await api.post('/criteria/doc-mf/build', payload); + return response.data; + } catch (error) { + console.error('Error building fuzzy graph:', error); + throw error.response?.data?.detail || error.message; + } + + }; \ No newline at end of file