-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (111 loc) · 4.98 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
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
document.addEventListener('DOMContentLoaded', () => {
const randomFactButton = document.getElementById('random-fact');
const breedFactButton = document.getElementById('breed-fact');
const factContainer = document.getElementById('fact-container');
const factText = document.getElementById('fact');
const newButtons = document.getElementById('new-buttons');
const newFactButton = document.getElementById('new-fact');
const chooseDifferentButton = document.getElementById('choose-different');
const buttonContainer = document.getElementById('button-container');
const dropdownContainer = document.getElementById('dropdown-container');
const breedSelect = document.getElementById('breed-select');
const getBreedFactButton = document.getElementById('get-breed-fact');
const breedDetails = document.getElementById('breed-details');
const breedName = document.getElementById('breed-name');
const breedCountry = document.getElementById('breed-country');
const breedOrigin = document.getElementById('breed-origin');
const breedCoat = document.getElementById('breed-coat');
const breedPattern = document.getElementById('breed-pattern');
const themeToggle = document.getElementById('theme-toggle');
let lastChoice = ''; // Variable to store the last choice ('random' or 'breed')
let lastBreed = ''; // Variable to store the last selected breed
const fetchRandomFact = async () => {
const response = await fetch('https://catfact.ninja/fact');
const data = await response.json();
return data.fact;
};
const fetchBreedFact = async (breed) => {
// If there's an API that supports breed-specific facts, it will be used here
// For now, we'll use the same random fact API
return fetchRandomFact();
};
const displayFact = async (factPromise) => {
const fact = await factPromise;
factText.textContent = fact;
buttonContainer.classList.add('hidden');
dropdownContainer.classList.add('hidden');
factContainer.classList.remove('hidden', 'fade-out');
factContainer.classList.add('fade-in');
newButtons.classList.remove('hidden');
};
const loadBreeds = async () => {
const response = await fetch('breeds.json');
const data = await response.json();
return data.breeds;
};
const populateBreedDropdown = async () => {
const breeds = await loadBreeds();
breedSelect.innerHTML = breeds.map(breed => `<option value="${breed.breed}">${breed.breed}</option>`).join('');
};
const displayBreedDetails = (breed) => {
breedName.textContent = breed.breed;
breedCountry.textContent = breed.country;
breedOrigin.textContent = breed.origin;
breedCoat.textContent = breed.coat;
breedPattern.textContent = breed.pattern;
breedDetails.classList.remove('hidden');
};
const applyTheme = (theme) => {
document.body.classList.toggle('dark', theme === 'dark');
document.querySelector('.container').classList.toggle('dark', theme === 'dark');
themeToggle.checked = theme === 'dark';
};
const saveTheme = (theme) => {
localStorage.setItem('theme', theme);
};
const loadTheme = () => {
const savedTheme = localStorage.getItem('theme') || 'light';
applyTheme(savedTheme);
};
themeToggle.addEventListener('change', () => {
const theme = themeToggle.checked ? 'dark' : 'light';
applyTheme(theme);
saveTheme(theme);
});
randomFactButton.addEventListener('click', () => {
lastChoice = 'random';
displayFact(fetchRandomFact());
});
breedFactButton.addEventListener('click', async () => {
await populateBreedDropdown();
buttonContainer.classList.add('hidden');
dropdownContainer.classList.remove('hidden');
});
getBreedFactButton.addEventListener('click', async () => {
const selectedBreed = breedSelect.value;
lastChoice = 'breed';
lastBreed = selectedBreed;
const breeds = await loadBreeds();
const breed = breeds.find(b => b.breed === selectedBreed);
displayBreedDetails(breed);
displayFact(fetchBreedFact(selectedBreed));
});
newFactButton.addEventListener('click', () => {
if (lastChoice === 'random') {
displayFact(fetchRandomFact());
} else if (lastChoice === 'breed') {
displayFact(fetchBreedFact(lastBreed));
}
});
chooseDifferentButton.addEventListener('click', () => {
factContainer.classList.add('fade-out');
newButtons.classList.add('hidden');
breedDetails.classList.add('hidden');
buttonContainer.classList.remove('hidden');
setTimeout(() => {
factContainer.classList.add('hidden');
factContainer.classList.remove('fade-out');
}, 500); // Wait for the fade-out animation to complete
});
loadTheme(); // Load the theme when the page loads
});