-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageGallery.html
188 lines (173 loc) · 6.27 KB
/
ImageGallery.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.toggle-container {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.toggle-container label {
margin-left: 10px;
font-size: 16px;
}
.gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.gallery img {
max-width: 200px;
max-height: 200px;
object-fit: cover;
border: 2px solid #ddd;
border-radius: 4px;
transition: transform 0.2s;
}
.gallery img:hover {
transform: scale(1.05);
}
.show-more {
display: block;
margin: 20px auto;
padding: 10px 20px;
background-color: #CCC;
color: white;
border: none;
cursor: pointer;
font-size: 16px;
border-radius: 10px;
transition: background-color 0.3s;
}
.show-more:hover {
background-color: #8f8f8f;
}
.loading {
text-align: center;
margin: 20px 0;
font-size: 18px;
color: #555;
}
/* Toggle Switch Styles */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0; left: 0;
right: 0; bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px; width: 26px;
left: 4px; bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
</style>
</head>
<body>
<h3>Image Gallery</h3>
<!-- Toggle Switch -->
<div class="toggle-container">
<label class="switch">
<input type="checkbox" id="api-toggle">
<span class="slider"></span>
</label>
<label for="api-toggle">Change S3 bucket</label>
</div>
<div class="gallery" id="gallery"></div>
<div class="loading" id="loading" style="display: none;">Loading...</div>
<button class="show-more" id="show-more" onclick="loadImages()">Show More</button>
<script>
let nextPageToken = "";
let currentEndpoint = 'https://iwleaxdxnl.execute-api.ap-northeast-1.amazonaws.com/default/psy-images-gallery'; // Harry's S3 bucket
const alternateEndpoint = 'https://i1gjpj1st4.execute-api.ap-northeast-1.amazonaws.com/default/psy-images-gallery'; // Richie's S3 bucket
// Function to load images
async function loadImages() {
document.getElementById('loading').style.display = 'block';
document.getElementById('show-more').style.display = 'none';
try {
// console.log('Fetching images from:', currentEndpoint, 'with page token:', nextPageToken);
const response = await fetch(currentEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
page_token: nextPageToken
})
});
if (response.ok) {
const result = await response.json();
// Assuming the API returns { images: [...], next_page_token: "..." }
const images = result.images;
// console.log('Fetched images:', images);
nextPageToken = result.next_page_token;
const gallery = document.getElementById('gallery');
images.forEach(imageUrl => {
const imgElement = document.createElement('img');
imgElement.src = imageUrl;
imgElement.alt = "Gallery Image";
gallery.appendChild(imgElement);
});
document.getElementById('loading').style.display = 'none';
if (nextPageToken) {
document.getElementById('show-more').style.display = 'block';
} else {
document.getElementById('show-more').style.display = 'none';
}
} else {
console.error('Failed to fetch images:', response.statusText);
document.getElementById('loading').style.display = 'none';
document.getElementById('show-more').style.display = 'block';
}
} catch (error) {
console.error('Error loading images:', error);
document.getElementById('loading').style.display = 'none';
document.getElementById('show-more').style.display = 'block';
}
}
// Function to handle toggle change
function handleToggleChange() {
const isAlternate = document.getElementById('api-toggle').checked;
currentEndpoint = isAlternate ? alternateEndpoint : 'https://iwleaxdxnl.execute-api.ap-northeast-1.amazonaws.com/default/psy-images-gallery';
nextPageToken = "";
const gallery = document.getElementById('gallery');
gallery.innerHTML = ''; // Clear the gallery
loadImages();
}
// Add event listener to the toggle switch
document.getElementById('api-toggle').addEventListener('change', handleToggleChange);
// Initial load
loadImages();
</script>
</body>
</html>