-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgiphyScript.js
91 lines (85 loc) · 2.38 KB
/
giphyScript.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
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
const gifBtn = document.querySelector('#gif-btn');
const gifModal = document.querySelector('#gif-modal');
gifModal.style.display = 'none';
// make modal appear / dissapear
gifModal.addEventListener('click', (e) => {
e.stopPropagation();
});
let gifModalState = false;
gifBtn.addEventListener('click', (e) => {
e.preventDefault();
// e.stopPropagation();
gifModalState = !gifModalState;
switch (gifModalState) {
// if state is true, add reveal class and remove hide class from css
// switch state from true or false
case true:
gifModal.style.display = 'flex';
gifModal.classList = 'gif-modal reveal';
gifBtn.textContent = 'X';
break;
case false:
gifModal.classList = 'gif-modal hide';
gifBtn.textContent = 'GIF';
break;
default:
break;
}
});
// this makes it so you can click anywhere on the screen and the gif modal will dissapear
window.addEventListener('click', (e) => {
console.log(gifModalState);
if (gifModalState) {
gifModalState = false;
gifModal.classList = 'gif-modal hide';
gifBtn.textContent = 'GIF';
}
});
// .giphy-search-input
// .giphy-search-btn
const searchInput = document.querySelector('#giphy-search-input');
const searchBtn = document.querySelector('#giphy-search-btn');
const gif1 = document.querySelector('#gif1');
const gif2 = document.querySelector('#gif2');
const gif3 = document.querySelector('#gif3');
searchBtn.addEventListener('click', (e) => {
e.stopPropagation();
const api_key = 'qkHJRINHutZAVA8f5WJpG2xOTRIxjGOE';
const query = searchInput.value;
let gif1Url = '';
let gif2Url = '';
let gif3Url = '';
fetch(
`https://api.giphy.com/v1/gifs/search?api_key=${api_key}&q=${query}&limit=3`,
{
headers: {
'Content-Type': 'application/json',
},
}
)
.then((res) => res.json())
.then((res) => {
console.log(res);
gif1.src = res.data[0].images.fixed_height.url;
gif2.src = res.data[1].images.fixed_height.url;
gif3.src = res.data[2].images.fixed_height.url;
})
.catch((err) => console.log(err));
searchInput.value = '';
});
let chosenGif = '';
const saveChosenGif = (url) => {
chosenGif = url;
gifModalState = false;
gifModal.classList = 'gif-modal hide';
gifBtn.textContent = 'GIF';
};
gif1.addEventListener('click', (e) => {
saveChosenGif(e.target.src);
});
gif2.addEventListener('click', (e) => {
saveChosenGif(e.target.src);
});
gif3.addEventListener('click', (e) => {
saveChosenGif(e.target.src);
});