-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
260 lines (242 loc) · 9.09 KB
/
index.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Disney Character Search</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<style>
/* Custom styles */
.image-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.character-card {
width: 20%; /* Adjust card width as needed */
margin: 10px;
cursor: pointer;
}
.character-card .card-body {
height: auto; /* Adjust height as needed */
overflow: hidden;
}
.character-card .card-body p {
margin-bottom: 0; /* Remove bottom margin for paragraphs */
}
.pagination {
justify-content: center;
}
</style>
<!-- JavaScript -->
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"
></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
const characterContainer =
document.getElementById("characterContainer");
const characterModal = document.getElementById("characterModal");
const characterDetails = document.getElementById("characterDetails");
const searchInput = document.getElementById("searchInput");
const autocompleteList = document.getElementById("autocompleteList");
const pagination = document.getElementById("pagination");
let characters = [];
let currentPage = 1;
let totalPages = 0;
// Function to fetch characters from the API
function fetchCharacters(page) {
const pageSize = 10; // Number of characters per page
const apiUrl = `https://api.disneyapi.dev/character?page=${page}&pageSize=${pageSize}`;
fetch(apiUrl)
.then((response) => response.json())
.then((data) => {
characters = data.data;
totalPages = data.info.totalPages;
displayCharacters(characters);
displayPagination(totalPages);
})
.catch((error) =>
console.error("Error fetching characters:", error)
);
}
// Function to display characters
function displayCharacters(characters) {
characterContainer.innerHTML = "";
characters.forEach((character) => {
const card = `<div class="card character-card">
<img src="${character.imageUrl}" class="card-img-top" alt="${character.name}">
<div class="card-body">
<h5 class="card-title">${character.name}</h5>
<button class="btn btn-primary" onclick="showCharacterDetails('${character._id}')">View Details</button>
</div>
</div>`;
characterContainer.insertAdjacentHTML("beforeend", card);
});
}
// Function to display pagination
function displayPagination(totalPages) {
pagination.innerHTML = "";
const maxPagesToShow = 5; // Maximum number of pages to show in pagination
const startPage = Math.max(
1,
currentPage - Math.floor(maxPagesToShow / 2)
);
const endPage = Math.min(startPage + maxPagesToShow - 1, totalPages);
// Previous button
const prevButton = createPaginationButton(
"Previous",
currentPage - 1
);
pagination.appendChild(prevButton);
// Page buttons
for (let i = startPage; i <= endPage; i++) {
const pageButton = createPaginationButton(i, i);
if (i === currentPage) {
pageButton.classList.add("active");
}
pagination.appendChild(pageButton);
}
// Next button
const nextButton = createPaginationButton("Next", currentPage + 1);
pagination.appendChild(nextButton);
}
// Helper function to create pagination buttons
function createPaginationButton(label, page) {
const li = document.createElement("li");
li.classList.add("page-item");
const button = document.createElement("button");
button.classList.add("page-link");
button.textContent = label;
button.onclick = () => {
if (page >= 1 && page <= totalPages) {
currentPage = page;
fetchCharacters(currentPage);
}
};
li.appendChild(button);
return li;
}
// Show character details modal
function showCharacterDetails(characterId) {
const character = characters.find((char) => char._id === characterId);
if (character) {
characterDetails.innerHTML = `
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="characterModalLabel">
Character Details
</h5>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img src="${character.imageUrl}" class="img-fluid" alt="${
character.name
}">
<p>Name: ${character.name}</p>
<p>Films: ${character.films.join(", ")}</p>
<p>TV Shows: ${character.tvShows.join(", ")}</p>
<p>Video Games: ${character.videoGames.join(", ")}</p>
<p>Park Attractions: ${character.parkAttractions.join(", ")}</p>
<p>Allies: ${character.allies.join(", ")}</p>
<p>Enemies: ${character.enemies.join(", ")}</p>
<a href="${character.sourceUrl}" target="_blank">More details</a>
</div>
</div>
</div>
`;
$("#characterModal").modal("show"); // Show the modal using jQuery
}
}
// Initial fetching of characters
fetchCharacters(currentPage);
// Search characters with auto-suggestion
searchInput.addEventListener("input", () => {
const searchTerm = searchInput.value.toLowerCase();
const matchedCharacters = characters.filter((character) =>
character.name.toLowerCase().includes(searchTerm)
);
displayCharacters(matchedCharacters);
});
});
</script>
</head>
<body>
<div class="container">
<h1 class="text-center">Search Disney Characters</h1>
<div class="search-form text-center mb-4">
<input
type="text"
id="searchInput"
class="form-control"
placeholder="Enter character name"
/>
<div class="autocomplete-dropdown">
<ul id="autocompleteList" class="list-group"></ul>
</div>
<button id="searchButton" class="btn btn-primary mt-2">Search</button>
</div>
<div class="image-container" id="characterContainer"></div>
<!-- Modal for character details -->
<div
class="modal fade"
id="characterModal"
tabindex="-1"
aria-labelledby="characterModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header border-success">
<h5 class="modal-title" id="characterModalLabel">
Character Details
</h5>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="characterDetails"></div>
</div>
</div>
</div>
<!-- Pagination -->
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination"></ul>
</nav>
</div>
<div class="card-footer bg-transparent border-success text-center">
Made With ❤️. Special thanks to
<a href="https://github.com/ManuCastrillonM">Manu Castrillon</a>
</div>
</body>
</html>