-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (64 loc) · 2.08 KB
/
index.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
const state = {
earnings: 0,
expense: 0,
net: 0,
transactions: [
],
};
const transactionFormEl = document.getElementById("transactionForm");
const renderTransactions = () => {
const transactionContainerEl = document.querySelector(".transactions");
const netAmountEl = document.getElementById("netAmount");
const earningEl = document.getElementById("earning");
const expenseEl = document.getElementById("expense");
const transactions = state.transactions;
let earning = 0;
let expense = 0;
let net = 0;
transactionContainerEl.innerHTML = "";
transactions.forEach((transaction) => {
const { id, amount, text, type } = transaction;
const isCredit = type === "credit" ? true : false;
const sign = isCredit ? "+" : "-";
const transactionEl = `
<div class="transaction" id="${id}">
<div class="left">
<p>${text}</p>
<p>${sign} ₹ ${amount}</p>
</div>
<div class="status ${isCredit ? "credit" : "debit"}">${
isCredit ? "C" : "D"
}</div>
</div>`;
earning += isCredit ? amount : 0;
expense += !isCredit ? amount : 0;
net = earning - expense;
transactionContainerEl.insertAdjacentHTML("afterbegin", transactionEl);
});
console.log({ net, earning, expense });
netAmountEl.innerHTML = `₹ ${net}`;
earningEl.innerHTML = `₹ ${earning}`;
expenseEl.innerHTML = `₹ ${expense}`;
};
const addTransaction = (e) => {
e.preventDefault();
const isEarn = e.submitter.id === "earnBtn" ? true : false;
const formData = new FormData(transactionFormEl);
const tData = {};
formData.forEach((value, key) => {
tData[key] = value;
});
const { text, amount } = tData;
const transaction = {
id: Math.floor(Math.random() * 1000),
text: text,
amount: +amount,
type: isEarn ? "credit" : "debit",
};
state.transactions.push(transaction);
renderTransactions();
transactionFormEl.reset();
console.log({ state });
};
renderTransactions();
transactionFormEl.addEventListener("submit", addTransaction);