|
| 1 | +const budgetInput = document.querySelector(".budget-box input"), |
| 2 | + setBudgetBtn = document.querySelector(".budget-box button"), |
| 3 | + totalBudgetResult = document.getElementById("total-budget"), |
| 4 | + totalExpenseResult = document.getElementById("total-expense"), |
| 5 | + totalBalanceResult = document.getElementById("total-balance"), |
| 6 | + productTitleInput = document.querySelector(".expense-box").children[1], |
| 7 | + productCostInput = document.querySelector(".expense-box").children[2], |
| 8 | + addProductBtn = document.querySelector(".expense-box button"), |
| 9 | + expenseListWrapper = document.querySelector(".expense-list-wrapper"); |
| 10 | + |
| 11 | +let totalBudgetAmount = 0, |
| 12 | + totalExpenseAmount = 0, |
| 13 | + totalBalanceAmount = 0; |
| 14 | + |
| 15 | +// Update HTML content with the current values |
| 16 | +const updateHTML = () => { |
| 17 | + totalBudgetResult.textContent = totalBudgetAmount; |
| 18 | + totalExpenseResult.textContent = totalExpenseAmount; |
| 19 | + totalBalanceResult.textContent = totalBalanceAmount; |
| 20 | +}; |
| 21 | + |
| 22 | +// Set the budget amount and update the results |
| 23 | +const setBudget = () => { |
| 24 | + let budgetAmount = parseInt(budgetInput.value); |
| 25 | + |
| 26 | + if (budgetAmount <= 0 || isNaN(budgetAmount)) |
| 27 | + return alert("Enter a valid positive budget value"); |
| 28 | + |
| 29 | + totalBudgetAmount += budgetAmount; |
| 30 | + totalBalanceAmount += budgetAmount; |
| 31 | + |
| 32 | + updateHTML(); |
| 33 | + budgetInput.value = ""; |
| 34 | +}; |
| 35 | + |
| 36 | +// Render the product in the expense list |
| 37 | +const renderProduct = (title, cost) => { |
| 38 | + const listBox = ` |
| 39 | + <div |
| 40 | + class="d-flex align-items-center justify-content-between px-3 py-2 mb-3 list-box" |
| 41 | + > |
| 42 | + <input type="text" value="${title}" class="fw-medium" readonly /> |
| 43 | + <input type="number" value="${cost}" readonly /> |
| 44 | + <div class="d-flex align-items-center text-primary gap-2"> |
| 45 | + <i class="ri-edit-box-line"></i> |
| 46 | + <i class="ri-delete-back-line"></i> |
| 47 | + </div> |
| 48 | + </div>`; |
| 49 | + |
| 50 | + expenseListWrapper.insertAdjacentHTML("beforeend", listBox); |
| 51 | +}; |
| 52 | + |
| 53 | +// Add product to the expense list and update amounts |
| 54 | +const addProduct = () => { |
| 55 | + let productTitle = productTitleInput.value, |
| 56 | + productCost = parseInt(productCostInput.value); |
| 57 | + |
| 58 | + if (!productTitle) return alert("Enter product title"); |
| 59 | + if (productCost <= 0 || isNaN(productCost)) |
| 60 | + return alert("Enter a valid positive product cost"); |
| 61 | + if (productCost > totalBalanceAmount) return alert("Not enough balance!"); |
| 62 | + |
| 63 | + renderProduct(productTitle, productCost); |
| 64 | + |
| 65 | + totalExpenseAmount += productCost; |
| 66 | + totalBalanceAmount -= productCost; |
| 67 | + |
| 68 | + updateHTML(); |
| 69 | + |
| 70 | + productTitleInput.value = ""; |
| 71 | + productCostInput.value = ""; |
| 72 | +}; |
| 73 | + |
| 74 | +// Handle edit and delete actions for expenses |
| 75 | +expenseListWrapper.addEventListener("click", (e) => { |
| 76 | + const editBtn = e.target.closest(".ri-edit-box-line"); |
| 77 | + const deleteBtn = e.target.closest(".ri-delete-back-line"); |
| 78 | + const currentListBox = e.target.closest(".list-box"); |
| 79 | + const currentTitle = currentListBox?.querySelector("input[type='text']"); |
| 80 | + const currentCost = currentListBox?.querySelector("input[type='number']"); |
| 81 | + |
| 82 | + if (editBtn) { |
| 83 | + if (!currentTitle.value || !currentCost.value) |
| 84 | + return alert("Value can't be empty!"); |
| 85 | + |
| 86 | + // Toggle editable state and save changes |
| 87 | + currentTitle.toggleAttribute("readonly"); |
| 88 | + currentCost.toggleAttribute("readonly"); |
| 89 | + currentTitle.classList.toggle("editable"); |
| 90 | + currentCost.classList.toggle("editable"); |
| 91 | + currentTitle.setAttribute("value", currentTitle.value); |
| 92 | + currentCost.setAttribute("value", currentCost.value); |
| 93 | + e.target.classList.toggle("ri-save-line"); |
| 94 | + |
| 95 | + // Recalculate total expenses and balance |
| 96 | + totalExpenseAmount = [ |
| 97 | + ...expenseListWrapper.querySelectorAll(".list-box"), |
| 98 | + ].reduce( |
| 99 | + (total, box) => |
| 100 | + total + parseInt(box.querySelector("input[type='number']").value), |
| 101 | + 0 |
| 102 | + ); |
| 103 | + totalBalanceAmount = totalBudgetAmount - totalExpenseAmount; |
| 104 | + |
| 105 | + updateHTML(); |
| 106 | + } |
| 107 | + |
| 108 | + if (deleteBtn) { |
| 109 | + // Remove product and adjust total values |
| 110 | + currentListBox.remove(); |
| 111 | + totalExpenseAmount -= parseInt(currentCost.value); |
| 112 | + totalBalanceAmount += parseInt(currentCost.value); |
| 113 | + |
| 114 | + updateHTML(); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +// Attach event listeners to buttons |
| 119 | +setBudgetBtn.addEventListener("click", setBudget); |
| 120 | +addProductBtn.addEventListener("click", addProduct); |
0 commit comments