Skip to content

Commit 18e7e73

Browse files
committed
day 7
1 parent df851d3 commit 18e7e73

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ Motivate yourself to code daily till 60 days, and see the magic!
2323
| [Day 4](./each%20day%20build%20day!/Day%204/) | [Array Methods](./each%20day%20build%20day!/Day%203/) | [demo](https://powerofflex.z22.web.core.windows.net/) | [Takeaways](./each%20day%20build%20day!/Day%204/README.md/) |
2424
| [Day 5](./each%20day%20build%20day!/Day%205/) | [Memory Game](./each%20day%20build%20day!/Day%205/) | [demo](https://powerofflex.z22.web.core.windows.net/) | [Takeaways](./each%20day%20build%20day!/Day%205/README.md/) |
2525
| [Day 6](./each%20day%20build%20day!/Day%206/) | [The Broken Piano](./each%20day%20build%20day!/Day%206/) | [demo](https://powerofflex.z22.web.core.windows.net/) | [Takeaways](./each%20day%20build%20day!/Day%206/README.md/) |
26+
| [Day 7](./each%20day%20build%20day!/Day%207/) | [The Broken Piano](./each%20day%20build%20day!/Day%207/) | [demo](https://powerofflex.z22.web.core.windows.net/) | [Takeaways](./each%20day%20build%20day!/Day%207/README.md/) |
2627

each day build day!/Day 6/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ Try it out here :
1010
- The piano layout
1111
- html5 audio
1212

13+
# Snap
14+
15+
![snapshot]('./../rsz_2020-04-11-191628_1360x768_scrot.png')
16+
17+
Loading

each day build day!/Day 7/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Instant Search AJAX
2+
3+
when google first introduced ajax for its search engine, it was an instant hhit and developers all over the world loved it. That was one important milestone in the history web development. This is my take on ajax like type ahead search. It hits an open source api to get all the cities at once, then we use javascript magic to provide great user-experience by suggesting all the matches way ahead so users don't have to type.
4+
5+
# Challenges
6+
- fetch api
7+
- filter method
8+
- regex
9+
- change and keyup events
10+
11+
# demo
12+
13+
![link to the gif]('')

each day build day!/Day 7/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Instant search 👀</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
10+
<form class="search-form">
11+
<input type="text" class="search" placeholder="City or State">
12+
<ul class="suggestions">
13+
<li>Filter for a city</li>
14+
<li>or a state</li>
15+
</ul>
16+
</form>
17+
<script defer src="main.js"></script>
18+
</body>
19+
</html>

each day build day!/Day 7/main.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
2+
3+
const suggestions = document.querySelector('.suggestions');
4+
const searchBox = document.querySelector('.search');
5+
6+
//load cities
7+
let cities =[];
8+
fetch(endpoint)
9+
.then(res=>res.json())
10+
.then(data => cities.push(...data));
11+
12+
13+
function findMatches(wordToMatch, cities) {
14+
return cities.filter(place => {
15+
// here we need to figure out if the city or state matches what was searched
16+
const regex = new RegExp(wordToMatch, 'gi');
17+
return place.city.match(regex) || place.state.match(regex)
18+
});
19+
}
20+
21+
22+
function numberWithCommas(x) {
23+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
24+
}
25+
26+
27+
//trigger search after events like keyup, change
28+
searchBox.addEventListener('change', displayMatches);
29+
searchBox.addEventListener('keyup', displayMatches);
30+
31+
function displayMatches(){
32+
33+
const matchArray = findMatches(this.value, cities);
34+
const html = matchArray.map(place => {
35+
const regex = new RegExp(this.value, 'gi');
36+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`);
37+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`);
38+
return `
39+
<li>
40+
<span class="name">${cityName}, ${stateName}</span>
41+
<span class="population">${numberWithCommas(place.population)}</span>
42+
</li>
43+
`;
44+
}).join('');
45+
suggestions.innerHTML = html;
46+
}

each day build day!/Day 7/style.css

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
html {
2+
box-sizing: border-box;
3+
background: #363636;
4+
font-family: 'helvetica neue';
5+
font-size: 20px;
6+
font-weight: 200;
7+
}
8+
9+
*, *:before, *:after {
10+
box-sizing: inherit;
11+
}
12+
13+
input {
14+
width: 100%;
15+
padding: 20px;
16+
}
17+
18+
.search-form {
19+
max-width: 400px;
20+
margin: 50px auto;
21+
}
22+
23+
input.search {
24+
margin: 0;
25+
text-align: center;
26+
outline: 0;
27+
border: 10px solid #F7F7F7;
28+
width: 120%;
29+
left: -10%;
30+
position: relative;
31+
top: 10px;
32+
z-index: 2;
33+
border-radius: 5px;
34+
font-size: 40px;
35+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
36+
}
37+
38+
.suggestions {
39+
margin: 0;
40+
padding: 0;
41+
position: relative;
42+
/*perspective: 20px;*/
43+
}
44+
45+
.suggestions li {
46+
background: white;
47+
list-style: none;
48+
border-bottom: 1px solid #D8D8D8;
49+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.14);
50+
margin: 0;
51+
padding: 20px;
52+
transition: background 0.2s;
53+
display: flex;
54+
justify-content: space-between;
55+
text-transform: capitalize;
56+
}
57+
58+
.suggestions li:nth-child(even) {
59+
transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001);
60+
background: linear-gradient(to bottom, #ffffff 0%,#EFEFEF 100%);
61+
}
62+
63+
.suggestions li:nth-child(odd) {
64+
transform: perspective(100px) rotateX(-3deg) translateY(3px);
65+
background: linear-gradient(to top, #ffffff 0%,#EFEFEF 100%);
66+
}
67+
68+
span.population {
69+
font-size: 15px;
70+
}
71+
72+
.hl {
73+
background: #50504f;
74+
}
75+

0 commit comments

Comments
 (0)