-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
105 lines (88 loc) · 3.79 KB
/
script.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
let totalAmount = document.getElementById('total-amount');
let userAmount = document.getElementById('user-amount');
const totalAmountButton = document.getElementById('total-amount-button');
const checkAmountButton = document.getElementById('check-amount');
const productTitle = document.getElementById('product-title');
const errorMessage = document.getElementById('budget-error');
const productTitleError = document.getElementById('product-title-error');
const productCostError = document.getElementById('product-cost-error');
const expenditureValue = document.getElementById('expenditure-value');
const balanceValue = document.getElementById('balance-amount');
const list = document.getElementById('list');
let tempAmount =0
const localStorageProductTitle = JSON.parse(
localStorage.getItem('product-title')
);
let transactions =
localStorage.getItem('product-title') !== null ? localStorageTransactions : [];
console.log(productTitle)
totalAmountButton.addEventListener("click", () => {
tempAmount = totalAmount.value;
if (tempAmount === "" || tempAmount < 0) {
errorMessage.classList.remove("hide");
} else {
errorMessage.classList.add("hide");
amount.innerHTML = tempAmount;
balanceValue.innerText = tempAmount - expenditureValue.innerText;
totalAmount.value = "";
}
});
const disableButtons = (bool) =>{
let editButtons = document.getElementsByClassName('edit')
Array.from(editButtons).forEach((element) => {
element.disabled = bool
});
};
const modifyElement = (element, edit = false) => {
let parentDiv = element.parentElement
let currentBalance = balanceValue.innerText
let currentExpense = expenditureValue.innerText
let parentAmount = parentDiv.querySelector(".amount").innerText;
if (edit) {
let parentText = parentDiv.querySelector(".product").innerText
productTitle.value = parentText
userAmount.value = parentAmount
disableButtons(true)
}
balanceValue.innerText = parseInt(currentBalance) + parseInt(parentAmount)
expenditureValue.innerText =
parseInt(currentExpense) - parseInt(parentAmount)
parentDiv.remove()
}
const listCreator = (expenseName, expenseValue) => {
let sublistContent = document.createElement('div')
sublistContent.classList.add("subtract", "flex-space")
list.appendChild(sublistContent)
sublistContent.innerHTML = `<p class="product">${expenseName}</p> <p class="amount">${expenseValue}</p>`
let editButton = document.createElement("button")
editButton.classList.add("fa-solid", "fa-pen-to-square", "edit")
editButton.style.fontSize = "1.2em"
editButton.addEventListener ("click", () => {
modifyElement(editButton, true)
})
let deleteButton = document.createElement("button")
deleteButton.classList.add("fa-solid", "fa-trash-can", "delete")
deleteButton.style.fontSize = "1.2em"
deleteButton.addEventListener("click", () => {
modifyElement(deleteButton)
console.log(deleteButton)
})
sublistContent.appendChild(editButton)
sublistContent.appendChild(deleteButton)
document.getElementById("list").appendChild(sublistContent)
}
checkAmountButton.addEventListener("click", () =>{
if (!userAmount.value || !productTitle.value) {
productTitleError.classList.remove("hide")
return false
}
disableButtons(false)
let expenditure = parseInt(userAmount.value)
let sum = parseInt(expenditureValue.innerText) + expenditure
expenditureValue.innerText = sum
const totalBalance = tempAmount - sum
balanceValue.innerText = totalBalance
listCreator(productTitle.value, userAmount.value)
productTitle.value = ""
userAmount.value = ""
})