-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
32 lines (32 loc) · 1.22 KB
/
main.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
import './style.css';
import './mobile.css';
const loader = (size) => `<div class="loader" style="width:${size}px; height:${size}px"></div>`;
document.querySelector('#app').innerHTML = `
<div class="flex col justify align h-fill">
<div id="joke" class="flex col justify align">Joke</div>
<button id="fetch-joke" class="flex justify align">Get New</button>
</div>
`;
const button = document.querySelector('#fetch-joke');
const jokeContainer = document.querySelector('#joke');
const updateJoke = (joke) => {
let innerHTML = '';
if (joke.type === 'twopart') {
innerHTML = `<div class="setup joke-content">${joke.setup}</div> <div class="joke-content">${joke.delivery}</div>`;
}
if (joke.type === 'single') {
innerHTML = `<div class="joke-content">${joke.joke}</div>`;
}
jokeContainer.innerHTML = innerHTML;
};
button.addEventListener('click', async () => {
button.setAttribute('disabled', '');
const prevText = button.innerHTML;
button.innerHTML = loader(20);
const response = await fetch('https://v2.jokeapi.dev/joke/Any');
const json = await response.json();
console.log(json);
updateJoke(json);
button.removeAttribute('disabled');
button.innerHTML = prevText;
});