-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrehber.js
184 lines (139 loc) · 4.75 KB
/
rehber.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
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
const isim = document.getElementById("isim")
const soyIsim = document.getElementById("soyisim")
const phoneNumber = document.getElementById("phoneNumber")
const eMail = document.getElementById("exampleInputEmail1")
const form = document.getElementById("form-rehber")
const table = document.querySelector(".table")
const saveButton = document.getElementById("submitBtn")
const peopleList = document.querySelector(".people-list")
const allPeopleArray = []
form.addEventListener('submit', save)
peopleList.addEventListener('click', doPersonOperations)
let chosenRow = undefined;
function save(e){
e.preventDefault();
const personWillBeAddedorEdited = {
ad: isim.value,
soyad: soyIsim.value,
phone: phoneNumber.value,
mail: eMail.value
}
const result = dataControl(personWillBeAddedorEdited)
if(result.status){
if(chosenRow){
editPerson(personWillBeAddedorEdited)
}
else{
addPerson(personWillBeAddedorEdited)
}
}
else{
createInfo(result.message, result.status)
}
}
function addPerson(person){
const createdTR = document.createElement('tr')
createdTR.innerHTML = `
<td>${person.ad}</td>
<td>${person.soyad}</td>
<td>${person.phone}</td>
<td>${person.mail}</td>
<td>
<button class="btn-edit" type="button"><i class="fa-regular fa-pen-to-square text-success"></i></button>
<button class="btn-delete" type="button"><i class="fa-regular fa-trash-can text-danger"></i></button>
</td>
`
peopleList.appendChild(createdTR)
allPeopleArray.push(person)
createInfo('Saved successfully!', true)
console.log(allPeopleArray);
}
function dataControl(person){
//Objelerde in kullanimi
for(const value in person){
if(person[value]){
}
else{
return {
status: false,
message: 'All blank areas should be filled!'
}
}
}
clearInputs()
return{
status: true,
message: 'Saved successfully !'
}
}
function createInfo(meesage, status){
const info = document.createElement('p')
info.textContent = meesage
info.className = 'bilgi text-center'
info.classList.add(status ? 'text-success' : 'text-danger')
const blank = document.createElement('br')
form.insertAdjacentElement('afterend', blank)
blank.insertAdjacentElement('afterend', info)
setTimeout(function(){
const infoWillBeDeleted = document.querySelector('.bilgi')
if(infoWillBeDeleted){
infoWillBeDeleted.remove()
blank.remove()
}
},2000)
}
function clearInputs(){
isim.value = ''
soyIsim.value = ''
phoneNumber.value = ''
eMail.value = ''
}
function doPersonOperations(event){
if(event.target.classList.contains('fa-trash-can')){
const TrElementWilBeDeleted = event.target.parentElement.parentElement.parentElement
const mailWillBeDeleted = event.target.parentElement.parentElement.previousElementSibling.textContent
deletePerson(TrElementWilBeDeleted, mailWillBeDeleted)
}
else if(event.target.classList.contains('fa-pen-to-square')){
saveButton.textContent = "Edit"
const chosenTR = event.target.parentElement.parentElement.parentElement
const mailWillBeEdited = chosenTR.cells[2].textContent
isim.value = chosenTR.cells[0].textContent
soyIsim.value = chosenTR.cells[1].textContent
phoneNumber.value = chosenTR.cells[2].textContent
eMail.value = chosenTR.cells[3].textContent
chosenRow = chosenTR;
//editPerson()
}
}
function deletePerson(TRElementWillBeRemoved, mailWillBeDeleted){
allPeopleArray.forEach((person, index) => {
if(person.mail === mailWillBeDeleted){
allPeopleArray.splice(index, 1)
}
});
TRElementWillBeRemoved.remove()
console.log(allPeopleArray);
clearInputs()
/*
2nd way
const stayingPeopleList = allPeopleArray.filter((person, index) => {
return person.mail !== mailWillBeDeleted
})
allPeopleArray.length = 0;
allPeopleArray.push(...stayingPeopleList) */
}
function editPerson(person){
for(let i = 0; i< allPeopleArray.length; i++){
if(allPeopleArray[i].mail === chosenRow.cells[3].textContent){
allPeopleArray[i] = person;
break;
}
}
chosenRow.cells[0].textContent = person.ad
chosenRow.cells[1].textContent = person.soyad
chosenRow.cells[2].textContent = person.phone
chosenRow.cells[3].textContent = person.mail
saveButton.textContent = "Save"
chosenRow = undefined;
}