diff --git a/src/App.tsx b/src/App.tsx index 9ae967b..aa57321 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,40 @@ +import { ChangeEvent, useState } from "react"; import Card from "./components/Card"; import { useFetchRepositories } from "./hooks/useRepos"; import { useFavoriteReposStore } from "./store/favoriteRepos"; function App() { - const { data, isLoading } = useFetchRepositories(); + const [userName, setUserName] = useState("sagoma-dev"); + const { data, isLoading, isError } = useFetchRepositories(userName); const { favoriteReposIds } = useFavoriteReposStore(); - if (isLoading) { - return

Loading...

; - } + + const handleUserChange = (e: ChangeEvent) => { + setUserName(e.target.value); + }; return (
- +

+ Repositorios de: + handleUserChange(e)} + value={userName} + /> +

+ {isLoading &&

Cargando Repos...

} + {isError &&

{userName} no existe

} + {data && ( + + )}
); } diff --git a/src/hooks/useRepos.ts b/src/hooks/useRepos.ts index aa4c993..22d634c 100644 --- a/src/hooks/useRepos.ts +++ b/src/hooks/useRepos.ts @@ -1,12 +1,13 @@ -import { useQuery } from "@tanstack/react-query"; +import { QueryFunctionContext, useQuery } from "@tanstack/react-query"; import api from "../api/github"; import { Repository } from "./types"; -async function fetchRepos() { - const { data } = await api.get("/users/sagoma-dev/repos"); +async function fetchRepos(ctx: QueryFunctionContext) { + const [, githubUser] = ctx.queryKey; + const { data } = await api.get(`/users/${githubUser}/repos`); return data; } -export function useFetchRepositories() { - return useQuery(["repos"], fetchRepos); +export function useFetchRepositories(user: string) { + return useQuery(["repos", user], fetchRepos); }