-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (102 loc) · 3.71 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
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
131
132
133
134
const state = {
earnings: 0,
expense: 0,
net: 0,
transactions: [
],
};
let isUpdate=false;
let tid
const transactionFormEl = document.getElementById("transactionForm");
const renderTransactions = () => {
const transactionContainerEl = document.querySelector(".transactions");
const netAmountEl = document.getElementById("netAmount");
const earnEl = document.getElementById("earning");
const expenseEl = document.getElementById("expensee");
let earning = 0;
let expense = 0;
let net = 0;
transactionContainerEl.innerHTML = "";
state.transactions.forEach((transaction) => {
const { id, amount, text, type } = transaction;
const isCredit = type === "credit" ? true :false;
const sign = isCredit ? "+" : "-";
const transactionEl = `
<div class="indiv" id="${id}" onclick="showEdit(${id})">
<div class="content" >
<div class="left">
<p>${text}</p>
<p>Rs${sign}${amount}</p>
</div>
</div>
<div class="status ${isCredit ? "credit" : "debit"}">
${isCredit ? "C" : "D"}
</div>
<div class="lower">
<div class="icon" onclick="handleUpdate(${id})">
<img src="./icons/pen.svg" alt="pen"/>
</div>
<div class="icon" onclick="handleDelete(${id})">
<img src="./icons/trash.svg" alt="trash"/>
</div>
</div>
</div>
`;
earning += isCredit ? amount : 0;
expense += !isCredit ? amount : 0;
net = earning - expense;
transactionContainerEl.insertAdjacentHTML("afterbegin", transactionEl);
});
netAmountEl.innerHTML = `Rs${net}`;
earnEl.innerHTML = `Rs${earning}`;
expenseEl.innerHTML = `Rs${expense}`;
};
const addTransaction = (e) => {
e.preventDefault();
const isEarn = e.submitter.id === "earn" ? true :false;
const formData = new FormData(transactionFormEl);
const tData = {};
formData.forEach((value,key)=>{
tData[key]=value;
});
const { text, amount } = tData;
const transaction = {
id: isUpdate ? tid: Math.floor(Math.random() * 1000),
text: text,
amount: +amount,
type: isEarn ? "credit" : "debit",
};
if(isUpdate){
const tIndex=state.transactions.findIndex((t)=>t.id===tid);
state.transactions[tIndex]=transaction;
isUpdate=false;
tid=null;
}else{
state.transactions.push(transaction);
}
renderTransactions();
transactionFormEl.reset();
console.log({ tData });
};
const showEdit = (id) => {
const selectedTransaction = document.getElementById(id);
const lowerEl = selectedTransaction.querySelector(".lower");
lowerEl.classList.toggle("showTransaction");
};
const handleUpdate = (id) =>{
const transaction = state.transactions.find((t)=> t.id ===id);
const {text,amount}=transaction;
const textInput= document.getElementById("text");
const amountInput=document.getElementById("Amount");
textInput.value=text;
amountInput.value=amount;
tid=id;
isUpdate=true;
}
const handleDelete = (id) =>{
const filteredTransaction = state.transactions.filter(t=> t.id !== id);
state.transactions = filteredTransaction;
renderTransactions();
}
renderTransactions();
transactionFormEl.addEventListener("submit", addTransaction);