@@ -9,15 +9,69 @@ const amount = document.getElementById('amount');
9
9
10
10
11
11
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
+
18
73
19
74
20
- let transactions = dummyTransactions
21
75
22
76
// Add transactions to DOM list
23
77
@@ -29,29 +83,92 @@ function addTransactionDOM(transaction) {
29
83
30
84
31
85
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
+ } ) ;
32
96
33
97
34
98
// add class based on value
35
99
item . classList . add ( transaction . amount < 0 ? 'minus' : 'plus' )
36
100
37
101
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 )
40
104
41
105
42
106
43
107
list . appendChild ( item ) ;
44
108
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 ( )
45
150
}
46
151
47
152
48
153
// init app
49
154
155
+
156
+
157
+ function updateLocalStorage ( ) {
158
+ localStorage . setItem ( 'transactions' , JSON . stringify ( transactions ) )
159
+ }
160
+
161
+
162
+
50
163
function init ( ) {
51
164
list . innerHTML = ''
52
165
53
166
transactions . forEach ( addTransactionDOM )
167
+ updateValues ( )
54
168
}
55
169
56
170
57
- init ( )
171
+ init ( )
172
+
173
+
174
+ form . addEventListener ( 'submit' , addTransaction )
0 commit comments