From 38f1fd72581ee987f1c994addd3378e781f0986c Mon Sep 17 00:00:00 2001 From: fede Date: Tue, 14 Oct 2025 18:51:59 -0300 Subject: [PATCH] =?UTF-8?q?lleve=20los=20fetch=20a=20los=20servicios=20y?= =?UTF-8?q?=20cre=C3=A9=20los=20tipos=20de=20datos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 54 ++++++++++++++++++--------------- src/Components/Alumno.tsx | 10 ++++-- src/services/servicioUsuario.ts | 7 +++++ src/types/usuario.ts | 6 ++++ 4 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 src/services/servicioUsuario.ts create mode 100644 src/types/usuario.ts diff --git a/src/App.tsx b/src/App.tsx index f4cc81d..8ecdded 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,31 +1,37 @@ import { useEffect, useState } from 'react'; -import './App.css' -import Alumno from './Components/Alumno' +import './App.css'; +import Alumno from './Components/Alumno'; +import { ObtenerUsuarios } from './services/servicioUsuario'; +function App() { + const [loading, setLoading] = useState(true); + const [alumnos, setAlumnos] = useState([]); - function App() { - const [loading, setLoading] = useState(true); - const [alumnos, setAlumnos] = useState([]); + useEffect(() => { + async function cargarDatos() { + setLoading(true); + try { + const data = await ObtenerUsuarios(); + setAlumnos(data); + } catch (error) { + console.error('Error al obtener usuarios:', error); + } finally { + setLoading(false); + } + } - useEffect(() => { - fetch("https://jsonplaceholder.typicode.com/users") - .then(res => res.json()) - .then(data => { - setAlumnos(data); - setLoading(false); - }) - .catch(() => setLoading(false)); - }, []); + cargarDatos(); + }, []); - if (loading) return
Cargando...
; + if (loading) return
Cargando...
; - return ( -
- {alumnos.map((x) => ( - - ))} -
- ); - } + return ( +
+ {alumnos.map((x) => ( + + ))} +
+ ); +} -export default App +export default App; diff --git a/src/Components/Alumno.tsx b/src/Components/Alumno.tsx index d42b236..85e8cd8 100644 --- a/src/Components/Alumno.tsx +++ b/src/Components/Alumno.tsx @@ -1,7 +1,8 @@ import { useState } from "react"; import "./Alumno.css" +import type { Usuario } from "../types/usuario"; -export default function Alumno({data}: {data: any}){ +export default function Alumno({data}: {data: Usuario}){ const [alumno, setAlumno] = useState({ presente: false }); return (
Nombre: {data.name}

-

+ +

Usuario: {data.username}

+ +

+ Usuario: {data.email} +


diff --git a/src/services/servicioUsuario.ts b/src/services/servicioUsuario.ts new file mode 100644 index 0000000..d1c8455 --- /dev/null +++ b/src/services/servicioUsuario.ts @@ -0,0 +1,7 @@ +import type { Usuario } from "../types/usuario"; + +export const ObtenerUsuarios = async (): Promise => { + const usuario = await fetch(`https://jsonplaceholder.typicode.com/users`); + const usuJson: Usuario[] = await usuario.json(); + return usuJson; +}; diff --git a/src/types/usuario.ts b/src/types/usuario.ts new file mode 100644 index 0000000..5fa7335 --- /dev/null +++ b/src/types/usuario.ts @@ -0,0 +1,6 @@ +export interface Usuario { + id: number, + name: string, + username: string, + email: string +}