-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
199 lines (170 loc) · 7.89 KB
/
Copy pathindex.js
File metadata and controls
199 lines (170 loc) · 7.89 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
document.addEventListener("DOMContentLoaded", function () {
const logIn = localStorage.getItem("loginkey");
if (!logIn || logIn !== "true") {
window.location.href = 'login.html';
}
const modal = document.getElementById("add-modal");
const openModalButton = document.getElementById("open-modal");
openModalButton.addEventListener("click", function () {
const form = modal.querySelector("form");
form.style.display = "flex";
modal.style.display = "block";
});
const closeModalButton = document.getElementById("close-modal");
closeModalButton.addEventListener("click", function () {
modal.style.display = "none";
});
window.addEventListener("click", function (event) {
if (event.target === modal) {
modal.style.display = "none";
}
});
const addButton = document.getElementById("add-button");
addButton.addEventListener("click", function () {
const id = document.getElementById("user-id").value;
const email = document.getElementById("user-email").value;
const firstName = document.getElementById("user-first-name").value;
const lastName = document.getElementById("user-last-name").value;
const avatar = document.getElementById("user-avatar").value;
const user = {
id: id,
email: email,
first_name: firstName,
last_name: lastName,
Avatar: avatar
};
fetch("https://reqres.in/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
})
.then(response => response.json())
.then(data => {
console.log("Apiden gelen:", data);
})
.catch(error => {
console.error("Hata var:", error);
});
const userTableBody = document.querySelector("tbody");
const row = createRow(user);
userTableBody.appendChild(row);
modal.style.display = "none";
});
const userTableBody = document.querySelector("tbody");
let selectedId = null;
userTableBody.addEventListener('click', function (event) {
const hedef = event.target;
if (hedef.classList.contains("edit-button")) {
const editModal = document.getElementById("edit-modal");
const editForm = editModal.querySelector("form");
editModal.style.display = "block";
editForm.style.display = "flex";
const closeEditModalButton = document.getElementById("close-edit-modal");
closeEditModalButton.addEventListener("click", function () {
editModal.style.display = "none";
});
window.addEventListener("click", function (event) {
if (event.target === editModal) {
editModal.style.display = "none";
}
});
const row = hedef.closest("tr");
const id = row.querySelector("td:nth-child(1)").textContent;
const email = row.querySelector("td:nth-child(2)").textContent;
const fullName = row.querySelector("td:nth-child(3)").textContent.split(" ");
const firstName = fullName[0];
const lastName = fullName[1];
const avatar = row.querySelector("td:nth-child(4) img").src;
editForm.querySelector("#edit-user-id").value = id;
editForm.querySelector("#edit-user-email").value = email;
editForm.querySelector("#edit-user-first-name").value = firstName;
editForm.querySelector("#edit-user-last-name").value = lastName;
editForm.querySelector("#edit-user-avatar").value = avatar;
const updateButton = editModal.querySelector("#update-button");
updateButton.addEventListener("click", () => {
const newId = editForm.querySelector("#edit-user-id").value;
const newEmail = editForm.querySelector("#edit-user-email").value;
const newFirstName = editForm.querySelector("#edit-user-first-name").value;
const newLastName = editForm.querySelector("#edit-user-last-name").value;
const newAvatar = editForm.querySelector("#edit-user-avatar").value;
selectedId = id;
updateUser(row, newId, newEmail, newFirstName, newLastName, newAvatar);
editModal.style.display = "none";
});
}
})
function updateUser(row, newId, newEmail, newFirstName, newLastName, newAvatar) {
const user = {
id: newId,
e_mail: newEmail,
first_name: newFirstName,
last_name: newLastName,
avatar: newAvatar
};
if (newId == selectedId) {
row.querySelector("td:nth-child(1").textContent = newId;
row.querySelector("td:nth-child(2)").textContent = newEmail;
row.querySelector("td:nth-child(3)").textContent = newFirstName + " " + newLastName;
row.querySelector("td:nth-child(4) img").src = newAvatar;
fetch(`https://reqres.in/api/users/${newId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
})
.then(response => { return response.json() })
.then(data => {
console.log("Kullanıcı güncellendi:", data);
deleteUser(user.id);
console.log("Eski değerler silindi")
})
.catch(error => {
console.error("Hata:", error);
});
}
}
function createRow(kullanici) {
const row = document.createElement("tr");
row.innerHTML =
` <td>${kullanici.id}</td>
<td>${kullanici.email}</td>
<td>${kullanici.first_name} ${kullanici.last_name}</td>
<td><img src="${kullanici.avatar}" alt="Avatar"></td>
<td>
<button class="delete-button" style="width:100%; height:100%; background:none; border:1px solid white; cursor:pointer;"><i style="font-size:25px; color:#ffa700; cursor:pointer" class="fa delete-button"></i></button>
<button class="edit-button"; style="width:100%; height:100%; background:none; border:1px solid white; cursor:pointer;"><i style="font-size:25px; color:#ffa700; cursor:pointer" class="fa edit-button"></i></button>
</td>
`;
const deleteButton = row.querySelector(".delete-button");
deleteButton.addEventListener("click", () => {
let responseConfirm = confirm("Silmek istediğinize emin misiniz?");
if (responseConfirm == true) {
deleteUser(kullanici.id);
row.remove();
setTimeout(() => alert("Başarıyla silindi"), 300)
}
});
return row;
}
function deleteUser(userId) {
fetch(`https://reqres.in/api/users/${userId}`, {
method: "DELETE"
})
.then(() => { console.log(`${userId} Silindi`) })
}
fetch("https://reqres.in/api/users?page=2")
.then(response => response.json())
.then(veri => {
console.log("Data:", veri)
veri.data.forEach(user => {
console.log(user)
const row = createRow(user);
userTableBody.appendChild(row);
});
console.log("--------------------------------------------");
})
.catch(error => console.error("Error", error));
});