From 72d1197b44aea743269ba20a3c5055a0cae98203 Mon Sep 17 00:00:00 2001 From: Anuj Kumar Singh Date: Mon, 20 May 2024 12:54:13 +0530 Subject: [PATCH] fixes search error #99 --- src/app/projects/page.tsx | 160 +++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 82 deletions(-) diff --git a/src/app/projects/page.tsx b/src/app/projects/page.tsx index a04ef64..77ba48b 100644 --- a/src/app/projects/page.tsx +++ b/src/app/projects/page.tsx @@ -1,95 +1,91 @@ -"use client" +"use client"; import * as React from "react"; -import axios from 'axios'; +import axios from "axios"; import Navbar from "@/components/Navbar/page"; interface Project { - name: string; - stargazers_count: number; - html_url: string; + name: string; + stargazers_count: number; + html_url: string; } const Project: React.FC = () => { - const [query, setQuery] = React.useState(''); - const [searchResults, setSearchResults] = React.useState([]); - const [loading, setLoading] = React.useState(false); - const [error, setError] = React.useState(''); + const [query, setQuery] = React.useState(""); + const [searchResults, setSearchResults] = React.useState([]); + const [loading, setLoading] = React.useState(false); + const [error, setError] = React.useState(""); - const handleSearch = async () => { - try { - setLoading(true); - setError(''); - const response = await axios.get('https://api.github.com/search/repositories', { - params: { - q: `${query}`, - sort: 'stars', - order: 'desc' - } - }); - setSearchResults(response.data.items); - } catch (error) { - setError('Failed to fetch data'); - } finally { - setLoading(false); - } - }; + const handleSearch = async () => { + try { + setLoading(true); + setError(""); + const response = await axios.get("https://api.github.com/search/repositories", { + params: { + q: `${query}`, + sort: "stars", + order: "desc", + }, + }); + setSearchResults(response.data.items); + } catch (error) { + setError("Failed to fetch data"); + } finally { + setLoading(false); + } + }; - return ( - <> -
-

Search Open Source Projects

-
- setQuery(e.target.value)} - /> - -
- {loading &&

Loading...

} - {error &&

{error}

} -
- {searchResults.map((project, index) => ( -
-

{project.name}

-

{`Stars: ${project.stargazers_count}`}

-

URL: {`${project.html_url}`}

-
- ))} -
+ return ( + <> +
+

Search Open Source Projects

+
+ { + if (e.key == "Enter") handleSearch(); + }} + onChange={(e) => setQuery(e.target.value)} + /> + +
+ {loading &&

Loading...

} + {error &&

{error}

} +
+ {searchResults.map((project, index) => ( +
+

{project.name}

+

{`Stars: ${project.stargazers_count}`}

+

+ + URL: {`${project.html_url}`} + +

- - - ); -} + ))} +
+
+ + ); +}; export default Project;