Fix JavaScript errors: Alpine.js x-for keys, Chart.js canvas reuse, missing CSS file

This commit is contained in:
1sonhador committed 2025-10-15 01:01:00 +01:00
1 parent d97c1a1881
commit 24459e38ff
2 files changed
+42 -12

No files matched your search

+42 -12
View File
@@ -228,7 +228,7 @@
</tr>
</thead>
<tbody>
<template x-for="(country, index) in stats.by_country.slice(0, 5)" :key="country.country_code">
<template x-for="(country, index) in (stats.by_country || []).slice(0, 5)" :key="'country-' + index">
<tr>
<td>
<div class="d-flex align-items-center">
@@ -272,7 +272,7 @@
</tr>
</thead>
<tbody>
<template x-for="(device, index) in stats.by_device" :key="device.device_type">
<template x-for="(device, index) in (stats.by_device || [])" :key="'device-' + index">
<tr>
<td>
<div class="d-flex align-items-center">
@@ -316,7 +316,7 @@
</tr>
</thead>
<tbody>
<template x-for="(referral, index) in stats.by_referral" :key="referral.referer">
<template x-for="(referral, index) in (stats.by_referral || [])" :key="'referral-' + index">
<tr>
<td>
<div class="d-flex align-items-center">
@@ -370,6 +370,15 @@ function publicCampaignStats() {
stats: window.publicCampaignStatsData.stats,
init() {
// Initialize with empty data to prevent Alpine errors
this.stats = {
overview: { total_clicks: 0, unique_clicks: 0, unique_ips: 0, avg_response_time: 0 },
by_country: [],
by_device: [],
by_browser: [],
by_referral: [],
daily_hits: []
};
this.loadStats();
},
@@ -428,16 +437,22 @@ function publicCampaignStats() {
const ctx = document.getElementById('daily-hits-chart');
if (!ctx) return;
// Destroy existing chart if it exists
if (window.dailyHitsChart) {
window.dailyHitsChart.destroy();
}
// Use real daily hits data from database
const dailyData = this.stats.daily_hits || [];
if (dailyData.length === 0) {
// Show empty state if no data
ctx.getContext('2d').clearRect(0, 0, ctx.width, ctx.height);
ctx.getContext('2d').fillStyle = '#666';
ctx.getContext('2d').font = '16px Arial';
ctx.getContext('2d').textAlign = 'center';
ctx.getContext('2d').fillText('No data available for the selected period', ctx.width / 2, ctx.height / 2);
const canvas = ctx.getContext('2d');
canvas.clearRect(0, 0, ctx.width, ctx.height);
canvas.fillStyle = '#666';
canvas.font = '16px Arial';
canvas.textAlign = 'center';
canvas.fillText('No data available for the selected period', ctx.width / 2, ctx.height / 2);
return;
}
@@ -447,7 +462,7 @@ function publicCampaignStats() {
});
const values = dailyData.map(d => parseInt(d.hits));
new Chart(ctx, {
window.dailyHitsChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
@@ -494,10 +509,15 @@ function publicCampaignStats() {
const ctx = document.getElementById('country-chart');
if (!ctx) return;
// Destroy existing chart if it exists
if (window.countryChart) {
window.countryChart.destroy();
}
const countries = (this.stats.by_country || []).slice(0, 5); // Limit to top 5
const colors = ['#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1'];
new Chart(ctx, {
window.countryChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: countries.map(c => c.country_name || c.country_code),
@@ -531,8 +551,13 @@ function publicCampaignStats() {
const ctx = document.getElementById('device-chart');
if (!ctx) return;
// Destroy existing chart if it exists
if (window.deviceChart) {
window.deviceChart.destroy();
}
const devices = this.stats.by_device || [];
new Chart(ctx, {
window.deviceChart = new Chart(ctx, {
type: 'bar',
data: {
labels: devices.map(d => d.device_type),
@@ -558,6 +583,11 @@ function publicCampaignStats() {
const ctx = document.getElementById('browser-chart');
if (!ctx) return;
// Destroy existing chart if it exists
if (window.browserChart) {
window.browserChart.destroy();
}
const browsers = (this.stats.by_browser || []).slice(0, 6); // Limit to top 6
const colors = ['#556ee6', '#f1b44c', '#34c38f', '#f46a6a', '#50a5f1', '#6c757d'];
@@ -569,7 +599,7 @@ function publicCampaignStats() {
return name;
});
new Chart(ctx, {
window.browserChart = new Chart(ctx, {
type: 'pie',
data: {
labels: cleanBrowsers,
View File
Whitespace-only changes.