forked from bpd01001/2024-spring-dmd-3475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>GitHub User Search</title> | ||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"> | ||
<style> | ||
.container { | ||
max-width: 600px; | ||
} | ||
|
||
img { | ||
width: 150px; | ||
} | ||
|
||
#error { | ||
display: none; | ||
} | ||
.list-group-item img { | ||
max-width: 100px; | ||
max-height: 100px; | ||
border-radius: 10px; | ||
} | ||
.list-group-item { | ||
padding-top: 20px; | ||
padding-bottom: 20px; | ||
} | ||
a { | ||
text-decoration: none; | ||
} | ||
</style> | ||
|
||
</head> | ||
<body> | ||
<div class="container mt-3"> | ||
<div class="d-flex justify-content-center"> | ||
<img src="img/github.png" alt="github logo" class="p-3"> | ||
</div> | ||
<h2 class="text-center mb-4">User Search</h2> | ||
<div class="row d-flex justify-content-center"> | ||
<div class="mb-3 col-10"> | ||
<input type="text" id="searchInput" class="form-control" placeholder="Search For User"> | ||
</div> | ||
<div class="col-2"> | ||
<button id="searchBtn" class="btn btn-primary">Search</button> | ||
</div> | ||
</div> | ||
<div id="error" class="text-danger"></div> | ||
|
||
<div id="results" class="mt-4"></div> | ||
</div> | ||
|
||
<script> | ||
document.getElementById('searchBtn').addEventListener('click', () => { | ||
const searchInput = document.getElementById('searchInput').value.trim(); | ||
|
||
if (searchInput.length < 1) { | ||
document.getElementById('error').innerText = 'Please enter at least 1 character.'; | ||
document.getElementById('error').style.display = 'block'; | ||
return; | ||
} | ||
|
||
fetch(`https://github.uconn.edu/api/v3/search/users?q=${searchInput}`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
displayResults(data); | ||
}) | ||
.catch(error => { | ||
console.error('There was a problem with the fetch operation:', error); | ||
}); | ||
}); | ||
|
||
function displayResults(data) { | ||
const resultsContainer = document.getElementById('results'); | ||
resultsContainer.innerHTML = ''; | ||
|
||
if (data.items.length === 0) { | ||
document.getElementById('error').innerText = 'No results found.'; | ||
document.getElementById('error').style.display = 'block'; | ||
return; | ||
} | ||
|
||
const totalCount = data.total_count; | ||
const users = data.items; | ||
|
||
document.getElementById('error').style.display = 'none'; | ||
|
||
resultsContainer.innerHTML = ` | ||
<div class="alert alert-success" role="alert">${totalCount} results found.</div> | ||
<ul class="list-group"> | ||
${users.map(user => ` | ||
<li class="list-group-item"> | ||
<a href="${user.html_url}" target="_blank"> | ||
<img src="${user.avatar_url}" alt="${user.login}" class="me-2">${user.login} | ||
</li> | ||
`).join('')} | ||
</ul> | ||
`; | ||
} | ||
|
||
</script> | ||
</body> | ||
</html> |