-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
106 lines (88 loc) · 2.73 KB
/
storage.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
function saveData() {
localStorage.setItem("udhaarProducts", JSON.stringify(products));
localStorage.setItem("udhaarOrders", JSON.stringify(orders));
localStorage.setItem("udhaarUsers", JSON.stringify(users));
}
function loadData() {
const savedProducts = localStorage.getItem("udhaarProducts");
const savedOrders = localStorage.getItem("udhaarOrders");
const savedUsers = localStorage.getItem("udhaarUsers");
if (savedProducts) products = JSON.parse(savedProducts);
if (savedOrders) orders = JSON.parse(savedOrders);
if (savedUsers) {
users = JSON.parse(savedUsers);
} else {
users = [
{
email: "admin@gmail.com",
password: "admin",
},
];
saveData();
}
}
function editProduct(productId) {
const product = products.find((p) => p.id === productId);
if (!product) {
alert("Product not found!");
return;
}
const newName = prompt("Enter new name:", product.name);
if (newName === null) return; // User clicked Cancel
const newPrice = prompt("Enter new price:", product.price);
if (newPrice === null) return;
const newStock = prompt("Enter new stock:", product.stock);
if (newStock === null) return;
// Update product
product.name = newName;
product.price = parseFloat(newPrice) || product.price;
product.stock = parseInt(newStock) || product.stock;
// Save to localStorage
saveData();
// Refresh the display
displayProducts();
alert("Product updated successfully!");
}
function updateStock(productId) {
const product = products.find((p) => p.id === productId);
if (!product) {
alert("Product not found!");
return;
}
const newStock = prompt("Enter new stock quantity:", product.stock);
if (newStock === null) return; // User clicked Cancel
const stockNum = parseInt(newStock);
if (isNaN(stockNum) || stockNum < 0) {
alert("Please enter a valid stock quantity!");
return;
}
// Update stock
product.stock = stockNum;
// Save to localStorage
saveData();
// Refresh the display
displayProducts();
alert("Stock updated successfully!");
}
function displayProducts() {
const productTable = document.querySelector("#productTable tbody");
if (!productTable) return;
productTable.innerHTML = "";
products.forEach((product) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${product.name}</td>
<td>PKR ${product.price}</td>
<td>${product.stock}</td>
<td>
<button onclick="editProduct(${product.id})" class="edit-btn">Edit</button>
<button onclick="updateStock(${product.id})" class="stock-btn">Update Stock</button>
</td>
`;
productTable.appendChild(row);
});
}
// Initialize data
loadData();
// Display products initially
displayProducts();