-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventSpaceManager.js
104 lines (88 loc) · 2.8 KB
/
eventSpaceManager.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
function openEventSpaces() {
const productId = setupCell("Dashboard", "B49");
const product = getProductById(productId);
if (product.type === "variable") {
openVariableProductSpaces(product);
} else {
openSimpleProductSpaces(product);
}
}
function openVariableProductSpaces(product) {
const variations = getProductVariations(product.id);
const ui = SpreadsheetApp.getUi();
let htmlOutput = '<form id="variationForm">';
for (const variation of variations) {
htmlOutput += `
<label for="${variation.id}">${variation.attributes[0].option}:</label>
<input type="number" id="${variation.id}" name="${variation.id}" value="${variation.stock_quantity || 0}" min="0"><br><br>
`;
}
htmlOutput += '<input type="submit" value="Update Spaces">';
htmlOutput += "</form>";
const htmlTemplate = HtmlService.createTemplate(htmlOutput);
htmlTemplate.productId = product.id;
const html = htmlTemplate.evaluate().setWidth(300).setHeight(400);
ui.showModalDialog(html, "Update Event Spaces");
}
function openSimpleProductSpaces(product) {
const ui = SpreadsheetApp.getUi();
const result = ui.prompt(
"Update Event Spaces",
"Enter the number of spaces to open:",
ui.ButtonSet.OK_CANCEL,
);
if (result.getSelectedButton() === ui.Button.OK) {
const newStock = Number.parseInt(result.getResponseText());
if (!Number.isNaN(newStock) && newStock >= 0) {
updateProductStock(product.id, newStock);
ui.alert("Event spaces updated successfully!");
} else {
ui.alert("Please enter a valid number of spaces.");
}
}
}
function updateProductVariations(productId, updatedVariations) {
for (const [variationId, newStock] of Object.entries(updatedVariations)) {
const data = {
stock_quantity: Number.parseInt(newStock),
manage_stock: true,
};
pokeToWordPressProducts(data, productId, variationId);
}
}
function updateProductStock(productId, newStock) {
const data = {
stock_quantity: newStock,
manage_stock: true,
};
pokeToWordPressProducts(data, productId);
}
function openSignup() {
openEventSpaces();
}
function closeSignup() {
const productId = setupCell("Dashboard", "B49");
const product = getProductById(productId);
if (product.type === "variable") {
closeVariableProductSpaces(product);
} else {
closeSimpleProductSpaces(product);
}
}
function closeVariableProductSpaces(product) {
const variations = getProductVariations(product.id);
for (const variation of variations) {
const data = {
stock_quantity: 0,
manage_stock: true,
};
pokeToWordPressProducts(data, product.id, variation.id);
}
const ui = SpreadsheetApp.getUi();
ui.alert("Signup closed successfully. All places set to 0.");
}
function closeSimpleProductSpaces(product) {
updateProductStock(product.id, 0);
const ui = SpreadsheetApp.getUi();
ui.alert("Signup closed successfully. All places set to 0.");
}