-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
73 lines (67 loc) · 1.79 KB
/
App.js
File metadata and controls
73 lines (67 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as React from "react";
import { Repos } from "./components/Repos";
import "./styles.css";
function App() {
const [key, setKey] = React.useState("");
const [repos, setRepos] = React.useState([]);
const [errorMsg, setErrorMsg] = React.useState("");
const changeHandler = (e) => {
setKey(e.target.value);
setRepos([]);
setErrorMsg("");
};
const getRepos = () => {
console.log(key);
if (key) {
fetch("https://api.github.com/user/repos", {
headers: {
Authorization: `token ${key}`,
Accept: "application/vnd.github.v3+json",
},
})
.then((res) => {
if (!res.ok) {
setRepos([]);
setErrorMsg("Enter a valid API key.");
throw new Error("API response was not ok!!!");
}
return res.json();
})
.then((data) => {
setErrorMsg("");
setRepos(data);
})
.catch((error) => {
console.error("Error:", error);
setErrorMsg("Enter a valid API key.");
setRepos([]);
});
}
};
return (
<div>
<div className="app-repo-form">
<div>
<label>Enter GH API key: </label>
<input
type="text"
value={key}
onChange={changeHandler}
placeholder="Enter API key and press Get Repos"
/>
</div>
<button onClick={getRepos}>Get Repos</button>
</div>
{errorMsg && <div style={{ color: "red" }}>{errorMsg}</div>}
{repos.length > 0 ? (
<>
<h3>{`Repos for ${key} (${repos.length})`}</h3>
<div className="app-repo-repos">
<Repos repos={repos} authKey={key} />
</div>
</>
) : null}
</div>
);
}
export default App;