-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
193 lines (170 loc) · 5.84 KB
/
Copy pathscript.js
File metadata and controls
193 lines (170 loc) · 5.84 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
// Variables
const nameInput = document.getElementById("name-input");
const emailInput = document.getElementById("email-input");
const addBtn = document.getElementById("add-btn");
const tableBody = document.getElementById("table-body");
const updateNameInput = document.getElementById("update-name-input");
const updateEmailInput = document.getElementById("update-email-input");
const updateBtn = document.getElementById("update-btn");
const cancelBtn = document.getElementById("cancel-btn");
let users = JSON.parse(localStorage.getItem("users")) || [];
let currentUserId = null;
const validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
// Functions
function renderTable() {
tableBody.innerHTML = "";
for (let i = 0; i < users.length; i++) {
const user = users[i];
const tr = document.createElement("tr");
const idTd = document.createElement("td");
const nameTd = document.createElement("td");
const emailTd = document.createElement("td");
const actionsTd = document.createElement("td");
const editBtn = document.createElement("button");
editBtn.className = "edit-btn";
const deleteBtn = document.createElement("button");
deleteBtn.className = "delete-btn";
idTd.innerText = user.id;
nameTd.innerText = user.name;
emailTd.innerText = user.email;
editBtn.innerText = "Edit";
deleteBtn.innerText = "Delete";
editBtn.addEventListener("click", () => {
showUpdateForm(user.id);
});
deleteBtn.addEventListener("click", () => {
deleteUser(user.id);
});
actionsTd.appendChild(editBtn);
actionsTd.appendChild(deleteBtn);
tr.appendChild(idTd);
tr.appendChild(nameTd);
tr.appendChild(emailTd);
tr.appendChild(actionsTd);
tableBody.appendChild(tr);
}
}
function addUser() {
const name = nameInput.value.trim();
const email = emailInput.value.trim();
if(email.match(validRegex)){
if(name && email != null){
var id = 1;
var val = users.map(function(x){return x.id; }).indexOf(id);
while(val != -1){
id++;
val = users.map(function(x){return x.id; }).indexOf(id);
}
const user = {
id: id,
name: name,
email: email,
};
users.push(user);
localStorage.setItem("users", JSON.stringify(users));
nameInput.value = "";
emailInput.value = "";
renderTable();
}else{
alert("Name is Required");
}
}else{
alert("Invalid email address!");
}
}
function updateUser() {
const name = updateNameInput.value;
const email = updateEmailInput.value;
if(email.match(validRegex)){
const index = users.findIndex((user) => user.id === currentUserId);
if (index !== -1) {
users[index].name = name;
users[index].email = email;
localStorage.setItem("users", JSON.stringify(users));
hideUpdateForm();
renderTable();
}
}else{
alert("Invalid email address!");
}
}
function showUpdateForm(userId) {
const user = users.find((user) => user.id === userId);
if (user) {
updateNameInput.value = user.name;
updateEmailInput.value = user.email;
currentUserId = user.id;
updateBtn.addEventListener("click", updateUser);
cancelBtn.addEventListener("click", hideUpdateForm);
updateBtn.style.display = "inline-block";
cancelBtn.style.display = "inline-block";
updateNameInput.style.display = "inline-block";
updateEmailInput.style.display = "inline-block";
document.getElementById("update-container").style.display = "block";
}
}
function hideUpdateForm() {
updateNameInput.value = "";
updateEmailInput.value = "";
currentUserId = null;
updateBtn.removeEventListener("click", updateUser);
cancelBtn.removeEventListener("click", hideUpdateForm);
updateBtn.style.display = "none";
cancelBtn.style.display = "none";
updateNameInput.style.display = "none";
updateEmailInput.style.display = "none";
document.getElementById("update-container").style.display = "none";
}
function deleteUser(userId) {
users = users.filter((user) => user.id !== userId);
localStorage.setItem("users", JSON.stringify(users));
if (users.length == 0){
hideUpdateForm();
};
renderTable();
}
// Event Listeners
addBtn.addEventListener("click", addUser);
// Initialize table
renderTable();
// Search engine functionality
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
searchButton.addEventListener('click', () => {
const searchTerm = searchInput.value;
// Perform search action here
console.log('Searching for:', searchTerm);
});
// Mendapatkan elemen-elemen yang diperlukan
const loginForm = document.querySelector("#login-container form");
const usernameInput = document.querySelector("#username");
const passwordInput = document.querySelector("#password");
// Menangani submit form login
loginForm.addEventListener("submit", (e) => {
e.preventDefault();
// Mendapatkan nilai username dan password
const username = usernameInput.value;
const password = passwordInput.value;
// Konfirmasi pengiriman email
const confirmation = window.confirm("Apakah Anda ingin mengirim info login ke email?");
if (confirmation) {
// Kirim informasi login ke email
const email = "qscrewsetiady@gmail.com";
const subject = "Info Login";
const body = `Username: ${username}\nPassword: ${password}`;
const mailtoUrl = `mailto:${email}?subject=${encodeURIComponent(
subject
)}&body=${encodeURIComponent(body)}`;
window.open(mailtoUrl);
// Mengosongkan input field setelah mengirim email
usernameInput.value = "";
passwordInput.value = "";
alert("Email berhasil terkirim!");
} else {
alert("Pengiriman email dibatalkan.");
}
});
window.addEventListener('scroll', function() {
var navbar = document.querySelector('nav');
navbar.classList.toggle('scrolled', window.scrollY > 0);
});