Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
2024-spring-dmd-3475/week-12/index.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
104 lines (91 sloc)
3.39 KB
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
<!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> |