Spaces:
Sleeping
Sleeping
File size: 14,515 Bytes
a40e4c4 |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
class QuotationDataManager {
constructor() {
this.keys = {
company: "companyDetails",
bank: "bankDetails",
customer: "customerDetails",
};
}
// Save company details to localStorage
saveCompanyDetails(formData) {
const companyData = {
name: formData["company-name"] || "",
address: formData["company-address"] || "",
phone: formData["company-phone"] || "",
email: formData["company-email"] || "",
gstin: formData["company-gstin"] || "",
};
try {
localStorage.setItem(
this.keys.company,
JSON.stringify(companyData),
);
} catch (error) {
console.error("Error saving company details:", error);
}
}
// Save bank details to localStorage
saveBankDetails(formData) {
const bankData = {
name: formData["bank-name"] || "",
account: formData["bank-account"] || "",
ifsc: formData["bank-ifsc"] || "",
branch: formData["bank-branch"] || "",
};
try {
localStorage.setItem(this.keys.bank, JSON.stringify(bankData));
} catch (error) {
console.error("Error saving bank details:", error);
}
}
// Save customer details to sessionStorage (temporary)
saveCustomerDetails(formData) {
const customerData = {
name: formData["customer-name"] || "",
address: formData["customer-address"] || "",
phone: formData["customer-phone"] || "",
email: formData["customer-email"] || "",
gstin: formData["customer-gstin"] || "",
};
try {
sessionStorage.setItem(
this.keys.customer,
JSON.stringify(customerData),
);
} catch (error) {
console.error("Error saving customer details:", error);
}
}
// Load company details from localStorage
loadCompanyDetails() {
try {
const data = localStorage.getItem(this.keys.company);
return data ? JSON.parse(data) : null;
} catch (error) {
console.error("Error loading company details:", error);
return null;
}
}
// Load bank details from localStorage
loadBankDetails() {
try {
const data = localStorage.getItem(this.keys.bank);
return data ? JSON.parse(data) : null;
} catch (error) {
console.error("Error loading bank details:", error);
return null;
}
}
// Load customer details from sessionStorage
loadCustomerDetails() {
try {
const data = sessionStorage.getItem(this.keys.customer);
return data ? JSON.parse(data) : null;
} catch (error) {
console.error("Error loading customer details:", error);
return null;
}
}
// Clear all stored data
clearAllData() {
try {
localStorage.removeItem(this.keys.company);
localStorage.removeItem(this.keys.bank);
sessionStorage.removeItem(this.keys.customer);
} catch (error) {
console.error("Error clearing data:", error);
}
}
// Clear only customer data
clearCustomerData() {
try {
sessionStorage.removeItem(this.keys.customer);
} catch (error) {
console.error("Error clearing customer data:", error);
}
}
// Load all saved form data into the form
loadFormData() {
// Load company details
const companyData = this.loadCompanyDetails();
if (companyData) {
document.getElementById("company-name").value =
companyData.name || "";
document.getElementById("company-address").value =
companyData.address || "";
document.getElementById("company-phone").value =
companyData.phone || "";
document.getElementById("company-email").value =
companyData.email || "";
document.getElementById("company-gstin").value =
companyData.gstin || "";
}
// Load bank details
const bankData = this.loadBankDetails();
if (bankData) {
document.getElementById("bank-name").value = bankData.name || "";
document.getElementById("bank-account").value =
bankData.account || "";
document.getElementById("bank-ifsc").value = bankData.ifsc || "";
document.getElementById("bank-branch").value =
bankData.branch || "";
}
// Load customer details
const customerData = this.loadCustomerDetails();
if (customerData) {
document.getElementById("customer-name").value =
customerData.name || "";
document.getElementById("customer-address").value =
customerData.address || "";
document.getElementById("customer-phone").value =
customerData.phone || "";
document.getElementById("customer-email").value =
customerData.email || "";
document.getElementById("customer-gstin").value =
customerData.gstin || "";
}
}
// Get form data as object
getFormData() {
const formData = new FormData(
document.getElementById("quotation-form"),
);
const data = {};
for (let [key, value] of formData.entries()) {
data[key] = value;
}
return data;
}
// Setup auto-save functionality
setupAutoSave() {
// Company fields auto-save
const companyFields = [
"company-name",
"company-address",
"company-phone",
"company-email",
"company-gstin",
];
companyFields.forEach((fieldId) => {
const field = document.getElementById(fieldId);
if (field) {
field.addEventListener("blur", () => {
const formData = this.getFormData();
this.saveCompanyDetails(formData);
this.updateStorageStatus();
});
}
});
// Bank fields auto-save
const bankFields = [
"bank-name",
"bank-account",
"bank-ifsc",
"bank-branch",
];
bankFields.forEach((fieldId) => {
const field = document.getElementById(fieldId);
if (field) {
field.addEventListener("blur", () => {
const formData = this.getFormData();
this.saveBankDetails(formData);
this.updateStorageStatus();
});
}
});
// Customer fields auto-save (to sessionStorage)
const customerFields = [
"customer-name",
"customer-address",
"customer-phone",
"customer-email",
"customer-gstin",
];
customerFields.forEach((fieldId) => {
const field = document.getElementById(fieldId);
if (field) {
field.addEventListener("blur", () => {
const formData = this.getFormData();
this.saveCustomerDetails(formData);
this.updateStorageStatus();
});
}
});
}
// Check if localStorage is available
isLocalStorageAvailable() {
try {
const test = "__localStorage_test__";
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch (_error) {
return false;
}
}
// Show status modal
showModal(message, type = "info") {
const modal = document.getElementById("status-modal");
const modalMessage = document.getElementById("modal-message");
modalMessage.innerHTML = `<p class="text-${type === "error" ? "red" : "green"}-600">${message}</p>`;
modal.classList.remove("hidden");
modal.classList.add("flex");
}
// Hide status modal
hideModal() {
const modal = document.getElementById("status-modal");
modal.classList.add("hidden");
modal.classList.remove("flex");
}
// Setup event handlers for data management buttons
setupDataManagementHandlers() {
// Save data button
const saveButton = document.getElementById("save-data");
if (saveButton) {
saveButton.addEventListener("click", () => {
try {
const formData = this.getFormData();
this.saveCompanyDetails(formData);
this.saveBankDetails(formData);
this.saveCustomerDetails(formData);
this.updateStorageStatus();
this.showModal("β
Data saved successfully!", "success");
} catch (error) {
console.error("Error saving data:", error);
this.showModal(
"β Error saving data. Please try again.",
"error",
);
}
});
}
// Clear data button
const clearButton = document.getElementById("clear-data");
if (clearButton) {
clearButton.addEventListener("click", () => {
if (
confirm(
"Are you sure you want to clear all saved data? This cannot be undone.",
)
) {
try {
this.clearAllData();
// Clear form fields
document.getElementById("quotation-form").reset();
this.updateStorageStatus();
this.showModal(
"ποΈ All saved data cleared successfully!",
"success",
);
} catch (error) {
console.error("Error clearing data:", error);
this.showModal(
"β Error clearing data. Please try again.",
"error",
);
}
}
});
}
// Modal close button
const modalCloseButton = document.getElementById("modal-close");
if (modalCloseButton) {
modalCloseButton.addEventListener("click", () => {
this.hideModal();
});
}
// Close modal when clicking outside
const modal = document.getElementById("status-modal");
if (modal) {
modal.addEventListener("click", (e) => {
if (e.target === modal) {
this.hideModal();
}
});
}
// Storage info panel handlers
const showStorageInfoButton =
document.getElementById("show-storage-info");
const hideStorageInfoButton =
document.getElementById("hide-storage-info");
const storageInfoPanel = document.getElementById("storage-info-panel");
if (showStorageInfoButton && storageInfoPanel) {
showStorageInfoButton.addEventListener("click", () => {
this.updateStorageStatus();
storageInfoPanel.classList.remove("hidden");
});
}
if (hideStorageInfoButton && storageInfoPanel) {
hideStorageInfoButton.addEventListener("click", () => {
storageInfoPanel.classList.add("hidden");
});
}
}
// Update storage status display
updateStorageStatus() {
const companyData = this.loadCompanyDetails();
const bankData = this.loadBankDetails();
const customerData = this.loadCustomerDetails();
// Update company status
const companyStatus = document.getElementById("company-storage-status");
if (companyStatus) {
if (companyData && companyData.name) {
companyStatus.textContent = `β
Saved (${companyData.name})`;
companyStatus.className = "text-blue-600";
} else {
companyStatus.textContent = "β Not saved";
companyStatus.className = "text-gray-600";
}
}
// Update bank status
const bankStatus = document.getElementById("bank-storage-status");
if (bankStatus) {
if (bankData && bankData.name) {
bankStatus.textContent = `β
Saved (${bankData.name})`;
bankStatus.className = "text-green-600";
} else {
bankStatus.textContent = "β Not saved";
bankStatus.className = "text-gray-600";
}
}
// Update customer status
const customerStatus = document.getElementById(
"customer-storage-status",
);
if (customerStatus) {
if (customerData && customerData.name) {
customerStatus.textContent = `β
Saved in session (${customerData.name})`;
customerStatus.className = "text-yellow-600";
} else {
customerStatus.textContent = "β Not saved (Session only)";
customerStatus.className = "text-gray-600";
}
}
}
// Initialize data manager
init() {
if (!this.isLocalStorageAvailable()) {
console.warn(
"localStorage is not available. Data will not be saved.",
);
return;
}
// Load existing data when page loads
document.addEventListener("DOMContentLoaded", () => {
this.loadFormData();
this.setupAutoSave();
this.setupDataManagementHandlers();
this.updateStorageStatus();
});
// Clear customer data when page unloads (optional)
window.addEventListener("beforeunload", () => {
this.clearCustomerData();
});
}
}
// Initialize the data manager
const dataManager = new QuotationDataManager();
dataManager.init();
|