-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (137 loc) · 4.99 KB
/
index.js
File metadata and controls
154 lines (137 loc) · 4.99 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
fetch(`https://api.artic.edu/api/v1/artworks`)
.then((response) => {
if (!response.ok) {
throw new Error(`Request failed`);
}
return response.json(); // Parse the response as JSON
})
.then(data => {
console.log("Artworks:", data);
const artworks = data.data;
renderArtworks(artworks);
const artList = document.getElementById("art-list");
artworks.forEach(art => {
const li = document.createElement("li");
const title = document.createElement("h3");
const artist = document.createElement("p");
const img = document.createElement("img");
const description = document.createElement("p");
title.innerText = art.title;
artist.innerText = art.artist_title || "Unknown Artist";
if (art.image_id) {
img.src = `https://www.artic.edu/iiif/2/${art.image_id}/full/200,/0/default.jpg`;
img.alt = art.title;
img.className = "art-image";
li.appendChild(img);
}
description.className = "art-description";
if (art.thumbnail?.alt_text) {
description.innerText = art.thumbnail.alt_text;
} else if (art.artist_display) {
description.innerText = art.artist_display;
} else {
description.innerText = "No description available.";
}
li.appendChild(title);
li.appendChild(artist);
li.appendChild(description);
artList.appendChild(li);
});
})
.catch((error) => {
console.error(`An error occurred:`, error);
}); // old code
const form = document.getElementById("search-form");
const select = document.getElementById("artist-select");
const artList = document.getElementById("art-list");
// Fetch artworks filtered by selected artist name
function fetchArtworksByArtist(artistName) {
const query = encodeURIComponent(artistName);
/* const apiUrl = `https://api.artic.edu/api/v1/artworks/search?q=${query}&limit=20&fields=title,artist_title,image_id`; */
const apiUrl = `https://api.artic.edu/api/v1/artworks/search?q=${query}&limit=20&fields=title,artist_title,image_id,thumbnail,artist_display`;
fetch(apiUrl)
.then((response) => {
if (!response.ok) throw new Error("Request failed");
return response.json();
})
.then((data) => {
const artworks = data.data;
renderArtworks(artworks);
})
.catch((err) => {
console.error("Error:", err);
artList.innerHTML = "<li>Could not load artworks. Try again.</li>";
});
}
// Render artworks in the DOM
function renderArtworks(artworks) {
artList.innerHTML = "";
if (artworks.length === 0) {
artList.innerHTML = "<li>No artworks found for this artist.</li>";
return;
}
artworks.forEach((art) => {
const li = document.createElement("li");
const title = document.createElement("h3");
const artist = document.createElement("p");
const img = document.createElement("img");
const description = document.createElement("p");
title.innerText = art.title;
artist.innerText = art.artist_title || "Unknown Artist";
if (art.image_id) {
img.src = `https://www.artic.edu/iiif/2/${art.image_id}/full/200,/0/default.jpg`;
img.alt = art.title;
img.className = "art-image";
li.appendChild(img);
}
// Add description
description.className = "art-description";
if (art.thumbnail?.alt_text) {
description.innerText = art.thumbnail.alt_text;
} else if (art.artist_display) {
description.innerText = art.artist_display;
} else {
description.innerText = "No description available.";
}
// Append elements to the list item
li.appendChild(title);
li.appendChild(artist);
li.appendChild(description);
artList.appendChild(li);
});
}
// Handle form submission
form.addEventListener("submit", (e) => {
e.preventDefault();
const selectedArtist = select.value;
if (selectedArtist) {
fetchArtworksByArtist(selectedArtist);
} else {
artList.innerHTML = "<li>Please select an artist first.</li>";
}
});
// Create and append the footer
const footer = document.createElement("footer");
footer.id = "main-footer";
const footerContent = document.createElement("div");
footerContent.className = "footer-content";
const text = document.createElement("p");
text.innerHTML = "© 2025 Art Explorer. All rights reserved.";
/* footer.innerHTML = `<p>© 2025 Art Explorer. All rights reserved.</p>`; */
// Create image element
const img = document.createElement("img");
img.src = "images/unnamed.png";
img.alt = "Logo";
img.className = "footer-logo";
img.style.width = "40px";
img.style.height = "40px";
// Append image and text to the container
footerContent.appendChild(img);
footerContent.appendChild(text);
// Append container to the footer
footer.appendChild(footerContent);
// Append footer to the body
document.body.appendChild(footer);
/* footer.appendChild(img);
// Append the footer to the body
document.body.appendChild(footer); */