-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (73 loc) · 1.8 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
import { GITHUB_KEY } from "./apikey.js";
// dotenv.config();
// require("dotenv").config();
// window.process = {
// env: {
// NODE_ENV: "development",
// },
// };
const form = document.querySelector("#user-search");
const userContainer = document.querySelector("#user-cont");
const search = document.querySelector("#search");
const getUserData = (username) => `{
search(type: USER, query: "${username}", first: 1) {
edges {
node {
... on User {
name
avatarUrl
bio
login
status {
emoji
message
emojiHTML
}
repositories(first: 20) {
totalCount
nodes {
name
primaryLanguage {
color
name
}
stargazerCount
updatedAt
forkCount
}
}
}
}
}
}
}`;
const renderUser = ({ data }) => {
const repoInfo = data?.search?.edges?.[0]?.node;
localStorage.setItem("user", JSON.stringify(repoInfo));
userContainer.innerHTML = `<div class="user-container">
<p style="margin: 0">${repoInfo ? repoInfo.name : `User not found`}</p>
<a href="profile.html">${repoInfo ? `View Profile` : ``}</a>
</div>`;
};
const loadUserProfile = async (e) => {
e.preventDefault();
const username = form.elements["search"].value;
const options = {
method: `post`,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${GITHUB_KEY}`,
},
body: JSON.stringify({
query: getUserData(username),
}),
};
await fetch(`https://api.github.com/graphql`, options)
.then((res) => res.json())
.then(renderUser)
.catch((err) => {
console.log(err);
});
// form.reset();
};
form.addEventListener("submit", loadUserProfile);