-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
189 lines (167 loc) · 5.68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product Search</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #2f392f;
color: white;
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px;
}
.logo {
font-size: 32px;
font-weight: bold;
}
form {
display: flex;
align-items: center;
justify-content: center;
margin: 20px 0;
}
.search-input {
padding: 10px;
font-size: 18px;
border: none;
border-radius: 5px;
width: 50%;
margin-right: 10px;
}
.search-timer {
white-space: nowrap;
padding: 0 15px;
}
.search-btn {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 18px;
cursor: pointer;
}
.grid-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 10px;
margin-top: 20px;
padding: 20px;
}
.grid-item {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
height: 400px;
border-radius: 5px;
overflow: hidden;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
transition: box-shadow 0.2s ease-in-out;
}
.grid-item:hover {
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.4);
}
.grid-item img {
max-height: 100%;
max-width: 100%;
object-fit: cover;
}
.product-name {
font-size: 18px;
text-align: center;
margin: 10px 0;
font-weight: bold;
}
.product-price {
font-size: 24px;
font-weight: bold;
text-align: center;
margin-bottom: 10px;
}
</style>
</head>
<body>
<header>
<div class="logo">Product Search</div>
<form>
<div style="display: flex; flex-direction: column; text-align: right">
<div id="search-timer" class="search-timer"></div>
<div id="search-timer2" class="search-timer"></div>
<div id="search-timer3" class="search-timer"></div>
</div>
<input type="text" id="search-input" class="search-input" placeholder="Search...">
<button type="submit" class="search-btn">Search</button>
</form>
</header>
<div class="grid-container" id="results"></div>
<script>
const searchInput = document.getElementById('search-input');
const resultsDiv = document.getElementById('results');
const domain = 'https://stage-sulu.next.mofakult.ch';
const searchTimer = document.getElementById('search-timer');
const queryTime = document.getElementById('search-timer2');
const normalizeTime = document.getElementById('search-timer3');
// Declare an AbortController and an AbortSignal object
let controller = new AbortController();
let signal = controller.signal;
// Helper function to format price from cents to euros
function formatPrice(priceInCents) {
return (priceInCents / 100).toFixed(2) + ' €';
}
searchInput.addEventListener('input', () => {
const searchTerm = searchInput.value;
if (!searchTerm) {
resultsDiv.innerHTML = '';
return;
}
// Abort the previous request before sending a new one
controller.abort();
controller = new AbortController();
signal = controller.signal;
const startTime = Date.now();
searchTimer.innerHTML = 'Loading...';
queryTime.innerHTML = '';
normalizeTime.innerHTML = '';
// fetch the URL with the search term as a parameter
fetch(`${domain}/de/search.json?query=${searchTerm}`, {signal})
.then(response => {
const fetchTime = Date.now() - startTime;
searchTimer.innerHTML = `Results loaded in ${fetchTime} ms`;
return response.json();
})
.then(data => {
queryTime.innerHTML = `Query duration ${(data.timings.query * 1000).toFixed(2)} ms`;
normalizeTime.innerHTML = `Normalization duration ${(data.timings.normalization * 1000).toFixed(2)} ms`;
// create a grid item for each hit in the response
const gridItems = data.hits.map(hit => {
const imgSrc = hit.image ? `${domain}${hit.image.formatUri.replace('{format}', 'x260')}` : '';
const productName = hit.name;
const productPriceInCents = hit.prices.consumer?.grossPrice || 0;
const productPrice = formatPrice(productPriceInCents);
return `
<div class="grid-item" id="${hit.image?.formatUri}">
${imgSrc ? `<img src="${imgSrc}">` : ''}
<div class="product-name">${productName}</div>
<div class="product-price">${productPrice}</div>
</div>
`;
}).join('');
// display the grid items in the results div
resultsDiv.innerHTML = gridItems;
})
.catch(error => console.error(error));
});
</script>
</body>
</html>