-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
190 lines (162 loc) · 6.91 KB
/
Copy pathindex.html
File metadata and controls
190 lines (162 loc) · 6.91 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
<!DOCTYPE html>
<html>
<head>
<title>Random Users!</title>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<script>
$().ready(() => {
const USER_API_URL = 'https://randomuser.me/api';
const USERS_PER_ROW = 3;
const state = {
users: [],
nextId: 0,
currentEditedUserId: null,
};
$('.get-user-button').on('click', () => {
getUser(appendUserElement);
});
for (let i = 0; i < 10; i++) {
getUser(appendUserElement);
}
$('.print-state-button').on('click', () => {
console.log(state);
});
$('#edit-user-form').on('submit', (e) => {
e.preventDefault();
const form = e.currentTarget;
const fullName = form.name.value;
const [first, last] = fullName.split(' ');
const editedFields = {
name: {first: first, last: last},
email: form.email.value
};
const editedUserId = state.currentEditedUserId;
editUserInState(editedUserId, editedFields);
editUserInHTML(editedUserId);
closeEditForm();
});
function getUser(onUserReceived) {
$.get({
url: USER_API_URL,
success: (response) => {
const user = response.results[0];
const userWithNewId = addUserToState(user);
onUserReceived(userWithNewId);
}
});
}
function appendUserElement(user) {
const $user = $(
`
<div class="user col-12 col-md-${Math.floor(12 / USERS_PER_ROW)}">
<img src="${user.picture.large}"/>
<div>
<p class="name">${user.name.first} ${user.name.last}</p>
<p class="email">${user.email}</p>
<button class="delete-button">DELETE</button>
<button class="edit-button">EDIT</button>
</div>
</div>
`
);
$user.find('.delete-button').on('click', () => {
// remove user from HTML
$user.remove();
// remove user from state
removeUserFromState(user)
});
$user.find('.edit-button').on('click', () => {
state.currentEditedUserId = user.id;
prefillEditForm(user);
openEditForm();
});
if (state.users.length % USERS_PER_ROW === 0) {
$('.container').append($('<div class="row"></div>'));
}
$('.row:last').append($user);
}
function prefillEditForm(user) {
const $modal = $('#edit-modal');
$modal.find('input[name=name]').val(`${user.name.first} ${user.name.last}`);
$modal.find('input[name=email]').val(user.email);
}
function openEditForm() {
const $modal = $('#edit-modal');
$modal.modal('show');
}
function closeEditForm() {
const $modal = $('#edit-modal');
$modal.modal('hide');
}
function editUserInHTML(id) {
const userIndex = findUserIndex(id);
const user = state.users[userIndex];
// we use the selector '.user:eq(x)' to avoid iterating over ALL .user in DOM
// this selector will cause jQuery to stop iterating once we get .user:eq(x)
const $user = $(`.user:eq(${userIndex})`);
$user.find('.name').text(`${user.name.first} ${user.name.last}`);
$user.find('.email').text(user.email);
}
function editUserInState(id, fields) {
const userIndex = findUserIndex(id);
state.users[userIndex] = Object.assign(state.users[userIndex], fields);
}
function removeUserFromState(user) {
const userIndex = findUserIndex(user.id);
state.users.splice(userIndex, 1);
}
function addUserToState(user) {
const modifiedUser = Object.assign(user, {
id: state.nextId++
});
state.users.push(modifiedUser);
return modifiedUser;
}
function findUserIndex(id) {
const userIndex = state.users.findIndex(testedUser => testedUser.id === id);
// verify that user exists in state.users
if (userIndex === -1) {
throw new Error(`user index '${id}' is illegal`);
}
return userIndex;
}
});
</script>
</head>
<body>
<div class="app">
<button class="print-state-button">PRINT STATE</button>
<button class="get-user-button">GET USER</button>
<div class="container">
</div>
</div>
<div id="edit-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Edit User</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<form id="edit-user-form" class="form">
<div class="modal-body">
<div>
<label for="name">Name</label>
<input id="name" class="form-control" name="name" />
</div>
<div>
<label for="email">Email</label>
<input id="email" class="form-control" name="email" />
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>