|
| 1 | +const balance = document.getElementById("balance"); |
| 2 | +const moneyPlus = document.getElementById("money-plus"); |
| 3 | +const moneyMinus = document.getElementById("money-minus"); |
| 4 | +const list = document.getElementById("list"); |
| 5 | +const form = document.getElementById("form"); |
| 6 | +const text = document.getElementById("text"); |
| 7 | +const amount = document.getElementById("amount"); |
| 8 | +const notification = document.getElementById("notification"); |
| 9 | + |
| 10 | +// const dummyTransactions = [ |
| 11 | +// { id: 1, text: "Flower", amount: -20 }, |
| 12 | +// { id: 2, text: "Salary", amount: 300 }, |
| 13 | +// { id: 3, text: "Book", amount: -10 }, |
| 14 | +// { id: 4, text: "Camera", amount: 150 }, |
| 15 | +// ]; |
| 16 | + |
| 17 | +// let transactions = dummyTransactions; |
| 18 | + |
| 19 | +const localStorageTransactions = JSON.parse( |
| 20 | + localStorage.getItem("transactions") |
| 21 | +); |
| 22 | +let transactions = |
| 23 | + localStorageTransactions !== null ? localStorageTransactions : []; |
| 24 | + |
| 25 | +function updateLocaleStorage() { |
| 26 | + localStorage.setItem("transactions", JSON.stringify(transactions)); |
| 27 | +} |
| 28 | + |
| 29 | +function showNotification() { |
| 30 | + notification.classList.add("show"); |
| 31 | + setTimeout(() => { |
| 32 | + notification.classList.remove("show"); |
| 33 | + }, 2000); |
| 34 | +} |
| 35 | + |
| 36 | +function generateID() { |
| 37 | + return Math.floor(Math.random() * 100000000); |
| 38 | +} |
| 39 | + |
| 40 | +function addTransaction(e) { |
| 41 | + e.preventDefault(); |
| 42 | + if (text.value.trim() === "" || amount.value.trim() === "") { |
| 43 | + showNotification(); |
| 44 | + } else { |
| 45 | + const transaction = { |
| 46 | + id: generateID(), |
| 47 | + text: text.value, |
| 48 | + amount: +amount.value, |
| 49 | + }; |
| 50 | + transactions.push(transaction); |
| 51 | + addTransactionDOM(transaction); |
| 52 | + updateValues(); |
| 53 | + updateLocaleStorage(); |
| 54 | + text.value = ""; |
| 55 | + amount.value = ""; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function addTransactionDOM(transaction) { |
| 60 | + const sign = transaction.amount < 0 ? "-" : "+"; |
| 61 | + const item = document.createElement("li"); |
| 62 | + item.classList.add(sign === "+" ? "plus" : "minus"); |
| 63 | + item.innerHTML = ` |
| 64 | + ${transaction.text} <span>${sign}${Math.abs(transaction.amount)}</span |
| 65 | + ><button class="delete-btn" onclick="removeTransaction(${ |
| 66 | + transaction.id |
| 67 | + })"><i class="fa fa-times"></i></button> |
| 68 | + `; |
| 69 | + list.appendChild(item); |
| 70 | +} |
| 71 | + |
| 72 | +function updateValues() { |
| 73 | + const amounts = transactions.map((transaction) => transaction.amount); |
| 74 | + const total = amounts |
| 75 | + .reduce((accumulator, value) => (accumulator += value), 0) |
| 76 | + .toFixed(2); |
| 77 | + const income = amounts |
| 78 | + .filter((value) => value > 0) |
| 79 | + .reduce((accumulator, value) => (accumulator += value), 0) |
| 80 | + .toFixed(2); |
| 81 | + const expense = ( |
| 82 | + amounts |
| 83 | + .filter((value) => value < 0) |
| 84 | + .reduce((accumulator, value) => (accumulator += value), 0) * -1 |
| 85 | + ).toFixed(2); |
| 86 | + balance.innerText = `$${total}`; |
| 87 | + moneyPlus.innerText = `$${income}`; |
| 88 | + moneyMinus.innerText = `$${expense}`; |
| 89 | +} |
| 90 | + |
| 91 | +function removeTransaction(id) { |
| 92 | + transactions = transactions.filter((transaction) => transaction.id !== id); |
| 93 | + updateLocaleStorage(); |
| 94 | + init(); |
| 95 | +} |
| 96 | + |
| 97 | +// Init |
| 98 | +function init() { |
| 99 | + list.innerHTML = ""; |
| 100 | + transactions.forEach(addTransactionDOM); |
| 101 | + updateValues(); |
| 102 | +} |
| 103 | + |
| 104 | +init(); |
| 105 | + |
| 106 | +form.addEventListener("submit", addTransaction); |
0 commit comments