Skip to content

Commit 3a60732

Browse files
committed
keep moving forward
1 parent b23d7c5 commit 3a60732

File tree

2 files changed

+143
-12
lines changed

2 files changed

+143
-12
lines changed

expenseTracker/script.js

+127-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,69 @@ const amount = document.getElementById('amount');
99

1010

1111

12-
const dummyTransactions = [
13-
{ id: 1, text: 'Flower', amount: -20 },
14-
{ id: 2, text: 'Salary', amount: 300 },
15-
{ id: 3, text: 'Book', amount: -10 },
16-
{ id: 4, text: 'Camera', amount: 150 }
17-
]
12+
const localStorageTransactions = JSON.parse(localStorage.getItem('transactions'))
13+
14+
15+
16+
// const dummyTransactions = [
17+
// { id: 1, text: 'Flower', amount: -20 },
18+
// { id: 2, text: 'Salary', amount: 300 },
19+
// { id: 3, text: 'Book', amount: -10 },
20+
// { id: 4, text: 'Camera', amount: 150 }
21+
// ]
22+
23+
let transactions = localStorage.getItem('transactions') !== null ? localStorageTransactions : []
24+
25+
26+
27+
// add transaction
28+
29+
30+
31+
32+
function addTransaction(e) {
33+
e.preventDefault()
34+
35+
if (text.value.trim() === '' || amount.value.trim() === '') {
36+
alert('Please add text and amount')
37+
38+
}
39+
40+
else {
41+
const transaction = {
42+
43+
id: generateId(),
44+
text: text.value,
45+
amount: Number(amount.value)
46+
}
47+
48+
49+
transactions.push(transaction)
50+
51+
addTransactionDOM(transaction)
52+
53+
updateValues()
54+
55+
updateLocalStorage()
56+
57+
text.value = '';
58+
59+
amount.value = ''
60+
}
61+
62+
63+
64+
65+
}
66+
67+
// generate random id
68+
69+
function generateId() {
70+
return Math.floor(Math.random() * 1000000000)
71+
}
72+
1873

1974

20-
let transactions = dummyTransactions
2175

2276
// Add transactions to DOM list
2377

@@ -29,29 +83,92 @@ function addTransactionDOM(transaction) {
2983

3084

3185
const item = document.createElement('li')
86+
const button = document.createElement('btn');
87+
88+
89+
button.classList.add('delete-btn')
90+
button.innerHTML = 'X'
91+
// <button class="delete-btn" onClick="removeTransaction(${transaction.id})> X </button>
92+
93+
button.addEventListener('click', function () {
94+
removeTransaction(transaction.id);
95+
});
3296

3397

3498
// add class based on value
3599
item.classList.add(transaction.amount < 0 ? 'minus' : 'plus')
36100

37101

38-
item.innerHTML = `${transaction.text} <span>${sign} ${Math.abs(transaction.amount)}</span> <button class="delete-btn"> X </button>`
39-
102+
item.innerHTML = `${transaction.text} <span>${sign} ${Math.abs(transaction.amount)}</span>`
103+
item.appendChild(button)
40104

41105

42106

43107
list.appendChild(item);
44108

109+
110+
}
111+
112+
113+
// update the balance and expence
114+
115+
116+
117+
function updateValues() {
118+
const amounts = transactions.map(transaction => transaction.amount)
119+
120+
const total = amounts.reduce((acc, item) => (acc += item), 0).toFixed(2)
121+
122+
123+
const income = amounts.filter(item => item > 0).reduce((acc, item) => (acc += item), 0).toFixed(2)
124+
125+
126+
const expense = (amounts.filter(item => item < 0).reduce((acc, item) => (acc += item), 0) * -1).toFixed(2)
127+
128+
129+
console.log(total, 'total', income)
130+
console.log(expense)
131+
132+
133+
balance.innerText = `$${total}`;
134+
money_plus.innerText = `$${income}`
135+
money_plus.classList.add('plus')
136+
money_minus.innerText = `$${expense}`
137+
138+
}
139+
140+
141+
// removes transaticon by id
142+
143+
144+
function removeTransaction(id) {
145+
console.log('hello')
146+
transactions = transactions.filter(trans => trans.id !== id)
147+
148+
updateLocalStorage()
149+
init()
45150
}
46151

47152

48153
// init app
49154

155+
156+
157+
function updateLocalStorage() {
158+
localStorage.setItem('transactions', JSON.stringify(transactions))
159+
}
160+
161+
162+
50163
function init() {
51164
list.innerHTML = ''
52165

53166
transactions.forEach(addTransactionDOM)
167+
updateValues()
54168
}
55169

56170

57-
init()
171+
init()
172+
173+
174+
form.addEventListener('submit', addTransaction)

expenseTracker/style.css

+16-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ h4 {
8181
color: #2ecc71;
8282
}
8383

84+
85+
.plus {
86+
color: #2ecc71;
87+
}
88+
8489
.money.minus {
8590
color: #c0392b;
8691
}
@@ -110,7 +115,8 @@ input[type='text'].input[type='number'] {
110115
color: #fff;
111116
border: 0;
112117
display: block;
113-
cursor: pointer font-size: 16px;
118+
cursor: pointer;
119+
font-size: 16px;
114120
margin: 10px 0 30px;
115121
padding: 10px;
116122
width: 100%;
@@ -119,7 +125,7 @@ input[type='text'].input[type='number'] {
119125

120126
.list {
121127
list-style-type: none;
122-
padding: 0;
128+
padding: 1rem;
123129
margin-bottom: 40px;
124130
}
125131

@@ -129,6 +135,7 @@ input[type='text'].input[type='number'] {
129135
box-shadow: var(--box-shadow);
130136
color: #333;
131137
display: flex;
138+
padding: 1rem;
132139
justify-content: space-between;
133140
position: relative;
134141
margin: 10px 0;
@@ -140,11 +147,16 @@ input[type='text'].input[type='number'] {
140147
}
141148

142149

150+
143151
.list li.minus {
144152
border-right: 5px solid #c0392b;
145153
}
146154

147155

156+
li:hover {
157+
cursor: pointer;
158+
}
159+
148160
.delete-btn {
149161
cursor: pointer;
150162
background-color: #e74c3c;
@@ -164,10 +176,12 @@ input[type='text'].input[type='number'] {
164176

165177
.list li:hover .delete-btn {
166178
opacity: 1;
179+
cursor: pointer;
167180
}
168181

169182

170183

184+
171185
.btn:focus,
172186
.delete-btn:focus {
173187
outline: 0;

0 commit comments

Comments
 (0)