-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (91 loc) · 3.05 KB
/
app.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
'use strict';
//DOMS of the project
const balance = document.getElementById('total');
const income = document.getElementById('inc_price');
const expense = document.getElementById('exp_price');
const list = document.querySelector('.list');
const form = document.getElementById('form');
const text = document.getElementById('text');
const number = document.getElementById('number');
//submitted data
// const transactions = [
// {'id':1, 'text':'Flower', 'amount':200},
// {'id':2, 'text':'Udemy', 'amount': -300},
// {'id':3, 'text':'Youtube', 'amount': 400}
// ];
const localstorage = JSON.parse(localStorage.getItem('transaction'));
let newTransaction= localStorage.getItem('transaction') !==null?localstorage:[];
// let newTransaction = transactions
//Enter Text through input form to dom
function addTransactionFromInput(e){
e.preventDefault();
if (text.value.trim() =="" || number.value.trim() =="") {
alert('Field(s) is(are) empty. ')
}else{
const inputValues = {
'id':generateId(),
'text':text.value,
'amount':+number.value
}
text.textContent ='';
number.textContent ='';
newTransaction.push(inputValues);
addTransactionToDom(inputValues);
updateDom()
updatelocalStorage()
text.value = '';
number.value = '';
}
}
//Add to history dom
function addTransactionToDom(transaction){
const sign = transaction.amount < 0 ?'-':'+';
const listItem = document.createElement('li');
listItem.classList.add(transaction.amount < 0 ? 'rightBorderExp':'rightBorderInc');
listItem.innerHTML = `
${transaction.text}<span id="list_price">${sign}${Math.abs(transaction.amount)}</span><button class="delete_btn" onClick='Delete(${transaction.id});'>x</button></li>
`
list.appendChild(listItem);
}
//getting to balance, income and expense
function updateDom(){
const amount = newTransaction.map(item => item.amount);
//balance value
const total = amount.reduce((acc,item)=> {
return acc+=item;
},0).toFixed(2);
//income value
const income_value = amount.filter(item => item > 0).reduce((acc, item) =>{
return acc+=item
},0).toFixed(2);
//expense value
const expense_values = (amount.filter(item=> item < 0).reduce((acc, item)=>{
return acc+=item;
},0)*-1).toFixed(2);
//adding values to DOMS
balance.textContent = `$${total }`;
income.textContent = `$${income_value}`;
expense.textContent = `$${expense_values}`;
}
function Delete(id){
newTransaction = newTransaction.filter(item =>item.id !==id);
updatelocalStorage()
init();
}
//local storage
function updatelocalStorage(){
localStorage.setItem('transaction', JSON.stringify(newTransaction))
}
//generate random id
function generateId(){
return Math.floor(Math.random()* 100000);
updatelocalStorage()
}
//init
function init(){
list.innerHTML ='';
newTransaction.forEach(addTransactionToDom);
updateDom()
}
init();
form.addEventListener('submit', addTransactionFromInput);