-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (81 loc) · 3.39 KB
/
script.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
document.addEventListener('DOMContentLoaded', () => {
const startMenu = document.getElementById('start-menu');
const adviceDisplay = document.getElementById('advice-display');
const searchMenu = document.getElementById('search-menu');
const inspireMeBtn = document.getElementById('inspire-me-btn');
const searchBtn = document.getElementById('search-btn');
const newAdviceBtn = document.getElementById('new-advice-btn');
const goBackBtn = document.getElementById('go-back-btn');
const searchInput = document.getElementById('search-input');
const searchInspireBtn = document.getElementById('search-inspire-btn');
const adviceText = document.getElementById('advice-text');
const adviceId = document.getElementById('advice-id');
const errorMessage = document.getElementById('error-message');
const fetchAdvice = async (id = null) => {
const url = id ? `https://api.adviceslip.com/advice/${id}` : 'https://api.adviceslip.com/advice';
const response = await fetch(url);
const data = await response.json();
return data;
};
const displayAdvice = (advice, id) => {
adviceText.textContent = `"${advice}"`;
adviceId.textContent = `ID: ${id}`;
fadeTransition(adviceDisplay, true);
};
const fadeTransition = (element, fadeIn) => {
element.classList.remove('fade-in', 'fade-out', 'hidden');
if (fadeIn) {
element.classList.add('fade-in');
} else {
element.classList.add('fade-out');
setTimeout(() => element.classList.add('hidden'), 500);
}
};
const fadeQuoteTransition = async () => {
adviceText.classList.add('fade-out');
adviceId.classList.add('fade-out');
await new Promise(resolve => setTimeout(resolve, 500)); // wait for the fade-out animation to complete
const data = await fetchAdvice();
adviceText.textContent = `"${data.slip.advice}"`;
adviceId.textContent = `ID: ${data.slip.id}`;
adviceText.classList.remove('fade-out');
adviceId.classList.remove('fade-out');
adviceText.classList.add('fade-in');
adviceId.classList.add('fade-in');
};
inspireMeBtn.addEventListener('click', async () => {
fadeTransition(startMenu, false);
setTimeout(async () => {
const data = await fetchAdvice();
displayAdvice(data.slip.advice, data.slip.id);
}, 500);
});
searchBtn.addEventListener('click', () => {
fadeTransition(startMenu, false);
setTimeout(() => {
fadeTransition(searchMenu, true);
}, 500);
});
newAdviceBtn.addEventListener('click', fadeQuoteTransition);
goBackBtn.addEventListener('click', () => {
fadeTransition(adviceDisplay, false);
setTimeout(() => {
fadeTransition(startMenu, true);
}, 500);
});
searchInspireBtn.addEventListener('click', async () => {
const id = searchInput.value;
if (id) {
const data = await fetchAdvice(id);
if (data.message) {
errorMessage.classList.remove('hidden');
} else {
errorMessage.classList.add('hidden');
fadeTransition(searchMenu, false);
setTimeout(() => {
displayAdvice(data.slip.advice, data.slip.id);
}, 500);
}
}
});
});