-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser-Profile-List-with-Dynamic-Search.html
598 lines (569 loc) · 17.1 KB
/
User-Profile-List-with-Dynamic-Search.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>User Profile list with Dynamic Search</title>
<style>
body {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS",
sans-serif;
}
#root {
width: 100%;
& > #container {
& > nav {
width: 100%;
height: 50px;
background-color: #bbb8b5;
position: fixed;
top: 0;
left: 0;
z-index: 1;
display: flex;
justify-content: space-around;
align-items: center;
box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px;
& > input[type="text"] {
width: 300px;
height: 30px;
border-radius: 8px;
border: 1px solid transparent;
padding-left: 10px;
}
& > .pagination {
width: 40%;
height: 40px;
display: flex;
justify-content: space-around;
align-items: center;
& button {
width: 45px;
height: 30px;
border-radius: 5px;
border: 1px solid transparent;
box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;
}
}
& > ul {
border-radius: 10px;
position: fixed;
width: 300px;
top: 35px;
left: 10%;
list-style-type: none;
padding: 0;
background-color: #ffffff;
& li {
margin: 10px;
cursor: pointer;
}
& li:hover {
background-color: #b2dcf8;
border-radius: 3px;
}
}
}
& > .user {
position: relative;
top: 50px;
display: flex;
justify-content: left;
margin-bottom: 20px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px,
rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;
& > .left {
display: flex;
justify-content: center;
align-items: center;
background-color: #b2dcf8;
}
& > .right {
width: calc(100% - 10px);
padding-left: 10px;
background-color: #b2dcf8;
padding-right: 10px;
& > .top {
font-size: 17px;
text-align: center;
}
& > .bottom {
width: 100%;
font-size: 14px;
display: flex;
justify-content: center;
gap: 10px;
& > div {
width: 100%;
margin: 10px auto;
border-radius: 10px;
display: flex;
flex-direction: column;
justify-content: left;
row-gap: 10px;
& > div {
padding: 10px;
box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px,
rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
background-color: aliceblue;
border-radius: 5px;
& > .color {
color: rgb(255, 0, 153);
font-size: 18px;
font-weight: bold;
}
}
}
}
}
}
}
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script src="https://www.unpkg.com/react@18.2.0/umd/react.development.js"></script>
<script src="https://www.unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/axios@1.6.7/dist/axios.min.js"></script>
<script type="text/babel">
// Your code will go here
// Functional component for the entire application
const App = () => {
// State variables for managing user data, pagination, search, and loading/error status
const [users, setUsers] = React.useState([]);
const [originalUsers, setOriginalUsers] = React.useState([]);
const [page, setPage] = React.useState(1);
const [searchQuery, setSearchQuery] = React.useState("");
const [limit, setLimit] = React.useState(10);
const [total, setTotal] = React.useState(0);
const [skip, setSkip] = React.useState(0);
const [loading, setLoading] = React.useState(false);
const [error, setError] = React.useState(false);
const [suggestions, setSuggestions] = React.useState([]);
// Function to fetch and update user data
async function fetchAndUpdateData() {
setLoading(true); // Setting loading state to true
// Fetching data from the API
// Using Axios library for making HTTP requests
// The limit and skip parameters are used to paginate the data
try {
const newSkip = (page - 1) * limit;
let resp = await axios({
method: "get",
url: `https://dummyjson.com/users?limit=${limit}&skip=${newSkip}`,
});
// Updating state variables with the fetched data
setUsers(resp.data.users);
setOriginalUsers(resp.data.users);
setLimit(resp.data.limit);
setTotal(resp.data.total);
setLoading(false);
setError(false);
} catch (error) {
// Handling errors
console.log(error);
setLoading(false);
setError(true);
}
}
// Component for displaying user details
const BasicDetails = ({ user }) => {
const {
firstName,
lastName,
maidenName,
age,
gender,
birthDate,
email,
phone,
bloodGroup,
} = user;
return (
<>
<div>
<p className="color">Basic Details</p>
<p>
Name:{" "}
<strong>
{firstName} {lastName}
</strong>
</p>
<p>
Maiden Name: <strong>{maidenName}</strong>
</p>
<p>
Age: <strong>{age}</strong>
</p>
<p>
Gender: <strong>{gender}</strong>
</p>
<p>
Birth Date: <strong>{birthDate}</strong>
</p>
<p>
Blood Group: <strong>{bloodGroup}</strong>
</p>
</div>
<div>
<p className="color">Contact Details</p>
<p>
Phone: <strong>{phone}</strong>
</p>
<p>
Email: <strong>{email}</strong>
</p>
</div>
</>
);
};
// Component for displaying user life style details
const LifeStyle = ({ user }) => {
const {
height,
weight,
eyeColor,
hair: { color, type },
ip,
domain,
} = user;
return (
<>
<div>
<p className="color">Life Style</p>
<p>
Height: <strong>{height}</strong>
</p>
<p>
Weight: <strong>{weight}</strong>
</p>
<p>
Eye Color: <strong>{eyeColor}</strong>
</p>
<p>Hair Style:</p>
<p>
Color: <strong>{color}</strong>
</p>
<p>
Type: <strong>{type}</strong>
</p>
</div>
<div>
<p className="color">Other Details</p>
<p>
IP: <strong>{ip}</strong>
</p>
<p>
Domain: <strong>{domain}</strong>
</p>
</div>
</>
);
};
// Component for displaying user address details
const HomeAddress = ({ user }) => {
const {
address: {
address,
city,
coordinates: { lat, lng },
postalCode,
state,
},
macAddress,
university,
} = user;
return (
<>
<div>
<p className="color">Home Address</p>
<p>
Address: <strong>{address}</strong>
</p>
<p>
City: <strong>{city}</strong>
</p>
<p>
Latitude: <strong>{lat}</strong>
</p>
<p>
Longitude: <strong>{lng}</strong>
</p>
<p>
Postal Code: <strong>{postalCode}</strong>
</p>
<p>
State: <strong>{state}</strong>
</p>
</div>
<div>
<p className="color">Other Details</p>
<p>
MacAddress: <strong>{macAddress}</strong>
</p>
<p>
University: <strong>{university}</strong>
</p>
</div>
</>
);
};
// Component for displaying user office details
const OfficeDetails = ({ user }) => {
const {
company: {
address: {
address,
city,
coordinates: { lat, lng },
postalCode,
state,
},
department,
name,
title,
},
} = user;
return (
<>
<div>
<p className="color">Office Details</p>
<p>
Company: <strong>{name}</strong>
</p>
<p>
Position: <strong>{title}</strong>
</p>
<p>
Department: <strong>{department}</strong>
</p>
</div>
<div>
<p className="color">Office Address</p>
<p>
Address: <strong>{address}</strong>
</p>
<p>
City: <strong>{city}</strong>
</p>
<p>
Latitude: <strong>{lat}</strong>
</p>
<p>
Longitude: <strong>{lng}</strong>
</p>
<p>
Postal Code: <strong>{postalCode}</strong>
</p>
<p>
State: <strong>{state}</strong>
</p>
</div>
</>
);
};
// Component for displaying user bank details
const BankDetails = ({ user }) => {
const {
bank: { cardExpire, cardNumber, cardType, currency, iban },
crypto: { coin, wallet, network },
ssn,
ein,
} = user;
return (
<>
<div>
<p className="color">Bank Details</p>
<p>
Card Number: <strong>{cardNumber}</strong>
</p>
<p>
Card Type: <strong>{cardType}</strong>
</p>
<p>
Card Expire: <strong>{cardExpire}</strong>
</p>
<p>
Currency: <strong>{currency}</strong>
</p>
<p>
IBAN: <strong>{iban}</strong>
</p>
</div>
<div>
<p className="color">Crypto Details</p>
<p>
Coin: <strong>{coin}</strong>
</p>
<p>
Wallet: <strong>{wallet}</strong>
</p>
<p>
Network: <strong>{network}</strong>
</p>
<p>
SSN: <strong>{ssn}</strong>, EIN: <strong>{ein}</strong>
</p>
</div>
</>
);
};
// Function to handle search input
function handleSearch(e) {
let currentSearchQuery = e.target.value;
setSearchQuery(currentSearchQuery);
if (currentSearchQuery) {
let filteredUsers = originalUsers.filter((user) => {
return (
user.firstName
.toLowerCase()
.includes(currentSearchQuery.toLowerCase()) ||
user.lastName
.toLowerCase()
.includes(currentSearchQuery.toLowerCase())
);
});
setUsers(filteredUsers);
let userSuggestion = filteredUsers.map((user) => ({
firstName: user.firstName,
lastName: user.lastName,
id: user.id,
}));
setSuggestions(userSuggestion);
} else {
setUsers(originalUsers);
setSuggestions([]);
}
}
// Function to handle suggestion click
function handleSuggestionClick(userId) {
let selectedUser = originalUsers.find((user) => user.id === userId);
setUsers([selectedUser]);
}
// Function to handle pagination
function Pagination() {
return (
<div className="pagination">
<button onClick={handlePrevPage} disabled={page === 1}>
Prev
</button>
{originalUsers.map((user, index) => {
return (
<button onClick={() => handlePageChange(index + 1)} key={index}>
{index + 1}
</button>
);
})}
<button
onClick={handleNextPage}
disabled={page === Math.ceil(total / limit) || total === 0}
>
Next
</button>
</div>
);
}
// Function to handle page change
function handlePageChange(newPage) {
setPage(newPage);
}
// Function to handle previous page
function handlePrevPage() {
setPage((prevPage) => Math.max(prevPage - 1, 1));
}
// Function to handle next page
function handleNextPage() {
setPage((prevPage) => prevPage + 1);
console.log(users);
}
// Component for displaying individual user
function ShowUser({ user }) {
const { image, username, userAgent } = user;
return (
<div className="user">
<div className="left">
<div className="imgDiv">
<img src={image} alt={username} />
</div>
</div>
<div className="right">
<div className="top">
<p>
User Agent: <strong>{userAgent}</strong>
</p>
</div>
<div className="bottom">
<div className="basicDetails">
<BasicDetails user={user} />
</div>
<div className="lifeStyle">
<LifeStyle user={user} />
</div>
<div className="homeAddress">
<HomeAddress user={user} />
</div>
<div className="officeDetails">
<OfficeDetails user={user} />
</div>
<div className="bankDetails">
<BankDetails user={user} />
</div>
</div>
</div>
</div>
);
}
// Effect hook to fetch data when page or limit changes
React.useEffect(() => {
setSkip((page - 1) * limit);
fetchAndUpdateData(); // Fetching and updating data
}, [page, limit]);
// Rendering loading state
if (loading) {
return <h1>Loading......</h1>;
}
// Rendering error state
if (error) {
return <h1>Something went wrong....</h1>;
}
// Rendering the App component into the UI
return (
<div id="container">
<nav>
<input
type="text"
onChange={(e) => handleSearch(e)}
value={searchQuery}
/>
{suggestions.length > 0 && (
<ul>
{suggestions.map((suggestion, index) => (
<li
key={index}
onClick={() => handleSuggestionClick(suggestion.id)}
>
{suggestion.firstName} {suggestion.lastName}
</li>
))}
</ul>
)}
<Pagination />
</nav>
{users.map((user, index) => (
<ShowUser key={index} user={user} />
))}
</div>
);
};
// Rendering the App component into the root element
let rootElement = document.getElementById("root");
let reactRoot = ReactDOM.createRoot(rootElement);
reactRoot.render(<App />);
</script>
</html>