-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (91 loc) · 3.3 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
106
107
108
109
110
111
112
113
const databaseURL = "https://grocery-list-fc939-default-rtdb.firebaseio.com/";
const inputFieldEl = document.getElementById("inputTitle");
const inputAmountEl = document.getElementById("inputAmount");
const inputQuantityEl = document.getElementById("quantity");
const addItemBtnEl = document.getElementById("item-btn");
const listEl = document.getElementById("list");
const totalAmountEl = document.getElementById("totalAmountSect");
// Array to store all subtotal amounts
const itemsArray = [];
let itemId = 0; // Variable to assign unique IDs to each item
addItemBtnEl.addEventListener("click", function (event) {
event.preventDefault();
const itemName = inputFieldEl.value.trim();
const amount = parseFloat(inputAmountEl.value) || 0; // Parse as float or default to 0
const quantity = parseInt(inputQuantityEl.value) || 1; // Parse as integer or default to 1
// Check if all required fields are filled
if (!itemName || isNaN(amount) || isNaN(quantity)) {
alert("Please fill in all required fields.");
return;
}
// Calculate total amount for the item
const subTotalAmount = amount * quantity;
// Add the item to the array
const newItem = {
id: itemId++,
name: itemName,
quantity: quantity,
subTotal: subTotalAmount,
};
itemsArray.push(newItem);
// Update the total amount in the UI
updateTotalAmount();
// Add the item to the list
addItemToList(newItem);
// Clear input fields
inputFieldEl.value = "";
inputAmountEl.value = "";
inputQuantityEl.value = "1";
});
function updateTotalAmount() {
// Calculate the total amount from the itemsArray
const newTotalAmount = itemsArray.reduce(
(acc, item) => acc + item.subTotal,
0
);
// Display the updated total amount in the UI
totalAmountEl.textContent = `₦ ${newTotalAmount.toFixed(2)}`;
}
function addItemToList(newItem) {
// Create a new list item
const listItem = document.createElement("li");
listItem.setAttribute("data-id", newItem.id); // Set the data-id attribute
listItem.innerHTML = `
<span class="descriptionInput">${newItem.name}</span>
<span class="qty">${newItem.quantity}</span>
<span class="subTotal">${newItem.subTotal.toFixed(2)}</span>
<button class="btnDelete">X</button>
`;
// Append the list item to the transaction history list
listEl.appendChild(listItem);
}
listEl.addEventListener("click", function (event) {
if (event.target.classList.contains("btnDelete")) {
const itemId = event.target.closest("li").getAttribute("data-id");
deleteItem(itemId);
}
});
function deleteItem(id) {
console.log("Deleting item with ID:", id);
// Remove the item from the array based on the provided ID
const indexToRemove = itemsArray.findIndex((item) => item.id == id);
console.log("Index to remove:", indexToRemove);
if (indexToRemove !== -1) {
itemsArray.splice(indexToRemove, 1);
}
// Update the total amount in the UI
updateTotalAmount();
// Remove the item from the list
const listItem = document.querySelector(`li[data-id="${id}"]`);
if (listItem) {
listItem.remove();
} else {
console.log("List item not found:", id);
}
}
listEl.addEventListener("click", function (event) {
if (event.target.classList.contains("btnDelete")) {
const itemId = event.target.closest("li").getAttribute("data-id");
deleteItem(itemId);
}
});