From 76b4d3694cf3b3a1c826af24ef1a72a0d66ee903 Mon Sep 17 00:00:00 2001 From: Tejas Shekar <68545229+TejasShekar@users.noreply.github.com> Date: Tue, 14 Jun 2022 15:45:01 +0530 Subject: [PATCH] feat: add full GitHub user search functionality --- .../github-user-search/GitHubUserSearch.jsx | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/plays/github-user-search/GitHubUserSearch.jsx b/src/plays/github-user-search/GitHubUserSearch.jsx index af0630ad9..00356f62a 100644 --- a/src/plays/github-user-search/GitHubUserSearch.jsx +++ b/src/plays/github-user-search/GitHubUserSearch.jsx @@ -13,12 +13,87 @@ function GitHubUserSearch(props) { const play = getPlayById(id); // Your Code Start below. + const [query, setQuery] = useState(""); + const [resData, setData] = useState(null); + const [error, setError] = useState(false); + const [resetData, setResetData] = useState({ + count: 0, + time: undefined, + }); + + const getResetTime = (time) => { + const newTime = new Date(time * 1000); + return newTime.toLocaleTimeString(); + }; + + const getUsers = async () => { + try { + setError(false); + const res = await axios.get( + `https://api.github.com/search/users?q=${query}&per_page=20` + ); + setData(res.data.items); + } catch (error) { + setError(true); + setData(null); + } + }; + + useEffect(() => { + (async function () { + const rate_limit = await axios.get("https://api.github.com/rate_limit"); + const reset_time = getResetTime(rate_limit.data.resources.search.reset); + const reset_count = rate_limit.data.resources.search.remaining; + if (reset_count > 1) setError(false); + setResetData((prev) => { + return {...prev, count: reset_count, time: reset_time}; + }); + })(); + }, [query, resData]); + return ( <>
{/* Your Code Starts Here */} +
+ setQuery(e.target.value)} + /> + + {!resetData.count < 1 && ( +

No. of searches remaining : {resetData.count}

+ )} + {error && ( +

+ You have exhausted your search limit. Please wait until{" "} + {resetData.time} +

+ )} + {!error && ( +
+ {resData?.map(({id, login, avatar_url, html_url}) => ( + + ))} +
+ )} +
{/* Your Code Ends Here */}