Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (106 loc) · 3.38 KB
/
Copy pathscript.js
File metadata and controls
126 lines (106 loc) · 3.38 KB
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
const API_URL = 'https://api.freeapi.app/api/v1/public/cats/cat/random';
const loading = document.getElementById('loading');
const error = document.getElementById('error');
const basicInfo = document.getElementById('basic-info');
const weightInfo = document.getElementById('weight-info');
const temperamentList = document.getElementById('temperament-list');
const ratingsGrid = document.getElementById('ratings-grid');
const imageContainer = document.getElementById('image-container');
async function fetchBreed() {
loading.style.display = 'block';
error.style.display = 'none';
clearDisplays();
try {
const response = await fetch(API_URL);
if (!response.ok) throw new Error(`API error: ${response.status}`);
const data = await response.json();
console.log("API Response:", data);
let breed;
if (data?.data) {
breed = data.data;
} else {
throw new Error("No breed data found");
}
displayBreed(breed);
} catch (err) {
error.textContent = err.message;
error.style.display = 'block';
} finally {
loading.style.display = 'none';
}
}
function clearDisplays() {
basicInfo.innerHTML = '';
weightInfo.innerHTML = '';
temperamentList.innerHTML = '';
ratingsGrid.innerHTML = '';
imageContainer.innerHTML = '';
}
function displayBreed(breed) {
// 🖼 IMAGE FIX
const imageUrl =
breed?.image?.url ||
breed?.image ||
breed?.url;
if (imageUrl) {
const img = document.createElement('img');
img.src = imageUrl;
img.alt = breed.name || 'Cat Image';
imageContainer.appendChild(img);
}
// BASIC INFO
const fields = [
{ label: "Name", value: breed.name },
{ label: "Origin", value: breed.origin },
{ label: "Life Span", value: breed.life_span }
];
fields.forEach(f => {
if (f.value) {
const div = document.createElement('div');
div.className = "info-item";
div.innerHTML = `<b>${f.label}</b><br>${f.value}`;
basicInfo.appendChild(div);
}
});
// WEIGHT
if (breed.weight) {
weightInfo.innerHTML = `
<div><b>Imperial:</b> ${breed.weight.imperial}</div>
<div><b>Metric:</b> ${breed.weight.metric}</div>
`;
}
// TEMPERAMENT
if (breed.temperament) {
breed.temperament.split(',').forEach(t => {
const li = document.createElement('li');
li.textContent = t.trim();
temperamentList.appendChild(li);
});
}
// RATINGS
const ratings = [
{ label: "Adaptability", value: breed.adaptability },
{ label: "Affection", value: breed.affection_level },
{ label: "Energy", value: breed.energy_level },
{ label: "Intelligence", value: breed.intelligence }
];
ratings.forEach(r => {
const div = document.createElement('div');
div.className = "rating-item";
div.innerHTML = `
<b>${r.label}</b><br>
${getStars(r.value)}
`;
ratingsGrid.appendChild(div);
});
}
function getStars(val) {
let stars = "";
for (let i = 0; i < 5; i++) {
stars += i < val
? '<span class="full-star">★</span>'
: '<span class="empty-star">☆</span>';
}
return stars;
}
fetchBreed();