-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautocomplete-script.js
More file actions
153 lines (140 loc) · 4.83 KB
/
autocomplete-script.js
File metadata and controls
153 lines (140 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//dirty way to make original suggestions. might want to drop entirely in favor of waiting on api results
let fullCatalog = imdb250Movies.items.concat(imdb250TV.items);
//global for tracking keypress
let timeoutID;
//creates the new autocomplete form
const autoCompleteJS = new autoComplete({
selector: "#search",
wrapper: false,
placeHolder: "Find a Movie",
submit: true,
searchEngine: "loose",
data: {
src: fullCatalog,
cache: true,
keys: ['title']
},
//styling for list elements
resultItem: {
highlight: true,
element: (item, data) =>{
let year = data.value.year ? data.value.year : data.value.Year;
item.classList = 'flex flex-row justify-between hover:bg-indigo-200 rounded px-1 items-baseline'
item.innerHTML =`
<p>${data.match}</p><p class='text-right text-sm text-gray-400'>${year}</p>`
// console.log(data.value.year)
}
},
//interactions and events here
events: {
input: {
selection: (event) => {
const selection = event.detail.selection.value;
autoCompleteJS.input.value = selection.title ? selection.title : selection.Title
//submits form when a selection is made.
document.querySelector('#auto-complete-loading-icon').classList.add('opacity-0')
document.querySelector("#form").requestSubmit()
},
focus: () => {
if (autoCompleteJS.input.value.length) {
document.querySelector('#auto-complete-loading-icon').classList.remove('opacity-0');
autoCompleteJS.start()
};
},
keyup: () => {
//shows and hides the loading icon
searchNoResultsToolTip.hide()
if (autoCompleteJS.input.value.length) {
document.querySelector('#auto-complete-loading-icon').classList.remove('opacity-0');
} else {
document.querySelector('#auto-complete-loading-icon').classList.add('opacity-0');
}
//I don't think this if statement is needed (the executed code is the same regardless of the condition.)
if (!timeoutID) {
timeoutID = setTimeout(() => {
console.log(`times up`);
}, 3000);
} else {
clearTimeout(timeoutID);
timeoutID = setTimeout(() => {
if (checkInputSize(autoCompleteJS.input.value.trim())) {
goFindSomeResults(autoCompleteJS.input.value.trim());
}
}, 750);
}
},
blur: () => {
//when the search bar loses focus close the search list and hide the loading icon
document.querySelector('#auto-complete-loading-icon').classList.add('opacity-0');
autoCompleteJS.close();
}
}
},
resultsList: {
// add styling to the list of suggestions
class: 'bg-white text-lg text-gray-900 rounded-lg py-4 px-4 space-y-2 absolute z-10',
maxResults: 10,
tabSelect: true,
},
threshold: 3,
});
//checks that there are enough chars for a search.
const checkInputSize = () => {
if (autoCompleteJS.input.value.trim().length > 2) {
return true;
}
return false;
};
//api call that drives the results to the updateSuggestions function. i think async and await are not useful here.
const goFindSomeResults = async (searchText) => {
let url = `https://www.omdbapi.com/?s=${searchText}&apikey=2086f7b1`;
console.log(`CAUTION: API call to ${url} in progress`);
await fetch(url)
.then(response => response.json())
.then(data => {
updateSuggestions(data.Search);
});
};
//updates the suggestions list under the search bar if there are results
const updateSuggestions = (arr) => {
if (arr) {
autoCompleteJS.data = {
src: arr,
cache: true,
keys: ['Title']
};
} else {
console.log(`there's nothing we can add to the search suggestions`)
searchNoResultsToolTip.show()
}
autoCompleteJS.start();
};
let searchNoResultsToolTip = tippy(document.querySelector('#search'), {
content: `<h2 class='text-yellow-400 text-2xl border-b border-solid border-yellow-300 mb-2'><i class="fas fa-exclamation-triangle"></i> No Search Results</h2><p class='text-yellow-400 text-xl'>We can't find anything that matches your search 😞. Try again with a <span class='font-semibold'>new term.</span></p>`,
allowHTML: true,
// trigger for testing/styling
// trigger: 'click',
trigger: 'manual',
theme: 'dark-warning',
maxWidth: 400,
placement: 'top-start',
arrow: false,
animation: 'shift-away-extreme',
inertia: true,
})
const sampleDatadotSearch = [
{
"Title": "Red Rum",
"Year": "2000",
"imdbID": "tt0237643",
"Type": "movie",
"Poster": "N/A"
},
{
"Title": "The Red Rum Diaries",
"Year": "2016",
"imdbID": "tt7283336",
"Type": "series",
"Poster": "https://m.media-amazon.com/images/M/MV5BM2Q3OTM5YmUtZmI1OS00Y2I0LTg2NmQtNWFiOTY3OWRmYmYyXkEyXkFqcGdeQXVyOTM2ODgzNw@@._V1_SX300.jpg"
}
]