-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
141 lines (116 loc) · 3.86 KB
/
Copy pathscripts.js
File metadata and controls
141 lines (116 loc) · 3.86 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
let users = []
let page = 1
let totalPages = 0
let actualAction = ""
$(document).ready(function () {
$('#userModal').on('hidden.bs.modal', function (e) {
fillForm({ id: ' ', firstName: ' ', lastName: ' ', email: ' ' })
})
getUsers();
});
function getUsers() {
fetch(`http://localhost:5000/users?page=${page}`)
.then(response => {
if (response.status === 400) {
page--
reloadData()
return Promise.reject();
}
return response.json()
})
.then(body => {
totalPages = body.pages
users = [...body.data]
const tbody = $('#user-table-body')
if (users.length) {
users.forEach(user => {
tbody.append(`
<tr>
<th scope="row">${user.id}</th>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.email}</td>
<td>
<ion-icon name="create-outline" class="action-icon edit-icon" data-toggle="modal"
data-target="#userModal" onclick="editUser(${user.id})"></ion-icon>
<ion-icon name="trash-outline" class="action-icon delete-icon" onclick="remove(${user.id})"></ion-icon>
</td>
</tr >
`)
});
} else {
tbody.append(`
<tr>
<td class="no-users-alert" colspan="5">
There is no users
</td>
</tr>
`)
}
const paginationButtons = $('#pagination-buttons')
for (let i = 1; i <= totalPages; i++) {
paginationButtons.append(`
<button type="button" class="btn btn-secondary" onclick="changePage(${i})">${i}</button>
`)
}
})
}
function changePage(nextPage) {
page = nextPage
reloadData()
}
function createUser() {
actualAction = "create"
changeModalTitle('New Stock')
}
function editUser(id) {
actualAction = "update"
const user = users.find(user => user.id === id)
changeModalTitle(`${user.firstName} ${user.lastName}`)
fillForm(user)
}
function changeModalTitle(text) {
$('#userModalLabel').html(text)
}
function fillForm({ id, firstName, lastName, email }) {
$('#userIdInput').val(id)
$('#firstNameInput').val(firstName)
$('#lastNameInput').val(lastName)
$('#emailInput').val(email)
}
function saveChanges() {
var firstName = $('#firstNameInput').val()
var lastName = $('#lastNameInput').val()
var email = $('#emailInput').val()
const user = { firstName, lastName, email }
const id = $('#userIdInput').val()
const url = actualAction === 'create' ? 'http://localhost:5000/users' : `http://localhost:5000/users/${id}`
const method = actualAction === 'create' ? 'post' : 'put'
fetch(url, {
method,
headers: {
'Accept': 'application/json, text/plain, /',
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
}).then(res => res.json())
.then(() => closeModalAndReloadData());
}
function remove(id) {
fetch(`http://localhost:5000/users/${id}`, {
method: 'delete',
headers: {
'Accept': 'application/json, text/plain, /',
'Content-Type': 'application/json'
},
}).then(() => closeModalAndReloadData())
}
function closeModalAndReloadData() {
$('#userModal').modal('hide')
reloadData()
}
function reloadData() {
$('#pagination-buttons').html('')
$('#user-table-body').html('')
getUsers()
}