-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
178 lines (127 loc) · 4.94 KB
/
js.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
//? Selectors
const ekleBtn = document.getElementById("ekle-btn")
const gelirInput = document.getElementById("gelir-input")
const ekleFormu = document.getElementById("ekle-formu")
const gelirinizTd = document.getElementById("geliriniz")
const giderinizTd = document.getElementById("gideriniz")
const kalanTd = document.getElementById("kalan")
//& Variables
let gelirler = 0
let harcamaListesi = []
//* Ekle Formu
ekleFormu.addEventListener("submit",(e)=>{
e.preventDefault() //! reload u engellemek için
gelirler = gelirler + Number(gelirInput.value)
ekleFormu.reset()
// console.log(gelirler)
localStorage.setItem("gelirler", gelirler)
hesaplaVeGuncelle()
// gelirinizTd.textContent = gelirler
})
//! Harcama Formu
const harcamaFormu = document.getElementById("harcama-formu")
const tarihInput = document.getElementById("tarih")
const miktarInput = document.getElementById("miktar")
const harcamaAlaniInput = document.getElementById("harcama-alani")
const harcamaBody = document.getElementById("harcama-body")
const temizleBtn = document.getElementById("temizle-btn")
harcamaFormu.addEventListener("submit",(e)=>{
e.preventDefault() //! reload u engellemek için
const yeniHarcama = {
tarih: tarihInput.value,
miktar: miktarInput.value,
alan: harcamaAlaniInput.value,
id: new Date().getTime()
}
// console.log(yeniHarcama)
harcamaListesi.push(yeniHarcama)
// console.log(harcamaListesi)
localStorage.setItem("harcamalar", JSON.stringify(harcamaListesi))//!stringleştiriyor
harcamayiDomaYaz(yeniHarcama)
harcamaFormu.reset()
tarihInput.valueAsDate = new Date()
hesaplaVeGuncelle()
})
//& Harcamayı Dom'a yaz
const harcamayiDomaYaz = ({id, miktar, tarih, alan}) =>{
// const {id, miktar, tarih, alan} = yeniHarcama
// console.log(id, miktar, tarih, alan)
//! 1. yöntem (Eski ve güvenlik açığı nedeniyle kullanılmaması gereken)
// harcamaBody.innerHTML += `
// <tr>
// <td>${tarih}</td>
// <td>${alan}</td>
// <td>${miktar}</td>
// <td><i id=${id} class="fa-solid fa-trash-can text-danger" type="button"></i></td>
// </tr>
// `
//^ innerHTML kullandığımızda aşağıdaki kodları harcama alanına yazarsak istemediğimiz şekilde bir tablo düzeni ile karşılaşırız.
//^ <h2>Hello World!</h2><p>Have a nice day!</p>
//^ <img src="https://5.imimg.com/data5/VK/EK/UG/SELLER-101818061/danger-signs-500x500.jpg">
//! 2. yöntem
const tr = document.createElement("tr")
const appendTd = (content)=>{
const td = document.createElement("td")
td.textContent = content;
return td
}
const createLastTd = () =>{
const td = document.createElement("td")
const iElement = document.createElement("i")
iElement.id = id
iElement.className = "fa-solid fa-trash-can text-danger"
iElement.type = "button"
td.appendChild(iElement)
return td
}
tr.append(
appendTd(tarih), //tarih td si
appendTd(alan), //alan td si
appendTd(miktar), //miktar td si
createLastTd() // Çöp kutusu ve id yi ekler
)
harcamaBody.append(tr) //& son girileni alta ekler
// harcamaBody.prepend(tr) //& son girileni öne ekler
}
//& hesaplaVeGuncelle
const hesaplaVeGuncelle = ()=>{
const giderler = harcamaListesi.reduce(
(toplam, harcama) => toplam + Number(harcama.miktar),0
)
// console.log(giderler)
giderinizTd.textContent = giderler
gelirinizTd.textContent = gelirler
kalanTd.textContent = gelirler - giderler
}
//? Event listeners
window.addEventListener("load", ()=>{
gelirler = Number(localStorage.getItem("gelirler")) || 0 //! Local storage den gelirleri al numberlaştır. Veri yoksa 0 ata
harcamaListesi = JSON.parse(localStorage.getItem("harcamalar")) || []
harcamaListesi.forEach(harcama => {
harcamayiDomaYaz(harcama)
});
gelirinizTd.textContent = gelirler
hesaplaVeGuncelle()
tarihInput.valueAsDate = new Date()
})
harcamaBody.addEventListener("click",(e)=>{
// console.log(e.target.classList.contains("fa-trash-can"))
if (e.target.classList.contains("fa-trash-can")) {
e.target.parentElement.parentElement.remove()
const id = e.target.id
harcamaListesi = harcamaListesi.filter((harcama)=> harcama.id != id)
// console.log(harcamaListesi)
localStorage.setItem("harcamalar", JSON.stringify(harcamaListesi))
}
})
temizleBtn.addEventListener("click",()=>{
if (confirm("Tüm veriler silinecek. Devam etmek istiyor musun?")) {
harcamaListesi = []
gelirler = 0
harcamaBody.innerHTML = ""
// localStorage.clear() // Tüm localstorage i siler
localStorage.removeItem("gelirler")
localStorage.removeItem("harcamalar")
hesaplaVeGuncelle()
}
})