Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
REST and APIs
  • Loading branch information
nlz18001 committed Apr 13, 2024
1 parent 478d27f commit b62151b
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions week-12/index.html
@@ -0,0 +1,153 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>REST and APIs</title>
</head>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
}

header {
background-color: #1a4f85;
color: #fff;
padding: 20px;
text-align: center;
}

h1 {
margin-top: 0;
margin-bottom: 5px;
}

#searchInput {
padding: 8px;
margin-right: 8px;
}

#searchButton {
padding: 8px 16px;
background-color: #b1d4ff;
color: #5e5e5e;
border: none;
cursor: pointer;
}

#searchButton:hover {
background-color: #ffffff;
}

main {
margin: 20px;
}

#resultsCount {
margin-top: 5px;
margin-left: 20px;
font-size: 12px;
color: rgb(149, 57, 75);
}

p {
margin-top: 5px;
margin-left: 20px;
font-size: 12px;
color: rgb(149, 57, 75);
}

ul {
list-style: none;
padding: 0;
}

li {
margin-bottom: 10px;
margin-left: 20px;
}

a {
text-decoration: none;
color: #1a4f85;
}

a:hover {
text-decoration: underline;
}

img {
width: 50px;
height: 50px;
border-radius: 50%;
margin-right: 10px;
}

</style>
<body>
<header>
<h1>User Search</h1>
<input type="text" id="searchInput" placeholder="Enter name of user">
<button id="searchButton">Search</button>
</header>
<div id="resultsCount"></div>
<div id="searchResults"></div>

<script>
document.addEventListener('DOMContentLoaded', function () {
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const searchResults = document.getElementById('searchResults');
const resultsCountDisplay = document.getElementById('resultsCount');

searchButton.addEventListener('click', function () {
const searchTerm = searchInput.value.trim();
if (searchTerm.length === 0) {
alert('Please enter a name.');
return;
}

fetch(`https://github.uconn.edu/api/v3/search/users?q=${searchTerm}`)
.then(response => response.json())
.then(data => displayResults(data))
.catch(error => console.error('Error fetching data:', error));
});

function displayResults(data) {
searchResults.innerHTML = '';
const users = data.items;
const resultsCount = data.total_count;

if (resultsCount === 0) {
searchResults.innerHTML = '<p>No results found.</p>';
resultsCountDisplay.textContent = '';
return;
}

const resultList = document.createElement('ul');

users.forEach(user => {
const listItem = document.createElement('li');
const link = document.createElement('a');
const avatar = document.createElement('img');

link.href = user.html_url;
link.textContent = user.login;
avatar.src = user.avatar_url;
avatar.alt = `${user.login}'s avatar`;

link.appendChild(avatar);
listItem.appendChild(link);
resultList.appendChild(listItem);
});

searchResults.appendChild(resultList);
resultsCountDisplay.textContent = `${resultsCount} results found.`;
}
});
</script>
</body>
</html>

0 comments on commit b62151b

Please sign in to comment.