-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
181 lines (160 loc) · 5.25 KB
/
app.js
File metadata and controls
181 lines (160 loc) · 5.25 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
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
// app.js — light state manager + realistic-ish delays
const DEFAULT_STATE = {
loggedIn: false,
subscriptionStatus: "active", // active | paused | cancelled
billingProvider: "direct", // direct | apple | google | partner
plan: "Standard",
renewalDateISO: "2026-03-18",
payment: "Visa •••• 4242",
};
function qs() {
return new URLSearchParams(window.location.search);
}
function isTestMode() {
return qs().get("test") === "1";
}
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function delay(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function realisticDelay() {
if (isTestMode()) return;
await delay(randInt(300, 850));
}
function loadState() {
const raw = localStorage.getItem("mockflix_state");
if (!raw) return { ...DEFAULT_STATE };
try {
return { ...DEFAULT_STATE, ...JSON.parse(raw) };
} catch {
return { ...DEFAULT_STATE };
}
}
function saveState(state) {
localStorage.setItem("mockflix_state", JSON.stringify(state));
}
function setState(patch) {
const s = loadState();
const next = { ...s, ...patch };
saveState(next);
return next;
}
function requireLogin(redirectTo = "index.html") {
const s = loadState();
if (!s.loggedIn) window.location.href = redirectTo;
}
function fmtDate(iso) {
// keep it simple / stable (no locale surprises)
const [y, m, d] = iso.split("-").map(Number);
const months = [
"January","February","March","April","May","June",
"July","August","September","October","November","December"
];
return `${months[m - 1]} ${d}, ${y}`;
}
function setLoading(containerId = "page") {
const el = document.getElementById(containerId);
if (!el) return;
el.classList.add("is-loading");
}
function clearLoading(containerId = "page") {
const el = document.getElementById(containerId);
if (!el) return;
el.classList.remove("is-loading");
}
function disableBriefly(button) {
if (!button) return;
button.disabled = true;
const ms = isTestMode() ? 50 : randInt(350, 900);
setTimeout(() => (button.disabled = false), ms);
}
function signOut() {
setState({ loggedIn: false });
window.location.href = "index.html";
}
function injectTopbar(active = "") {
const s = loadState();
const top = document.getElementById("topbar");
if (!top) return;
top.innerHTML = `
<div class="topbar-inner">
<a class="brand" href="browse.html">Mockflix</a>
<div class="topbar-right">
<button class="iconbtn" id="profileBtn" aria-label="Profile menu" data-testid="profile-menu">
<span class="avatar"></span>
</button>
<div class="menu" id="profileMenu" aria-hidden="true">
<a href="account.html" class="${active === "account" ? "active" : ""}">Account</a>
<button class="menu-btn" id="logoutBtn" data-testid="logout">Sign out</button>
</div>
</div>
</div>
`;
const btn = document.getElementById("profileBtn");
const menu = document.getElementById("profileMenu");
btn?.addEventListener("click", () => {
const open = menu.getAttribute("aria-hidden") === "false";
menu.setAttribute("aria-hidden", open ? "true" : "false");
});
document.getElementById("logoutBtn")?.addEventListener("click", signOut);
document.addEventListener("click", (e) => {
if (!menu) return;
if (btn?.contains(e.target)) return;
if (menu.contains(e.target)) return;
menu.setAttribute("aria-hidden", "true");
});
// Little “real-world” touch: sometimes menu closes when you navigate quickly
if (!isTestMode()) {
window.addEventListener("beforeunload", () => {
try { menu.setAttribute("aria-hidden", "true"); } catch {}
});
}
}
function renderMembershipSummary(containerId = "membershipSummary") {
const s = loadState();
const el = document.getElementById(containerId);
if (!el) return;
const renewal = fmtDate(s.renewalDateISO);
let statusLine = "";
if (s.subscriptionStatus === "active") statusLine = `Renews on <strong>${renewal}</strong>`;
if (s.subscriptionStatus === "paused") statusLine = `Paused until <strong>${renewal}</strong>`;
if (s.subscriptionStatus === "cancelled") statusLine = `Active until <strong>${renewal}</strong>`;
el.innerHTML = `
<div class="kv">
<div class="k">Plan</div>
<div class="v">${s.plan}</div>
</div>
<div class="kv">
<div class="k">Status</div>
<div class="v">${statusLine}</div>
</div>
<div class="kv">
<div class="k">Billing</div>
<div class="v">${s.billingProvider === "direct" ? s.payment : `Billed through ${cap(s.billingProvider)}`}</div>
</div>
`;
}
function cap(x) {
return x.charAt(0).toUpperCase() + x.slice(1);
}
// For realism + testing: allow setting provider via query param once logged in.
// e.g. membership.html?provider=apple
function applyProviderParamIfPresent() {
const p = qs().get("provider");
if (!p) return;
const allowed = ["direct", "apple", "google", "partner"];
if (allowed.includes(p)) setState({ billingProvider: p });
}
// Expose a few helpers globally for inline scripts
window.Mockflix = {
loadState, saveState, setState,
requireLogin, realisticDelay,
setLoading, clearLoading,
disableBriefly, injectTopbar,
renderMembershipSummary, fmtDate,
applyProviderParamIfPresent,
signOut,
isTestMode
};