@@ -13,7 +13,7 @@ function createTransactionElement(text, amount) {
1313 transactionTextElement . classList . add ( 'transaction-text' ) ;
1414
1515 let transactionAmountElement = document . createElement ( 'h5' ) ;
16- transactionAmountElement . textContent = `${ amount < 0 ? '' : '+' } ${ amount } ` ;
16+ transactionAmountElement . textContent = `${ amount < 0 ? '' : '+' } ${ amount } ` ;
1717 transactionAmountElement . classList . add ( 'transaction-amount' ) ;
1818
1919 element . appendChild ( transactionTextElement ) ;
@@ -42,7 +42,7 @@ function addTransaction(event) {
4242
4343 let transactionText = transactionTextInput . value ;
4444 let transactionElement = createTransactionElement ( transactionText , transactionAmount ) ;
45-
45+
4646 document . querySelector ( '.history-transactions-container' ) . appendChild ( transactionElement ) ;
4747
4848 balance += transactionAmount ;
@@ -58,7 +58,53 @@ function addTransaction(event) {
5858 incomeSpan . textContent = `${ income . toFixed ( 2 ) } ` ;
5959 transactionElement . classList . add ( 'income' ) ;
6060 }
61- transactionTextInput . value = transactionAmountInput . value = '' ;
61+ transactionTextInput . value = transactionAmountInput . value = '' ;
62+ updatePersistentData ( transactionText , transactionAmount ) ;
63+ }
64+
65+ async function updatePersistentData ( newText , newAmount ) {
66+ let transactions = [ ] ;
67+ const texts = document . querySelectorAll ( '.transaction-element .transaction-text' ) ;
68+ const amounts = document . querySelectorAll ( '.transaction-element .transaction-amount' ) ;
69+
70+ for ( let i = 0 ; i < texts . length ; i ++ ) {
71+ transactions . push ( {
72+ text : texts [ i ] . textContent ,
73+ amount : amounts [ i ] . textContent . replace ( '+' , '' )
74+ } )
75+ }
76+
77+ localStorage . setItem ( 'data' , JSON . stringify ( transactions ) ) ;
78+ }
79+
80+ async function loadPersistentData ( ) {
81+ const data = JSON . parse ( localStorage . getItem ( 'data' ) ) ;
82+ if ( ! data ) return
83+
84+ const incomeSpan = document . querySelector ( '.incomeValue' ) ;
85+ const expenseSpan = document . querySelector ( '.expenseValue' ) ;
86+ const balanceSpan = document . querySelector ( '.balanceValue' ) ;
87+
88+ for ( let i = 0 ; i < data . length ; i ++ ) {
89+ let transactionElement = createTransactionElement ( data [ i ] . text , data [ i ] . amount ) ;
90+ let transactionAmount = Number ( data [ i ] . amount ) ;
91+
92+ balance += transactionAmount ;
93+ balanceSpan . textContent = `$ ${ balance . toFixed ( 2 ) } ` ;
94+
95+ if ( transactionAmount < 0 ) {
96+ expense -= transactionAmount ;
97+ expenseSpan . textContent = `${ expense . toFixed ( 2 ) } ` ;
98+ transactionElement . classList . add ( 'expense' ) ;
99+ }
100+ else {
101+ income += transactionAmount ;
102+ incomeSpan . textContent = `${ income . toFixed ( 2 ) } ` ;
103+ transactionElement . classList . add ( 'income' ) ;
104+ }
105+
106+ document . querySelector ( '.history-transactions-container' ) . appendChild ( transactionElement ) ;
107+ }
62108}
63109
64110function main ( ) {
@@ -68,6 +114,8 @@ function main() {
68114
69115 const transactionButton = document . querySelector ( 'button' ) ;
70116 transactionButton . addEventListener ( 'click' , addTransaction ) ;
117+
118+ loadPersistentData ( ) ;
71119}
72120
73121window . onload = main ;
0 commit comments