-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (90 loc) · 3.35 KB
/
index.js
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
const searchBtn = document.querySelector(".search-btn");
const searchField = document.querySelector(".search-bar");
const resultsContainer = document.querySelector(".results-container");
const editModal = new window.bootstrap.Modal(document.getElementById('editModal'), {backdrop:true, keyboard:true, focus:true})
let editId;
const nameField = document.querySelector("#name");
const phoneField = document.querySelector("#phone");
const companyField = document.querySelector("#company");
const updateBtn = document.querySelector("#updateBtn");
const closeBtn = document.querySelector("#closeBtn");
closeBtn.addEventListener('click', ()=> editModal.hide());
const createCard = (user) => {
const resultCard = document.createElement('div');
resultCard.classList.add('result-card');
resultCard.classList.add('card');
const cardHeader = document.createElement('div');
cardHeader.classList.add('card-header');
cardHeader.innerText=(user.name)
resultCard.appendChild(cardHeader);
const cardBody = document.createElement('div');
cardBody.classList.add('card-body');
const cardTitle = document.createElement('h5');
cardTitle.classList.add("card-title");
cardTitle.innerText=(user.phone);
cardBody.appendChild(cardTitle);
const cardText = document.createElement('p');
cardText.classList.add("card-text");
cardText.innerText=(`Company: ${user.company.name}`);
cardBody.appendChild(cardText);
const editBtn = document.createElement('button');
editBtn.classList.add("btn");
editBtn.classList.add("btn-primary");
editBtn.classList.add("edit-btn");
editBtn.innerText=(`EDIT`);
editBtn.dataset.id = user.id;
editBtn.dataset.bsToggle = "modal";
editBtn.dataset.bsTarget = "#editModal";
editBtn.addEventListener("click", openModal)
cardBody.appendChild(editBtn);
const deleteBtn = document.createElement('button');
deleteBtn.classList.add("btn");
deleteBtn.classList.add("btn-primary");
deleteBtn.classList.add("delete-btn");
deleteBtn.innerText=(`DELETE`);
deleteBtn.dataset.id = user.id;
deleteBtn.addEventListener("click", deleteFunc)
cardBody.appendChild(deleteBtn);
resultCard.appendChild(cardBody);
resultsContainer.appendChild(resultCard);
// <div class="result-card card">
// <div class="card-header">
// Leanne Graham
// </div>
// <div class="card-body">
// <h5 class="card-title">1-770-736-8031 x56442</h5>
// <p class="card-text"> Company: Romaguera-Crona</p>
// <button class="btn btn-primary edit-btn">EDIT</button>
// <button clss="btn btn-primary delete-btn">DELETE</button>
// </div>
// </div>
}
const search = async()=>{
const searchQuery = searchField.value;
console.log(searchQuery);
const {data: users} = await axios.get("https://jsonplaceholder.typicode.com/users");
console.log(users);
resultsContainer.innerHTML = '';
users.forEach(user => {
createCard(user);
});
}
const updateUser = ()=>{
const user = {};
if(nameField.value)
user.name = nameField.value;
if(phoneField.value)
user.phone = phoneField.value;
if(companyField.value)
user.company = companyField.value;
console.log(editId,user);
}
const openModal = (e)=>{
editId = e.target.dataset.id;
editModal.show();
}
const deleteFunc = (e)=>{
console.log("delete", e.target.dataset.id);
}
searchBtn.addEventListener("click", search);
updateBtn.addEventListener('click', updateUser);