From 69a81c27ff64f88300b5354ac7ee5d190a8f62da Mon Sep 17 00:00:00 2001 From: No0ne003 Date: Thu, 16 May 2024 10:15:02 +0100 Subject: [PATCH] improve github profile finder ui and adding aborcontorller to fetching --- .../GithubProfileFinder.jsx | 37 ++++++++--- src/pages/GithubProfileFinder/User.jsx | 63 +++++++++++-------- 2 files changed, 63 insertions(+), 37 deletions(-) diff --git a/src/pages/GithubProfileFinder/GithubProfileFinder.jsx b/src/pages/GithubProfileFinder/GithubProfileFinder.jsx index 061d828..1d31e9f 100644 --- a/src/pages/GithubProfileFinder/GithubProfileFinder.jsx +++ b/src/pages/GithubProfileFinder/GithubProfileFinder.jsx @@ -1,17 +1,19 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { useEffect, useState } from "react"; -import { User } from "./User"; import Loading from "@/components/ui/Loading"; +import User from "./User"; const GithubProfileFinder = () => { const [userName, setUserName] = useState("No0ne003"); const [userData, setUserData] = useState(null); const [loading, setLoading] = useState(false); - async function fetchGithubUserData() { + async function fetchGithubUserData(signal) { setLoading(true); - const res = await fetch(`https://api.github.com/users/${userName}`); + const res = await fetch(`https://api.github.com/users/${userName}`, { + signal, + }); const data = await res.json(); @@ -20,7 +22,7 @@ const GithubProfileFinder = () => { setLoading(false); } - console.log(data) + console.log(data); } function handleSumbit() { @@ -28,28 +30,43 @@ const GithubProfileFinder = () => { } useEffect(() => { - fetchGithubUserData(); + const controller = new AbortController(); + const signal = controller.signal; + fetchGithubUserData(signal); + + return () => { + controller.abort(); + }; }, []); return ( -
+
setUserName(event.target.value)} /> -
- {loading ? : userData !== null ? ( - + {loading ? ( + + ) : userData !== null ? ( +
+ +
) : null}
); }; -export default GithubProfileFinder +export default GithubProfileFinder; diff --git a/src/pages/GithubProfileFinder/User.jsx b/src/pages/GithubProfileFinder/User.jsx index 724715f..0fd8e61 100644 --- a/src/pages/GithubProfileFinder/User.jsx +++ b/src/pages/GithubProfileFinder/User.jsx @@ -1,4 +1,4 @@ -export const User = ({ user }) => { +const User = ({ user }) => { const { avatar_url, followers, @@ -15,7 +15,7 @@ export const User = ({ user }) => { if (message) { return (

- + User {" "} Not Found @@ -24,39 +24,48 @@ export const User = ({ user }) => { } return ( -
-
- User -
-
- - {name || login} - -

- User joined on{" "} - {`${createDate.getDate()} ${createDate.toLocaleString("en-us", { - month: "short", - })} ${createDate.getFullYear()}`} -

+
+
+
+ {`${name +
+
+ + {name || login} + +

+ User joined on{" "} + {`${createDate.getDate()} ${createDate.toLocaleString("en-us", { + month: "short", + })} ${createDate.getFullYear()}`} +

+
-
-
-

Public Repos

+
+

{public_repos}

+

Repos

-
-

Followers

+

{followers}

+

Followers

-
-

Following

+

{following}

+

Following

); }; + +export default User;