diff --git a/src/components/Services/DtsList.tsx b/src/components/Services/DtsList.tsx index e55445be..6636581e 100644 --- a/src/components/Services/DtsList.tsx +++ b/src/components/Services/DtsList.tsx @@ -143,7 +143,7 @@ function DtsList() { function ButtonAddService({ className }: { className: string; }): JSX.Element { const handleButtonClick = () => { - console.log("Llamando al endpoint..."); + window.location.href = `/services/new`; }; return ( diff --git a/src/components/Services/DtsViewEdit.tsx b/src/components/Services/DtsViewEdit.tsx index 872facf2..208f8e4d 100644 --- a/src/components/Services/DtsViewEdit.tsx +++ b/src/components/Services/DtsViewEdit.tsx @@ -2,11 +2,11 @@ import React, { ChangeEvent, useEffect, useState } from 'react'; import { DtsListPostRequest, DtsResourceApi } from '../../openapi-client/apis/DtsResourceApi'; -import { DtsFilterFromJSON, DtsTemplateVO, DtsVO, EntityState } from '../../openapi-client/models'; +import { DtsFilterFromJSON, DtsTemplateVO, DtsVO, EntityState, DtsType } from '../../openapi-client/models'; import { DtsFilter } from '../../openapi-client/models'; import { Configuration, ConfigurationParameters, DtsTemplateResourceApi } from '../../openapi-client'; import { useAuth } from "react-oidc-context"; -import { usePathname } from 'next/navigation'; +import { usePathname, useRouter } from 'next/navigation'; import SelectDtsTemplate from '../Templates/SelectDtsTemplate'; import {v4 as uuidv4} from 'uuid'; import Ajv from 'ajv'; @@ -31,16 +31,21 @@ type SchemaConfig = { function DtsViewEdit() { - const [dtsVO, setDtsVO] = useState(); + const [dtsVO, setDtsVO] = useState({ + name: "", + id: "", + templateFk: "", + debug: false, + }); const auth = useAuth(); const pathname = usePathname() - let dtsName; - let debugInitialValue; + const router = useRouter(); const [dtsTemplateVOs, setDtsTemplateVOs] = useState([]); const [isOptionSelected, setIsOptionSelected] = useState(false); const [templateNames, setTemplateNames] = useState([]); const [selectedOption, setSelectedOption] = useState(''); + const [needsRefresh, setNeedsRefresh] = useState(false); const handleChange = async (e: ChangeEvent) => { setSelectedOption(e.target.value); @@ -109,55 +114,32 @@ function DtsViewEdit() { } } - let idinurl = pathname.replace("/services/", ""); - console.log(idinurl); - function listDtsTemplateVOs() { const configParameters: ConfigurationParameters = { headers: { 'Authorization': 'Bearer ' + auth.user?.access_token , - }, basePath: process.env.BACKEND_BASE_PATH, }; - - const config = new Configuration(configParameters); const apiDtst = new DtsTemplateResourceApi(config); - - apiDtst.dtstListPost({}).then((resp) => setDtsTemplateVOs(resp)); + apiDtst.dtstListPost({}).then((resp) => setDtsTemplateVOs(prevState => [...prevState, ...resp])); } - - - - useEffect(() => { - console.log("going here " + auth.isAuthenticated); - if (auth.isAuthenticated) { - - listDtsTemplateVOs(); - listTemplateNames(); - } - - -}, [auth]); - - function getDtsVO() { - if ((idinurl === null) || (idinurl === "new")) { - - setDtsVO({...dtsVO, name: "New Decentralized Trusted Service", id: uuidv4(), templateFk:"newTemplateFk", debug: false}) - + const id = uuidv4(); + setDtsVO({...dtsVO, name: "New Decentralized Trusted Service", id: uuidv4(), templateFk: id, debug: false}) + const newTemplate:DtsTemplateVO = {title: 'string', state: EntityState.Editing, yaml: 'string', name: "string", id: id, type: DtsType.ConversationalService} + setDtsTemplateVOs([newTemplate]); } else { const configParameters: ConfigurationParameters = { headers: { 'Authorization': 'Bearer ' + auth.user?.access_token , - }, basePath: process.env.BACKEND_BASE_PATH, }; @@ -166,7 +148,7 @@ function DtsViewEdit() { const config = new Configuration(configParameters); const api = new DtsResourceApi(config); - api.dtsGetIdGet({ id: idinurl}).then((resp: React.SetStateAction) => { + api.dtsGetIdGet({ id: idinurl}).then((resp) => { if (resp) { setDtsVO(resp); setIsOptionSelected(true); @@ -175,42 +157,32 @@ function DtsViewEdit() { } }); - - - dtsVO?.deploymentConfig?.orchestratorUrl; - - console.log("orchestratorUrl:" + dtsVO?.deploymentConfig?.orchestratorUrl); } - } + useEffect(() => { + if (auth.isAuthenticated) { + getDtsVO(); + listDtsTemplateVOs(); + listTemplateNames(); + } +}, [auth, needsRefresh]); + function getDeploymentConfigKeys(): string[] { let keylist: string[] = []; for (const key in dtsVO?.deploymentConfig) { - - keylist.push(key); - - - + keylist.push(key); } return keylist; } - function handleDeploymentConfigValueChange(key: string, e: React.ChangeEvent) { - - let deploymentConf = dtsVO?.deploymentConfig; - deploymentConf?[key] : e; - setDtsVO({...dtsVO, deploymentConfig: deploymentConf}) - } - - function saveDtsVO() { + async function saveDtsVO() { const configParameters: ConfigurationParameters = { headers: { 'Authorization': 'Bearer ' + auth.user?.access_token , - }, basePath: process.env.BACKEND_BASE_PATH, }; @@ -219,41 +191,32 @@ function DtsViewEdit() { const config = new Configuration(configParameters); const api = new DtsResourceApi(config); - - - api.dtsSavePost({ dtsVO: dtsVO }); + try { + await saveDtsTemplateVO() + await api.dtsSavePost({ dtsVO: dtsVO }); + } catch (error) { + } + const id = dtsVO?.id ?? "new"; + if (pathname.includes("new")) router.push(pathname.replace("new",id)) + setNeedsRefresh(true); } - function saveDtsTemplateVO() { + async function saveDtsTemplateVO() { if(selectedOption !== 'current'){ const templateVO = dtsTemplateVOs.find(t => t.id === dtsVO?.templateFk); const configParameters: ConfigurationParameters = { headers: { 'Authorization': 'Bearer ' + auth.user?.access_token , - }, basePath: process.env.BACKEND_BASE_PATH, }; - const config = new Configuration(configParameters); const api = new DtsTemplateResourceApi(config); - templateVO && api.dtstSavePost({ dtsTemplateVO: templateVO }); + templateVO && await api.dtstSavePost({ dtsTemplateVO: templateVO }); } } - - - useEffect(() => { - console.log("going here " + auth.isAuthenticated); - if (auth.isAuthenticated) { - - getDtsVO(); - } - - - }, [auth, dtsTemplateVOs]); - if (auth.isAuthenticated) { @@ -318,9 +281,6 @@ function DtsViewEdit() { -