-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
139 lines (134 loc) · 4.63 KB
/
index.html
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<script src="https://cdn.tailwindcss.com"></script>
<title>GitHub Location Search</title>
<style>
body {
font-family: Arial, sans-serif;
}
#search-btn {
cursor: pointer;
}
</style>
</head>
<body>
<div class="px-56 py-10">
<h1 class="text-lg font-bold leading-6 text-center">
Search GitHub Users
</h1>
<div>
<p class="text-center">
* Recommend searching by location, sorting by Repositories and
filtering for languages
</p>
</div>
<div>
<input
type="text"
id="location"
placeholder="Enter location"
class="block w-full p-2 min-w-0 flex-1 rounded-none border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
/>
</div>
<input
type="text"
id="language"
class="block w-full min-w-0 p-2 flex-1 rounded-none border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
placeholder="Enter programming language"
/>
<div class="flex">
<div>
<label>Min Followers</label>
<input
type="number"
id="min-followers"
value="0"
class="block w-50 min-w-0 flex-1 p-2 rounded-none border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
placeholder="Minimum followers"
/>
</div>
<div>
<label># Results</label>
<input
type="number"
value="10"
id="per-page"
class="block w-50 min-w-0 flex-1 p-2 rounded-none border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
placeholder="# Results"
/>
</div>
</div>
<select id="sort">
<option value="best-match">Best match</option>
<option value="followers">Followers</option>
<option value="repositories">Repositories</option>
</select>
<button
id="search-btn"
class="rounded-md bg-indigo-600 px-2.5 py-1.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Search
</button>
</div>
<div id="count"></div>
<ul id="results"></ul>
<script>
let perPage = '10';
const count = document.getElementById('count');
document
.getElementById('search-btn')
.addEventListener('click', function () {
const location = document.getElementById('location').value;
const language = document.getElementById('language').value;
const minFollowers = document.getElementById('min-followers').value;
perPage = document.getElementById('per-page').value;
const sort = document.getElementById('sort').value;
searchGitHubUsers(location, language, minFollowers, sort);
});
async function searchGitHubUsers(location, language, minFollowers, sort) {
const resultsElement = document.getElementById('results');
resultsElement.innerHTML = '';
const query = `location:${encodeURIComponent(location)}${
language ? `+language:${encodeURIComponent(language)}` : ''
}${minFollowers ? `+followers:>${minFollowers}` : ''}`;
const sortParam = sort === 'best-match' ? '' : `&sort=${sort}`;
try {
const response = await fetch(
`https://api.github.com/search/users?q=${query}${sortParam}&per_page=${perPage}`
);
if (!response.ok) {
throw new Error(`GitHub API Error: ${response.statusText}`);
}
const data = await response.json();
if (data.items.length === 0) {
resultsElement.innerHTML =
'<p class="bg-rose-500">No users found.</p>';
} else {
count.innerText = data.items.length + ' results found';
data.items.forEach((user) => {
const listItem = document.createElement('div');
const anchor = document.createElement('a');
anchor.href = user.html_url;
anchor.target = '_blank';
listItem.setAttribute(
'class',
'hover:bg-indigo-500 hover:text-white py-1 px-3 my-1 mx-1'
);
anchor.textContent = user.login;
listItem.appendChild(anchor);
resultsElement.appendChild(listItem);
});
}
} catch (error) {
resultsElement.innerHTML = `<li class="bg-rose-500">Error: ${error.message}</li>`;
}
}
</script>
</body>
</html>