-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
132 lines (121 loc) · 4.28 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta
name='viewport'
content='width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'
>
<meta
http-equiv='X-UA-Compatible'
content='ie=edge'
>
<title>Ksense Challenge</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<style>
.cardUser {
margin: 2em;
}
</style>
</head>
<body>
<nav class="navbar navbar-light bg-dark">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1 text-white">Users</span>
</div>
</nav>
<div class='container mt-4'>
<div class='row'>
<div class='col-12'>
<table class='table table-striped table-hover'>
<thead class='table-dark'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Website</th>
<th></th>
</tr>
</thead>
<tbody id='users'>
</tbody>
</table>
</div>
</div>
</div>
<!-- MODAL to show user posts -->
<div class="modal" tabindex="-1" id='modalPosts'>
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">See posts for person <span id='personName'></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" id='modalBody'>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
const USER_ENDPOINT = 'https://jsonplaceholder.typicode.com/users';
const POST_ENDPOINT = 'https://jsonplaceholder.typicode.com/posts';
const fetchUser = () => {
$.get(USER_ENDPOINT)
.then((users) => {
$('#users').html(users.map(user => userTr(user)));
})
}
const handleModalOpening = async (userId, personName) => {
const postsByUser = await fetchPostsByUser(userId);
$('#modalBody').html(postsByUser.map(post => handlePostCard(post)));
$('#personName').text(personName);
$('#modalPosts').modal('show');
}
const fetchPostsByUser = (userId) => {
return $.get(POST_ENDPOINT)
.then(posts => {
return Promise.resolve(posts.filter((post) => post.userId === userId));
})
.catch(error => {
return Promise.reject(error);
})
}
const userTr = ({
id,
name,
username,
email,
phone,
website
}) => (
`<tr>
<td>${id}</td>
<td>${name}</td>
<td>${username}</td>
<td>${email}</td>
<td>${phone}</td>
<td>${website}</td>
<td><button class='btn btn-info' onclick='handleModalOpening(${id}, "${name}");'>See Posts</button></td>
</tr>`
);
const handlePostCard = (post) => (
`<div class="card cardUser">
<div class="card-body">
<h5 class="card-title">${post.title}</h5>
<p class="card-text">${post.body}</p>
</div>
</div>`
)
$(document).ready(function(){
fetchUser();
});
</script>
</body>
</html>