Rework home page to use $state, $props, and $derived
This commit is contained in:
parent
5716d1ebc4
commit
7aa2fcd504
13
src/routes/+page.server.ts
Normal file
13
src/routes/+page.server.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import type { StatsTypeCurrencyChart } from "$lib/entities";
|
||||||
|
import type { PageServerLoad } from "./$types";
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
|
try {
|
||||||
|
const expensesData: StatsTypeCurrencyChart[] = await fetch("/api/statistics/type").then((res) => res.json());
|
||||||
|
return { expensesData: expensesData };
|
||||||
|
} catch (error) {
|
||||||
|
console.log("error in +page.server.ts", error)
|
||||||
|
return { expensesData: [] };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -13,22 +13,18 @@
|
||||||
|
|
||||||
Chart.register(DoughnutController, ArcElement, Tooltip, Legend);
|
Chart.register(DoughnutController, ArcElement, Tooltip, Legend);
|
||||||
|
|
||||||
let error: string | null = null;
|
let { data } = $props();
|
||||||
let data: StatsTypeCurrencyChart[] = [];
|
$inspect(data);
|
||||||
let configs: ChartConfiguration[] = [];
|
let error: string | null = $state(null);
|
||||||
let charts: Chart[] = [];
|
let { expensesData }: { expensesData: StatsTypeCurrencyChart[] } =
|
||||||
|
$state(data);
|
||||||
|
$inspect("expensesData =", expensesData);
|
||||||
|
|
||||||
|
// let expensesData: StatsTypeCurrencyChart[] = $state(data.expensesData);
|
||||||
let canvases: HTMLCanvasElement[] = [];
|
let canvases: HTMLCanvasElement[] = [];
|
||||||
|
let charts: Chart[] = [];
|
||||||
async function fetchChartStats() {
|
let configs: ChartConfiguration[] = $derived(
|
||||||
try {
|
expensesData.map((chart) => ({
|
||||||
const result = await fetch("/api/statistics/type");
|
|
||||||
if (!result.ok) {
|
|
||||||
const obj = await result.json();
|
|
||||||
error = obj.message;
|
|
||||||
} else {
|
|
||||||
data = await result.json();
|
|
||||||
|
|
||||||
configs = data.map((chart) => ({
|
|
||||||
type: "doughnut",
|
type: "doughnut",
|
||||||
data: {
|
data: {
|
||||||
labels: chart.elements.map((type) => type.name),
|
labels: chart.elements.map((type) => type.name),
|
||||||
|
@ -40,21 +36,18 @@
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
}));
|
})),
|
||||||
}
|
);
|
||||||
} catch (e) {
|
|
||||||
console.error("Error fetching data:", e);
|
|
||||||
error = "Failed to fetch chart data.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createTypeCharts() {
|
function createTypeCharts() {
|
||||||
charts.forEach((chart) => chart.destroy());
|
charts.forEach((chart) => chart.destroy());
|
||||||
charts = configs.map((config, index) => new Chart(canvases[index], config));
|
charts = configs.map((config, index) => new Chart(canvases[index], config));
|
||||||
}
|
}
|
||||||
onMount(async () => {
|
|
||||||
await fetchChartStats();
|
onMount(() => {
|
||||||
|
if (expensesData && canvases.length > 0) {
|
||||||
createTypeCharts();
|
createTypeCharts();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
|
@ -67,6 +60,10 @@
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<!-- Render canvas elements for charts -->
|
<!-- Render canvas elements for charts -->
|
||||||
|
<div class="container mx-auto my-8 bg-gray-100 p-6">
|
||||||
|
<h2 class="text-2xl mb-4 font-bold text-center block">
|
||||||
|
Expenses doughnut charts
|
||||||
|
</h2>
|
||||||
<div class="flex items-center justify-center">
|
<div class="flex items-center justify-center">
|
||||||
<div
|
<div
|
||||||
class="{configs.length > 4
|
class="{configs.length > 4
|
||||||
|
@ -78,13 +75,14 @@
|
||||||
<div class="type-chart">
|
<div class="type-chart">
|
||||||
<canvas bind:this={canvases[index]}></canvas>
|
<canvas bind:this={canvases[index]}></canvas>
|
||||||
<span class="flex justify-center"
|
<span class="flex justify-center"
|
||||||
>{`Total: ${NumberToFPA(data[index].total)} ${data[index].label}`}</span
|
>{`Total: ${NumberToFPA(expensesData[index].total)} ${expensesData[index].label}`}</span
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<style lang="postcss">
|
<style lang="postcss">
|
||||||
.type-chart {
|
.type-chart {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user