Repository navigation
Expand file tree
/
Copy path31-fetch-data.htm
More file actions
513 lines (371 loc) · 17.7 KB
/
Copy path31-fetch-data.htm
File metadata and controls
513 lines (371 loc) · 17.7 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fetch Data</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- LIBRARY FILES: basic.css and basic.js come first, your own code after them -->
<link rel="preload" href="../basic/font/open-sans/OpenSans-Regular.ttf" as="font" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../basic/basic.css">
<script src="../basic/basic.js" type="text/javascript" charset="utf-8"></script>
<script>
/*
FETCH DATA
Loads a list of users from a server (JSONPlaceholder, a free test API) and shows it.
A page that loads data has three states, and each one needs its own view:
- Loading -> Gray rows in the shape of the user rows. They pulse until the data comes.
- Error -> What went wrong and a "Try again" button.
- Ready -> The rows. The search box filters them without a new request.
- fetch(url) + await -> asks the server, and waits for the answer without stopping the page.
(await works only in an async function: see Users.load.)
- response.ok, response.json() -> was the answer a good one? Its text as JavaScript objects.
- AbortSignal.timeout(ms) -> the request stops when the server does not answer in 8 seconds.
- requestId -> an old answer that comes late is thrown away.
- list.children + remove() -> the rows of the old state are removed...
- createIn(list, fn) -> ...and the rows of the new state are created in the list.
- plainText -> data from a server is shown as text, never as HTML.
- list.every(ms, fn) -> fn runs again and again (the pulse), until its stop function is called.
The page needs the internet (it loads https://jsonplaceholder.typicode.com/users), but not a
server: it works when you open the file directly. Without the internet, the error view says so.
"Simulate error" asks for a page that does not exist, to see the error view.
TRY: set REQUEST_TIMEOUT to 1 (ms): the error view says that the server did not answer in time.
*/
// *** VARIABLES:
const API_URL = "https://jsonplaceholder.typicode.com/users";
const WRONG_URL = "https://jsonplaceholder.typicode.com/no-such-page";
const REQUEST_TIMEOUT = 8000; // ms
const MAX_WIDTH = 720; // The column of the page is not wider than this.
const ROW_HEIGHT = 76;
const AVATAR_SIZE = 44;
const SKELETON_ROW_COUNT = 6;
const PULSE_TIME = 700; // ms: the gray rows fade out, then in, in this time.
const PULSE_OPACITY = 0.45;
const DARK_BUTTON_COLOR = "#2F2F2F";
const ERROR_COLOR = "#C0392B";
const AVATAR_COLORS = ["#E07A5F", "#3D8B7D", "#5B6CD9", "#C9A227", "#8E6C8A", "#3E7CB1"];
const TOOLBAR_BUTTON = {
width: "auto",
height: 44,
padding: [16, 0],
round: 10,
minimal: 1,
fontSize: 14,
};
// WHY: The page helpers are in one object, so their names can not clash with basic.js.
const Users = {};
Users.all = []; // The users from the server.
Users.state = ""; // "loading", "error" or "ready"
Users.requestId = 0; // The number of the last request.
Users.simulateError = 0; // 1: the requests ask for a page that does not exist.
Users.stopPulse = null; // The stop function of the pulse timer. (list.every() returns it.)
let searchInput, errorButton, countLabel, list;
// *** START:
const start = function () {
page.color = "#F2F3F7";
// GROUP: Fills the page and puts the column in its middle
VGroup({ width: "100%", height: "100%", align: "center top", padding: [16, 24] });
// GROUP: The column: title, toolbar, count and the list
VGroup({
width: "100%",
height: "100%",
gap: 14,
align: "left top",
css: { maxWidth: MAX_WIDTH + "px" },
});
// LABEL: The title
Label({ text: "Users", fontSize: 26, bold: 1, textColor: Black(0.8) });
Users.createToolbar();
// LABEL: "10 users", "Loading..."
countLabel = Label({ text: "", fontSize: 13, textColor: Black(0.45) });
// BOX: The list scrolls, the toolbar stays. (grow: 1 -> the rest of the height)
startBox({ width: "100%", height: 10, grow: 1, color: "transparent", scrollY: 1 });
// GROUP: The rows of the state that is shown
list = VGroup({ width: "100%", height: "auto", align: "left top", gap: 10, padding: [0, 4] });
endGroup();
endBox();
endGroup();
endGroup();
Users.load();
};
// *** VIEW:
Users.createToolbar = function () {
// GROUP: Search, reload and the error switch. (wrap: on a phone the buttons go to the next line.)
HGroup({ width: "100%", height: "auto", gap: 10, wrap: 1, align: "left center" });
// INPUT: Filters the rows that are already loaded
searchInput = Input({
width: 10,
height: 44,
grow: 1,
fontSize: 15,
round: 10,
borderColor: Black(0.15),
placeholder: "Search a name, e-mail, city...",
inputType: "search",
css: { minWidth: "220px" },
});
searchInput.on("input", Users.showUsers);
// BUTTON: Loads the list again
Button({ text: "Reload", color: DARK_BUTTON_COLOR, textColor: "white", ...TOOLBAR_BUTTON });
that.on("click", Users.load);
// BUTTON: On / off
errorButton = Button({
text: "Simulate error: off",
color: "white",
textColor: Black(0.6),
border: 1,
borderColor: Black(0.15),
...TOOLBAR_BUTTON,
});
errorButton.on("click", Users.toggleSimulatedError);
endGroup();
};
// One user: avatar, name and e-mail, city and company.
// WHY: Everything that comes from the server is set with plainText, never with text (innerHTML):
// a name like "<img src=x onerror=...>" is shown as it is, and can not run code.
Users.createRow = function (user) {
const avatarColor = AVATAR_COLORS[user.id % AVATAR_COLORS.length];
// GROUP: The row
HGroup({
width: "100%",
height: ROW_HEIGHT,
gap: 14,
padding: [16, 0],
align: "left center",
color: "white",
round: 12,
});
// LABEL: The avatar: the initials in a colored circle
Label({
plainText: Users.getInitials(user.name),
width: AVATAR_SIZE,
height: AVATAR_SIZE,
round: AVATAR_SIZE / 2,
color: avatarColor,
textColor: "white",
fontSize: 16,
bold: 1,
textAlign: "center",
lineHeight: AVATAR_SIZE, // One line as high as the circle: the text is in its middle.
});
// GROUP: Name and e-mail. width: 10 + grow: 1 -> it takes the free width, and a long
// text ends with "..." (ellipsis) instead of pushing the city out of the row.
VGroup({ width: 10, height: "auto", grow: 1, gap: 2, align: "left center" });
Label({
plainText: user.name,
width: "100%",
fontSize: 16,
bold: 1,
textColor: Black(0.8),
ellipsis: 1,
});
Label({
plainText: user.email,
width: "100%",
fontSize: 13,
textColor: Black(0.5),
ellipsis: 1,
});
endGroup();
// GROUP: City and company
VGroup({ hug: 1, gap: 2, align: "right center" });
Label({ plainText: user.address.city, fontSize: 14, textColor: Black(0.7) });
Label({ plainText: user.company.name, fontSize: 12, textColor: Black(0.4) });
endGroup();
endGroup();
};
// A gray row in the shape of a user row. (Loading)
Users.createSkeletonRow = function () {
// GROUP: The row
HGroup({
width: "100%",
height: ROW_HEIGHT,
gap: 14,
padding: [16, 0],
align: "left center",
color: "white",
round: 12,
});
// BOX: The avatar
Box({ width: AVATAR_SIZE, height: AVATAR_SIZE, round: AVATAR_SIZE / 2, color: Black(0.08) });
// GROUP: Two gray lines in the place of the texts. (random widths: like texts of different lengths)
VGroup({ hug: 1, gap: 8, align: "left center" });
Box({ width: random(120, 200), height: 12, round: 6, color: Black(0.08) });
Box({ width: random(160, 240), height: 10, round: 5, color: Black(0.06) });
endGroup();
endGroup();
};
// A message in the middle of the list. (Error, nothing found)
// message: { icon, title, detail, buttonText, onClick }. Without a buttonText, there is no button.
Users.createMessage = function (message) {
// GROUP: The message
VGroup({ width: "100%", height: "auto", gap: 8, padding: [0, 48], align: "center top" });
Label({ text: message.icon, fontSize: 36 });
Label({
plainText: message.title,
fontSize: 17,
bold: 1,
textColor: Black(0.75),
textAlign: "center",
});
Label({
plainText: message.detail,
fontSize: 14,
textColor: Black(0.5),
textAlign: "center",
});
if (message.buttonText) {
Button({
text: message.buttonText,
width: "auto",
height: 40,
padding: [18, 0],
round: 10,
minimal: 1,
color: DARK_BUTTON_COLOR,
textColor: "white",
fontSize: 14,
});
that.on("click", message.onClick);
}
endGroup();
};
// *** DATA:
// async: the function can wait (await) for the server, and the page keeps working meanwhile.
Users.load = async function () {
// WHY: Reload can be pressed while a request is still on the way. Every request has a
// number, and only the answer of the last one is shown. An older answer that comes
// later is thrown away, so it can not replace the newer list.
Users.requestId++;
const thisRequestId = Users.requestId;
Users.showLoading();
const url = Users.simulateError ? WRONG_URL : API_URL;
try {
const users = await Users.fetchJson(url);
if (thisRequestId !== Users.requestId) return; // A newer request was sent.
Users.all = users;
Users.state = "ready";
Users.showUsers();
} catch (error) {
if (thisRequestId !== Users.requestId) return;
Users.showError(error.message);
}
};
// fetch() with a time limit and clear error texts.
Users.fetchJson = async function (url) {
try {
const response = await fetch(url, { signal: AbortSignal.timeout(REQUEST_TIMEOUT) });
// WHY: fetch() does not throw for 404 or 500. The answer came, but it is an error.
if (!response.ok) {
throw new Error("The server answered with the error " + response.status + ".");
}
return await response.json();
} catch (error) {
throw Users.toReadableError(error);
}
};
// The error of fetch() in words that a user understands.
Users.toReadableError = function (error) {
if (error.name === "TimeoutError") {
const seconds = REQUEST_TIMEOUT / 1000;
return new Error("The server did not answer in " + seconds + " seconds.");
}
// WHY: fetch() throws a TypeError when no answer came at all (no internet, a wrong address).
if (error.name === "TypeError") {
return new Error("There is no connection to the server.");
}
return error;
};
// "Leanne Graham" -> "LG": the first letters of the first two words that start with a capital letter.
Users.getInitials = function (name) {
let initials = "";
name.split(" ").forEach(function (word) {
const startsWithCapital = /^[A-Z]/.test(word);
if (startsWithCapital && initials.length < 2) initials += word[0];
});
return initials;
};
Users.toggleSimulatedError = function () {
Users.simulateError = Users.simulateError ? 0 : 1;
errorButton.text = "Simulate error: " + (Users.simulateError ? "on" : "off");
errorButton.textColor = Users.simulateError ? ERROR_COLOR : Black(0.6);
Users.load();
};
// *** STATES:
// Removes every row of the list, and stops the pulse of the loading view.
Users.clearList = function () {
Users.stopPulsing();
// WHY: list.children is a new array on every call, so remove() in the loop is safe.
list.children.forEach(function (child) {
child.remove();
});
};
Users.showLoading = function () {
Users.state = "loading";
Users.clearList();
countLabel.text = "Loading...";
createIn(list, function () {
for (let i = 0; i < SKELETON_ROW_COUNT; i++) {
Users.createSkeletonRow();
}
});
Users.startPulsing();
};
// The gray rows fade out and in, again and again, until the data comes.
Users.startPulsing = function () {
list.setMotion("opacity " + (PULSE_TIME / 1000) + "s");
Users.stopPulse = list.every(PULSE_TIME, function () {
list.opacity = (list.opacity === 1) ? PULSE_OPACITY : 1;
});
};
Users.stopPulsing = function () {
if (Users.stopPulse) {
Users.stopPulse();
Users.stopPulse = null;
}
list.setMotion("none");
list.opacity = 1;
};
Users.showError = function (errorText) {
Users.state = "error";
Users.clearList();
countLabel.text = "";
createIn(list, function () {
Users.createMessage({
icon: "⚠️",
title: "The users could not be loaded.",
detail: errorText,
buttonText: "Try again",
onClick: Users.load,
});
});
};
// The rows that match the search text. (The search box calls it at every letter.)
Users.showUsers = function () {
// WHY: The search box can be used while the data is loading. There is nothing to filter yet.
if (Users.state !== "ready") return;
const query = searchInput.value.trim().toLowerCase();
const users = Users.all.filter(function (user) {
const text = [user.name, user.email, user.address.city, user.company.name].join(" ");
return text.toLowerCase().includes(query);
});
Users.clearList();
createIn(list, function () {
if (users.length > 0) {
users.forEach(Users.createRow);
return;
}
Users.createMessage({
icon: "🔍",
title: "No user matches \"" + query + "\".",
detail: "Try another name or city.",
});
});
countLabel.text = (query === "")
? Users.all.length + " users"
: users.length + " of " + Users.all.length + " users";
};
</script>
</head>
<body>
</body>
</html>