Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gatarelib/d9cfc4dbca05c3d5e0e67836530ebdb0 to your computer and use it in GitHub Desktop.
Save gatarelib/d9cfc4dbca05c3d5e0e67836530ebdb0 to your computer and use it in GitHub Desktop.
Andela_Tech_Challenge - Cycle 11 (Kigali)
<div class="searchIT">
<input class="searchIT__input" type="search" placeholder="City or state" autofocus="autofocus"/>
<button class="searchIT__button" type="button">
<svg height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
<path d="M0 0h24v24H0z" fill="none"></path>
</svg>
</button>
</div>
<section class="results">
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</section>
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
const cities = [];
fetch(endpoint).
then(blob => blob.json()).
then(data => cities.push(...data)).
then(resp => displayMatches());
function findMatches(input) {
return cities.filter(place => {
const regex = new RegExp(input, 'gi');
return place.city.match(regex) || place.state.match(regex);
});
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function displayMatches() {
const input = searchInput.value;
const matchArray = findMatches(input);
const htmlElements = matchArray.map(place => {
// Add text highlights
const regex = new RegExp(input, 'gi');
const cityName = place.city.replace(regex,
`<span class="hl">${input}</span>`);
const stateName = place.state.replace(regex,
`<span class="hl">${input}</span>`);
// Handle growth display
const growth = place.growth_from_2000_to_2013;
const growthDirection = parseFloat(growth) > 0 ? 'up' : 'down';
return `
<article class="result">
<div class="result__rank">${place.rank}</div>
<h2 class="result__title">${cityName}, ${stateName}</h2>
<div class="result__meta">
${numberWithCommas(place.population)} people
<span class="growth growth--${growthDirection}">${growth}</span>
</div>
</article>`;
});
const headerText = input ?
`${htmlElements.length} matches for "${input}"` :
`${htmlElements.length} cities ranking`;
const html = `<h1 class="results__header">${headerText}</h1>` +
htmlElements.join('');
searchResults.innerHTML = html;
}
const searchInput = document.querySelector('.searchIT__input');
const searchResults = document.querySelector('.results');
searchInput.addEventListener('change', displayMatches);
searchInput.addEventListener('keyup', displayMatches);
@import url("https://fonts.googleapis.com/css?family=PT+Sans:400,700");
body {
color: #212121;
font: 16px/1.5 'PT Sans', sans-serif;
padding: 1rem;
}
h1, h2, h3 {
margin: 0;
line-height: inherit;
}
input:focus,
button:focus {
outline: 0;
}
input::-webkit-input-placeholder {
color: inherit;
opacity: 0.5;
}
input:-ms-input-placeholder {
color: inherit;
opacity: 0.5;
}
input::-ms-input-placeholder {
color: inherit;
opacity: 0.5;
}
input::placeholder {
color: inherit;
opacity: 0.5;
}
.hl {
background: #FFC107;
}
.growth--up {
color: #00796B;
}
.growth--up:before {
content: '\25b2';
}
.growth--down {
color: #D32F2F;
}
.growth--down:before {
content: '\25bc';
}
.searchIT {
border-radius: 25px;
color: #FFFFFF;
background-color: #8929a3;
padding: 2rem;
display: flex;
align-items: center;
}
.searchIT__input {
color: inherit;
font-size: 2.5rem;
font-family: inherit;
background: none;
border: 0;
width: 100%;
padding: 0.5rem 0;
}
.searchIT__button {
background: none;
border: 0;
cursor: pointer;
}
.searchIT__button svg {
fill: #FFFFFF;
}
.searchIT__button:hover {
opacity: 0.7;
}
.results__header {
color: #757575;
font-size: 1rem;
font-weight: normal;
text-align: center;
margin: 2rem 0;
}
@-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.result {
border-top: 1px solid #BDBDBD;
padding: 2rem;
-webkit-animation: fadeIn 0.3s ease-in;
animation: fadeIn 0.3s ease-in;
position: relative;
}
.result__title {
font-size: 1.5rem;
text-transform: capitalize;
}
.result__meta {
color: #757575;
}
.result__rank {
color: #757575;
font-size: 1.5rem;
float: right;
}
.result__rank:before {
content: '#';
}
@-webkit-keyframes sk-bouncedelay {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1);
transform: scale(1);
}
}
@keyframes sk-bouncedelay {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1);
transform: scale(1);
}
}
.spinner {
margin: 100px auto 0;
width: 70px;
text-align: center;
}
.spinner > div {
width: 18px;
height: 18px;
background-color: #333;
border-radius: 100%;
display: inline-block;
-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
animation: sk-bouncedelay 1.4s infinite ease-in-out both;
}
.spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment