-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (54 loc) · 1.75 KB
/
app.js
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
console.log("Let's get this party started!");
const API_KEY = "MhAodEJIJxQMxW9XqxKjyXfNYdLoOIym"
const API_ENDPOINT = "https://api.giphy.com/v1/gifs/search"
const RESULT_LIMIT = 20
async function callGiphyApi(searchTerm){
let res = await axios.get(API_ENDPOINT, {
params: {
api_key: API_KEY,
limit:RESULT_LIMIT,
q:searchTerm
}
})
return res.data.data;
}
function appendGifToDom(gifArray){
gif = gifArray[returnRandomInt(gifArray.length-1)];
// for(let gif of gifArray){
$('.images').append(
`
<div class="col-md-3">
<div class="thumbnail">
<div style="width:100%;height:0;padding-bottom:56%;position:relative;" >
<iframe src="${gif.embed_url}" width="100%"
height="100%"
style="position:absolute"
frameBorder="0"
class="giphy-embed "
>
</iframe>
</div>
<div class="caption">
<p class="text-center text-capitalize">${gif.title}</p>
</div>
</a>
</div>
</div>
`)
// }
}
function returnRandomInt(maxValue) {
var x = Math.floor((Math.random() * maxValue) + 1);
return x;
}
$("#delete_button" ).click(function() {
$( ".images" ).empty();
});
$( "form" ).submit(function( event ) {
event.preventDefault();
const searchTerm = $("input").val()
//const api_data = callGiphyApi(searchTerm);
callGiphyApi(searchTerm).then(function(result){ appendGifToDom(result)})
//Reset Form Values
event.target.reset()
});