-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
372 lines (339 loc) · 10 KB
/
script.js
File metadata and controls
372 lines (339 loc) · 10 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
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
const fallbackDataUrl = "./data/sample_dashboard.json";
const apiEndpoint = "/api/dashboard";
const state = {
period: "30d",
payload: null,
charts: {}
};
const icons = {
carbon: "🌍",
water: "💧",
points: "⚡",
streak: "🔥",
mission: "🎯",
badge: "🏅",
tree: "🌱",
bike: "🚲",
recycle: "♻️",
transit: "🚌",
energy: "🔋"
};
async function loadDashboard(period = state.period) {
const query = `?period=${encodeURIComponent(period)}`;
try {
const response = await fetch(`${apiEndpoint}${query}`);
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
state.payload = await response.json();
} catch (error) {
console.warn("Using local fallback data:", error.message);
const fallbackResponse = await fetch(fallbackDataUrl);
state.payload = await fallbackResponse.json();
state.payload.meta.source = "local-fallback";
state.payload.meta.period = period;
}
renderAll();
}
function renderAll() {
if (!state.payload) return;
renderHeroStats();
renderKPIs();
renderProgress();
renderActivity();
renderMissions();
renderBadges();
renderLeaderboard();
renderCharts();
}
function renderHeroStats() {
const { summary } = state.payload;
setText("heroCarbon", formatNumber(summary.co2_avoided_kg));
setText("heroStreak", `${summary.streak_days}`);
setText("heroWater", formatNumber(summary.water_saved_liters));
setText("heroRank", `#${summary.community_rank}`);
}
function renderKPIs() {
const kpis = [
{
label: "Climate points",
value: formatNumber(state.payload.summary.points),
note: "Earned from verified actions",
trend: `+${state.payload.summary.points_growth_pct}%`,
icon: icons.points
},
{
label: "CO₂ avoided (kg)",
value: formatNumber(state.payload.summary.co2_avoided_kg),
note: "Transportation + energy + waste",
trend: `+${state.payload.summary.co2_growth_pct}%`,
icon: icons.carbon
},
{
label: "Water saved (L)",
value: formatNumber(state.payload.summary.water_saved_liters),
note: "Household efficiency actions",
trend: `+${state.payload.summary.water_growth_pct}%`,
icon: icons.water
},
{
label: "Trees equivalent",
value: formatNumber(state.payload.summary.trees_equivalent),
note: "Converted environmental impact",
trend: `${state.payload.summary.streak_days} day streak`,
icon: icons.tree
}
];
const grid = document.getElementById("kpiGrid");
grid.innerHTML = kpis
.map(
(kpi) => `
<article class="kpi-card">
<div class="kpi-top">
<span class="kpi-icon" aria-hidden="true">${kpi.icon}</span>
<span class="kpi-trend">${kpi.trend}</span>
</div>
<p class="kpi-label">${kpi.label}</p>
<strong class="kpi-value">${kpi.value}</strong>
<p class="metric-note">${kpi.note}</p>
</article>
`
)
.join("");
}
function renderProgress() {
const { mission_progress } = state.payload.summary;
const percent = Math.max(0, Math.min(100, mission_progress.percentage));
const circumference = 2 * Math.PI * 70;
const offset = circumference - (percent / 100) * circumference;
document.getElementById("progressRingValue").style.strokeDasharray = `${circumference}`;
document.getElementById("progressRingValue").style.strokeDashoffset = `${offset}`;
setText("missionProgressValue", `${percent}%`);
const details = document.getElementById("progressDetails");
details.innerHTML = `
<li><strong>${mission_progress.current}</strong> of <strong>${mission_progress.target}</strong> mission points completed</li>
<li><strong>${mission_progress.next_reward}</strong> unlocks at ${mission_progress.target} points</li>
<li><strong>${mission_progress.remaining}</strong> points left to complete this cycle</li>
`;
}
function renderActivity() {
const list = document.getElementById("activityList");
list.innerHTML = state.payload.activities
.map(
(item) => `
<article class="activity-item">
<div class="activity-icon">${icons[item.icon] || "🌿"}</div>
<div class="activity-copy">
<h4>${item.title}</h4>
<p>${item.description}</p>
<small class="activity-meta">${item.timestamp} • +${item.points} pts • ${item.impact}</small>
</div>
</article>
`
)
.join("");
}
function renderMissions() {
const list = document.getElementById("missionList");
list.innerHTML = state.payload.missions
.map(
(mission) => `
<article class="mission-item">
<div class="mission-main">
<h4>${mission.title}</h4>
<p>${mission.description}</p>
</div>
<div class="mission-meta">
<span class="mission-points">+${mission.points} pts</span>
<div class="mission-status">${mission.status}</div>
</div>
</article>
`
)
.join("");
}
function renderBadges() {
const grid = document.getElementById("badgeGrid");
grid.innerHTML = state.payload.badges
.map(
(badge) => `
<article class="badge-item">
<div class="badge-icon">${icons[badge.icon] || "🏅"}</div>
<h4>${badge.name}</h4>
<p>${badge.description}</p>
</article>
`
)
.join("");
}
function renderLeaderboard() {
const body = document.getElementById("leaderboardBody");
body.innerHTML = state.payload.leaderboard
.map(
(entry) => `
<div class="leaderboard-row">
<span class="leaderboard-rank">#${entry.rank}</span>
<span>${entry.name}</span>
<span>${formatNumber(entry.points)}</span>
<span>${entry.co2_saved_kg} kg</span>
<span>${entry.badge}</span>
</div>
`
)
.join("");
}
function renderCharts() {
renderImpactTrendChart();
renderCategoryChart();
}
function renderImpactTrendChart() {
const ctx = document.getElementById("impactTrendChart");
destroyChart("impactTrend");
const chartData = state.payload.trends;
state.charts.impactTrend = new Chart(ctx, {
type: "line",
data: {
labels: chartData.map((item) => item.label),
datasets: [
{
label: "CO₂ Avoided (kg)",
data: chartData.map((item) => item.co2_avoided_kg),
borderColor: "#54d2a7",
backgroundColor: "rgba(84, 210, 167, 0.18)",
fill: true,
tension: 0.35,
pointRadius: 3
},
{
label: "Points Earned",
data: chartData.map((item) => item.points),
borderColor: "#7ed9ff",
backgroundColor: "rgba(126, 217, 255, 0.1)",
fill: false,
tension: 0.35,
pointRadius: 3
}
]
},
options: getSharedChartOptions()
});
}
function renderCategoryChart() {
const ctx = document.getElementById("categoryChart");
destroyChart("category");
const categories = state.payload.categories;
state.charts.category = new Chart(ctx, {
type: "bar",
data: {
labels: categories.map((item) => item.category),
datasets: [
{
label: "Impact points",
data: categories.map((item) => item.value),
backgroundColor: [
"rgba(84, 210, 167, 0.85)",
"rgba(126, 217, 255, 0.85)",
"rgba(255, 214, 107, 0.85)",
"rgba(255, 139, 139, 0.8)"
],
borderRadius: 12,
borderSkipped: false
}
]
},
options: {
...getSharedChartOptions(),
plugins: {
legend: { display: false }
}
}
});
}
function getSharedChartOptions() {
return {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
ticks: { color: "#aac0bb" },
grid: { display: false }
},
y: {
ticks: { color: "#aac0bb" },
grid: { color: "rgba(255,255,255,0.06)" }
}
},
plugins: {
legend: {
labels: { color: "#eef6f3" }
},
tooltip: {
backgroundColor: "rgba(6, 17, 26, 0.94)",
titleColor: "#eef6f3",
bodyColor: "#eef6f3",
borderColor: "rgba(84, 210, 167, 0.2)",
borderWidth: 1
}
}
};
}
function destroyChart(key) {
if (state.charts[key]) {
state.charts[key].destroy();
}
}
function setText(id, value) {
const element = document.getElementById(id);
if (element) element.textContent = value;
}
function formatNumber(value) {
return new Intl.NumberFormat("en-US").format(value);
}
function setupPeriodFilters() {
const buttons = document.querySelectorAll(".filter-btn");
buttons.forEach((button) => {
button.addEventListener("click", async () => {
buttons.forEach((btn) => btn.classList.remove("active"));
button.classList.add("active");
state.period = button.dataset.period;
await loadDashboard(state.period);
});
});
}
function setupNavigation() {
const navLinks = document.querySelectorAll(".nav-link");
const sections = [...document.querySelectorAll("main .section")];
const menuToggle = document.getElementById("menuToggle");
const navLinksWrap = document.getElementById("navLinks");
menuToggle.addEventListener("click", () => {
const isOpen = navLinksWrap.classList.toggle("open");
menuToggle.setAttribute("aria-expanded", String(isOpen));
});
navLinks.forEach((link) => {
link.addEventListener("click", () => {
navLinksWrap.classList.remove("open");
menuToggle.setAttribute("aria-expanded", "false");
});
});
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
const id = entry.target.getAttribute("id");
navLinks.forEach((link) => {
link.classList.toggle("active", link.getAttribute("href") === `#${id}`);
});
});
},
{
rootMargin: "-45% 0px -45% 0px",
threshold: 0.01
}
);
sections.forEach((section) => observer.observe(section));
}
window.addEventListener("DOMContentLoaded", async () => {
setupNavigation();
setupPeriodFilters();
await loadDashboard();
});