I cba with this commit message tbh

This commit is contained in:
Hickmeister
2025-01-14 20:28:59 +00:00
parent a1aee4134c
commit a08c4eba3b
26 changed files with 1135 additions and 231 deletions

View File

@@ -1,6 +1,6 @@
<?php include '../src/session_check.php'; ?>
<html lang="en" data-bs-theme="light">
<html lang="en" data-bs-theme="dark">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
@@ -139,16 +139,6 @@ document.addEventListener("DOMContentLoaded", function () {
`;
}
// Debounce function to reduce DOM updates
function debounceRender(func, delay) {
let inDebounce;
return function() {
const context = this, args = arguments;
clearTimeout(inDebounce);
inDebounce = setTimeout(() => func.apply(context, args), delay);
};
}
fetch('../src/filamentTracker/getFilamentPrices.php')
.then(response => response.json())
.then(data => {
@@ -159,36 +149,56 @@ document.addEventListener("DOMContentLoaded", function () {
const prices = data.data[filament].prices.map(entry => entry.price);
const timestamps = data.data[filament].prices.map(entry => entry.recordedAt);
const latestPrice = prices[prices.length - 1] || 0;
const previousPrice = prices[prices.length - 2] || latestPrice;
const priceDifference = latestPrice - previousPrice;
// Find the last distinct price (where the price actually changed)
let lastDistinctPrice = latestPrice;
for (let i = prices.length - 2; i >= 0; i--) {
if (prices[i] !== latestPrice) {
lastDistinctPrice = prices[i];
break;
}
}
const priceDifference = latestPrice - lastDistinctPrice;
// Price Change Indicator
let priceChangeIndicator;
if (priceDifference > 0) {
priceChangeIndicator = `<span class="text-danger ms-4">
<i class="bx bx-up-arrow-alt"></i> +£${priceDifference.toFixed(2)}</span>`;
} else if (priceDifference < 0) {
priceChangeIndicator = `<span class="text-success ms-4">
<i class="bx bx-down-arrow-alt"></i> £${Math.abs(priceDifference).toFixed(2)}</span>`;
<i class="bx bx-down-arrow-alt"></i> -£${Math.abs(priceDifference).toFixed(2)}</span>`;
} else {
priceChangeIndicator = `<span class="text-muted ms-4">
<i class="bx bx-minus"></i> £0.00</span>`;
}
const amazonUrl = data.data[filament].amazonUrl || '#';
const discount = data.data[filament].currentDiscount || 0; // Get discount percentage
const chartId = `chart-${filament.replace(/\s+/g, '-')}`;
const chartColor = getRandomColor();
// Create Card HTML with cart button
// Create Card HTML
const cardHTML = `
<div class="col-lg-4 mb-1">
<div class="card radius-10 overflow-hidden">
<div class="card-body d-flex align-items-center justify-content-between">
<div>
<a href="${amazonUrl}" target="_blank" class="mb-0 filament-name">${filament}</a>
<a href="${amazonUrl}" target="_blank" class="mb-0 filament-name">${filament}</a>
<h5 class="mb-0">
£${latestPrice} ${priceChangeIndicator}
</h5>
</div>
<div>
${
discount > 0
? `<span class="text-success fw-bold" style="font-size: 1rem;">
-${discount}%
</span>`
: ''
}
</div>
</div>
<div id="${chartId}"></div>
</div>
@@ -197,84 +207,38 @@ document.addEventListener("DOMContentLoaded", function () {
container.insertAdjacentHTML('beforeend', cardHTML);
// Observer to Wait for DOM Insertion
const observer = new MutationObserver(debounceRender(() => {
const chartElement = document.getElementById(chartId);
if (chartElement) {
const chartOptions = {
series: [{
name: "Price",
data: prices
}],
chart: {
type: "area",
height: 110,
toolbar: {
show: false
},
zoom: {
enabled: false
},
dropShadow: {
enabled: true,
top: 3,
left: 14,
blur: 4,
opacity: 0.12,
color: chartColor
},
sparkline: {
enabled: true
}
// Chart Rendering
const chartOptions = {
series: [{ name: "Price", data: prices }],
chart: {
type: "area",
height: 110,
toolbar: { show: false },
zoom: { enabled: false },
sparkline: { enabled: true },
},
markers: { size: 0 },
dataLabels: { enabled: false },
stroke: { show: true, width: 2.4, curve: "smooth" },
colors: [chartColor],
xaxis: { categories: timestamps, labels: { show: false } },
fill: { opacity: 1 },
tooltip: {
theme: "dark",
y: {
formatter: function (value, { dataPointIndex }) {
const date = new Date(timestamps[dataPointIndex]);
return `£${value} - ${date.toLocaleDateString()}`;
},
markers: {
size: 0,
colors: [chartColor],
strokeColors: "#fff",
strokeWidth: 2,
hover: {
size: 7
}
},
dataLabels: {
enabled: false
},
stroke: {
show: true,
width: 2.4,
curve: "smooth"
},
colors: [chartColor],
xaxis: {
categories: timestamps,
labels: {
show: false
}
},
fill: {
opacity: 1
},
tooltip: {
theme: "dark",
x: {
show: false
},
y: {
formatter: function (value, { dataPointIndex }) {
const date = new Date(timestamps[dataPointIndex]);
return `£${value} - ${date.toLocaleDateString()}`;
}
}
}
};
},
},
};
const chart = new ApexCharts(chartElement, chartOptions);
chart.render();
observer.disconnect();
}
}, 300));
observer.observe(container, { childList: true, subtree: true });
const chartElement = document.getElementById(chartId);
if (chartElement) {
const chart = new ApexCharts(chartElement, chartOptions);
chart.render();
}
});
} else {
container.innerHTML = '<p class="text-center">No filament data available.</p>';
@@ -288,6 +252,9 @@ document.addEventListener("DOMContentLoaded", function () {
});
</script>
</body>
</html>