-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
83 lines (67 loc) · 2.6 KB
/
scripts.js
File metadata and controls
83 lines (67 loc) · 2.6 KB
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
console.log("scripts.js is loaded and running!");
// Helper function to create a unique order ID
function generateUniqueId(orderNumber) {
const nzDate = new Date().toLocaleString("en-NZ", { timeZone: "Pacific/Auckland" });
return `Order-${orderNumber}-${nzDate}`;
}
// Function to handle form submission
function buyPlan(planName) {
console.log(`Buying plan: ${planName}`);
// Get the form associated with the plan
const formId = `${planName.toLowerCase()}-form`;
const form = document.getElementById(formId);
if (!form) {
console.error(`Form with ID '${formId}' not found.`);
return;
}
// Prevent default form submission
form.addEventListener("submit", function (event) {
event.preventDefault();
// Collect form values
const ign = this.querySelector("[name='ign']").value;
const location = this.querySelector("[name='location']").value;
const thorns = this.querySelector("[name='thorns']")?.value || "N/A";
const knockback = this.querySelector("[name='knockback']")?.value || "N/A";
const fireaspect = this.querySelector("[name='fireaspect']")?.value || "N/A";
// Load existing orders or initialize an empty array
const orders = JSON.parse(localStorage.getItem("orders")) || [];
const orderNumber = orders.length + 1;
const uniqueId = generateUniqueId(orderNumber);
// Create a new order object
const newOrder = {
uniqueId,
ign,
location,
plan: planName,
thorns,
knockback,
fireaspect,
orderNumber,
date: new Date().toISOString(),
};
// Save the order
orders.push(newOrder);
localStorage.setItem("orders", JSON.stringify(orders));
console.log("New order stored:", newOrder);
alert(`Order placed successfully! Your unique ID is: ${uniqueId}`);
form.reset();
});
}
// Attach event listeners to all forms
function initializeForms() {
const planNames = ["Wood", "Iron", "Diamond", "Netherite"];
planNames.forEach(planName => {
const formId = `${planName.toLowerCase()}-form`;
const form = document.getElementById(formId);
if (!form) {
console.warn(`Form with ID '${formId}' not found. Skipping.`);
return;
}
form.addEventListener("submit", function (event) {
event.preventDefault();
buyPlan(planName);
});
});
}
// Initialize on DOMContentLoaded
document.addEventListener("DOMContentLoaded", initializeForms);