-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
128 lines (116 loc) · 3.73 KB
/
admin.js
File metadata and controls
128 lines (116 loc) · 3.73 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
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
const ADMIN_PASSPHRASE = "ADMIN";
const ADMIN_STORAGE_KEY = "adminToken";
const loginSection = document.getElementById("adminLoginSection");
const dashboardSection = document.getElementById("adminDashboardSection");
const loginForm = document.getElementById("adminLoginForm");
const passInput = document.getElementById("adminPass");
const loginError = document.getElementById("adminLoginError");
const logoutBtn = document.getElementById("adminLogoutBtn");
const productsBody = document.getElementById("adminProductsBody");
const PRODUCT_SUMMARIES = {
apple: {
sku: "APL-001",
tagline: "Keeps the doctor away (unless you throw it at them).",
status: "In stock",
},
banana: {
sku: "BAN-002",
tagline: "Nature's energy bar. Also, giraffes approve!",
status: "Hot seller",
},
lemon: {
sku: "LEM-003",
tagline: "When life gives you lemons, make a website.",
status: "In stock",
},
};
function getProductCatalog() {
if (typeof PRODUCTS !== "undefined") {
return PRODUCTS;
}
// Fallback if shop.js is unavailable
return {
apple: { name: "Apple", emoji: "🍏" },
banana: { name: "Banana", emoji: "🍌" },
lemon: { name: "Lemon", emoji: "🍋" },
};
}
function isAdminAuthenticated() {
return localStorage.getItem(ADMIN_STORAGE_KEY) === ADMIN_PASSPHRASE;
}
function setAuthenticated(value) {
if (value) {
localStorage.setItem(ADMIN_STORAGE_KEY, ADMIN_PASSPHRASE);
} else {
localStorage.removeItem(ADMIN_STORAGE_KEY);
}
}
function toggleViews() {
const authenticated = isAdminAuthenticated();
loginSection.style.display = authenticated ? "none" : "block";
dashboardSection.style.display = authenticated ? "block" : "none";
if (authenticated) {
renderProductOverview();
passInput.value = "";
loginError.textContent = "";
}
}
function renderProductOverview() {
const catalog = getProductCatalog();
const rows = Object.entries(catalog)
.map(([id, product]) => {
const summary = PRODUCT_SUMMARIES[id] || {};
const enabled =
typeof isProductEnabled === "function" ? isProductEnabled(id) : true;
return `
<tr>
<td class="admin-emoji">${product.emoji || "🍇"}</td>
<td>${product.name || id}</td>
<td>${summary.sku || id.toUpperCase()}</td>
<td>${summary.tagline || "No description"}</td>
<td>${summary.status || "TBD"}</td>
<td>
<label class="toggle-switch">
<input
type="checkbox"
data-visibility-toggle="${id}"
${enabled ? "checked" : ""}
aria-label="Toggle ${product.name || id} visibility"
/>
<span>${enabled ? "Enabled" : "Disabled"}</span>
</label>
</td>
</tr>
`;
})
.join("");
productsBody.innerHTML = rows;
attachVisibilityHandlers();
}
function attachVisibilityHandlers() {
const toggles = document.querySelectorAll("[data-visibility-toggle]");
toggles.forEach((toggle) => {
toggle.addEventListener("change", (event) => {
const productId = event.target.dataset.visibilityToggle;
if (typeof setProductEnabled === "function") {
setProductEnabled(productId, event.target.checked);
}
renderProductOverview();
});
});
}
loginForm.addEventListener("submit", (event) => {
event.preventDefault();
const passphrase = passInput.value.trim();
if (passphrase.toUpperCase() === ADMIN_PASSPHRASE) {
setAuthenticated(true);
toggleViews();
} else {
loginError.textContent = "Incorrect passphrase. Please try again.";
}
});
logoutBtn.addEventListener("click", () => {
setAuthenticated(false);
toggleViews();
});
document.addEventListener("DOMContentLoaded", toggleViews);