Skip to content

Commit

Permalink
chore: Setup Prettier (#43)
Browse files Browse the repository at this point in the history
* chore: Setup prettier

* chore: Run prettier on all current files

* chore: Add script to package.json
  • Loading branch information
DafyddLlyr authored Aug 6, 2024
1 parent 01efc39 commit 64c7b9b
Show file tree
Hide file tree
Showing 28 changed files with 1,235 additions and 448 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "next/core-web-vitals"
"extends": ["next/core-web-vitals", "prettier"]
}
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-staged
10 changes: 10 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require("path");

const buildEslintCommand = (filenames) =>
`next lint --fix --file ${filenames
.map((f) => path.relative(process.cwd(), f))
.join(" --file ")}`;

module.exports = {
"*.{js,jsx,ts,tsx}": [buildEslintCommand],
};
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"bracketSpacing": true
}
2 changes: 1 addition & 1 deletion app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const prisma = new PrismaClient();
export async function POST(req: Request) {
try {
// Parse and validate user input
const data = await req.json()
const data = await req.json();
const input: Calculation = calculationSchema.parse(data);

// data are going to be queried at different levels of granularity based on the postcode
Expand Down
266 changes: 139 additions & 127 deletions app/components/graphs/LifetimeCombinedChart.tsx
Original file line number Diff line number Diff line change
@@ -1,154 +1,166 @@
"use client";

import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef } from "react";
import {
Chart,
ChartData,
ChartDataset,
ChartOptions,
ChartTypeRegistry,
PointElement,
BarElement,
BarController,
LineController,
LineElement,
CategoryScale,
LinearScale,
Tooltip,
Legend
} from 'chart.js';

Chart.register(PointElement, BarElement, BarController, LineController, LineElement, CategoryScale, LinearScale, Tooltip, Legend);
Chart,
ChartData,
ChartDataset,
ChartOptions,
ChartTypeRegistry,
PointElement,
BarElement,
BarController,
LineController,
LineElement,
CategoryScale,
LinearScale,
Tooltip,
Legend,
} from "chart.js";

Chart.register(
PointElement,
BarElement,
BarController,
LineController,
LineElement,
CategoryScale,
LinearScale,
Tooltip,
Legend
);

type DataInput = {
year: string;
landCost: number;
houseCost: number;
maintenanceCost?: number;
billsCost?: number;
incomeThreshold: number;
}
year: string;
landCost: number;
houseCost: number;
maintenanceCost?: number;
billsCost?: number;
incomeThreshold: number;
};

interface ColorScheme {
land: string;
house: string;
maintenance: string;
bills: string;
incomeThreshold: string;
land: string;
house: string;
maintenance: string;
bills: string;
incomeThreshold: string;
}

interface LifetimeChartProps {
data: DataInput[];
colorScheme: ColorScheme;
data: DataInput[];
colorScheme: ColorScheme;
}

// Implementation of combination bar and line chart
const LifetimeCombinedChart: React.FC<LifetimeChartProps> = ({ data, colorScheme }) => {

const ref = useRef<HTMLCanvasElement>(null);
const chartRef = useRef<Chart | null>(null);

useEffect(() => {
if (!ref.current) return;

// Transform the data into a format suitable for Chart.js
const years = data.map(item => item.year);
const landData = data.map(item => item.landCost);
const houseData = data.map(item => item.houseCost);
const maintenanceData = data.map(item => item.maintenanceCost ?? null);
const billsData = data.map(item => item.billsCost ?? null);
const affordabilityThreshold = data.map(item => item.incomeThreshold);

const datasets: ChartDataset<'bar' | 'line', number[]>[] = [
{
type: 'bar',
label: 'Land',
data: landData,
backgroundColor: colorScheme.land,
order: 1,
},
{
type: 'bar',
label: 'House',
data: houseData,
backgroundColor: colorScheme.house,
order: 2,
},
{
type: 'line',
label: 'Affordability threshold',
data: affordabilityThreshold,
borderColor: colorScheme.incomeThreshold,
backgroundColor: 'transparent',
order: 0,
},
];
// Only add maintenance data if it exists
if (maintenanceData.some(value => value !== null)) {
datasets.push({
type: 'bar',
label: 'Maintenance',
data: maintenanceData.map(value => value ?? 0),
backgroundColor: colorScheme.maintenance,
order: 3,
});
}
// Only add bills data if it exists
if (billsData.some(value => value !== null)) {
datasets.push({
type: 'bar',
label: 'Bills',
data: billsData.map(value => value ?? 0),
backgroundColor: colorScheme.bills,
order: 2,
});
}
const LifetimeCombinedChart: React.FC<LifetimeChartProps> = ({
data,
colorScheme,
}) => {
const ref = useRef<HTMLCanvasElement>(null);
const chartRef = useRef<Chart | null>(null);

useEffect(() => {
if (!ref.current) return;

// Transform the data into a format suitable for Chart.js
const years = data.map((item) => item.year);
const landData = data.map((item) => item.landCost);
const houseData = data.map((item) => item.houseCost);
const maintenanceData = data.map((item) => item.maintenanceCost ?? null);
const billsData = data.map((item) => item.billsCost ?? null);
const affordabilityThreshold = data.map((item) => item.incomeThreshold);

const datasets: ChartDataset<"bar" | "line", number[]>[] = [
{
type: "bar",
label: "Land",
data: landData,
backgroundColor: colorScheme.land,
order: 1,
},
{
type: "bar",
label: "House",
data: houseData,
backgroundColor: colorScheme.house,
order: 2,
},
{
type: "line",
label: "Affordability threshold",
data: affordabilityThreshold,
borderColor: colorScheme.incomeThreshold,
backgroundColor: "transparent",
order: 0,
},
];
// Only add maintenance data if it exists
if (maintenanceData.some((value) => value !== null)) {
datasets.push({
type: "bar",
label: "Maintenance",
data: maintenanceData.map((value) => value ?? 0),
backgroundColor: colorScheme.maintenance,
order: 3,
});
}
// Only add bills data if it exists
if (billsData.some((value) => value !== null)) {
datasets.push({
type: "bar",
label: "Bills",
data: billsData.map((value) => value ?? 0),
backgroundColor: colorScheme.bills,
order: 2,
});
}

const chartData: ChartData<"bar" | "line", number[], string> = {
labels: years,
datasets: datasets,
};

const chartData: ChartData<'bar' | 'line', number[], string> = {
labels: years,
datasets: datasets,
};

// Clear the canvas before re-drawing
if (chartRef.current) {
chartRef.current.destroy();
}

const newChartInstance = new Chart(ref.current, {
type: 'bar',
data: chartData,
options: {
responsive: true,
scales: {
x: {
stacked: true,
title: {
display: true,
text: 'Year',
},
},
y: {
stacked: true,
title: {
display: true,
text: 'Yearly payments',
},
},
},
// Clear the canvas before re-drawing
if (chartRef.current) {
chartRef.current.destroy();
}

const newChartInstance = new Chart(ref.current, {
type: "bar",
data: chartData,
options: {
responsive: true,
scales: {
x: {
stacked: true,
title: {
display: true,
text: "Year",
},
});
},
y: {
stacked: true,
title: {
display: true,
text: "Yearly payments",
},
},
},
},
});

chartRef.current = newChartInstance;
chartRef.current = newChartInstance;

// Cleanup function to destroy the chart instance when the component unmounts
return () => {
if (chartRef.current) {
chartRef.current.destroy();
}
};
}, [data, colorScheme]);
}, [data, colorScheme]);

return <canvas id={`chart-${colorScheme}`} ref={ref}></canvas>;
};

export default LifetimeCombinedChart;
export default LifetimeCombinedChart;
Loading

0 comments on commit 64c7b9b

Please sign in to comment.