-
Notifications
You must be signed in to change notification settings - Fork 0
/
[reg].tsx
215 lines (203 loc) · 6.21 KB
/
[reg].tsx
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
import NumberPlate from "@/components/number-plate/number-plate-visualiser";
import ValidateNumberPlate from "@/components/number-plate/number-plate-validate";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import { GetVehicleInfo, GetEbayListings } from "@/components/api/api-calls";
import UITaxMot from "@/components/ui/vehicle-info/ui-tax-mot";
import UIVehicleInformation from "@/components/ui/vehicle-info/ui-vehicle-information";
import UIVehicleSpec from "@/components/ui/vehicle-info/ui-vehicle-spec";
import UIMileage from "@/components/ui/mileage-history/ui-mileage";
import UIVehicleNotFound from "@/components/ui/not-found/ui-vehicle-not-found";
import UIMotSummary from "@/components/ui/mot-history/ui-mot-summary";
import UIVehiclesForSale from "@/components/ui/vehicle-info/ui-similar-vehicles";
import Loading from "@/components/ui/loading";
import { Separator } from "@/components/ui/separator";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { NextSeo } from "next-seo";
type vehicleInfoType = {
errorMessage: string;
taxStatus: {
taxed: string;
due: string;
};
taxInfo: {
rate: number | string;
band: string;
};
motStatus: { mot: string; expiryDateShort: string; expiryDateLong: string };
motInfo: {
lastMotTestDateShort: string;
lastMotTestDateLong: string;
hasHadMot: boolean;
totalMots: number;
totalPassedMots: number;
totalPassedMotsNoAdvisory: number;
totalPassedMotsWithAdvisory: number;
totalFailedMots: number;
totalAdvisories: number;
totalFails: number;
};
vehicleInformation: {
make: string;
model: string;
year: number | string;
colour: string;
v5cIssuedShort: string;
v5cIssuedLong: string;
firstRegistered: string;
markedForExport: boolean | string;
};
vehicleSpec: {
engineSize: number | string;
co2Emissions: number | string;
euroStatus: string;
fuelType: string;
revenueWeight: number | string;
};
mileage: {
lastMotMilage: number | string;
motOdometerUnit: string;
allPassedMotMiles: Array<allPassedMotMilesType>;
mileageIncreasePerYear: Array<mileageIncreasePerYearType>;
};
};
type allPassedMotMilesType = {
date: string;
odometerValue: number | string;
};
type mileageIncreasePerYearType = {
date: string;
odometerValue: number | string;
mileageDifference: number | string;
};
type ebayListingsType = {
results: object;
};
export async function getServerSideProps({
params,
}: {
params: { reg: string };
}) {
const { reg } = params;
const formattedReg = reg.replace(" ", "");
const upperCaseReg = formattedReg.toUpperCase();
const isValid = (await ValidateNumberPlate(upperCaseReg)) as boolean;
const validReg = isValid;
return {
props: {
upperCaseReg,
validReg,
},
};
}
export default function VehicleInfo({
upperCaseReg,
validReg,
}: {
upperCaseReg: string;
validReg: boolean;
}) {
const [vehicleData, setVehicleData] = useState<vehicleInfoType>();
const [ebayListings, setEbayListings] = useState<ebayListingsType>();
const router = useRouter();
// Default values shown
useEffect(() => {
if (!validReg) {
void router.push("/");
}
void router.replace(`/vehicle-check/${upperCaseReg}`, undefined, {
shallow: true,
});
// disable this rule as it does an infinite loop if i add router
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [validReg, upperCaseReg]);
useEffect(() => {
function fetchData() {
GetVehicleInfo(upperCaseReg)
.then((data) => setVehicleData(data as vehicleInfoType))
.catch((error) => {
console.error("Failed to fetch vehicle info:", error);
});
}
if (validReg) {
fetchData();
}
}, [upperCaseReg, validReg]);
useEffect(() => {
if (vehicleData?.errorMessage) {
return;
}
if (vehicleData) {
GetEbayListings(
vehicleData.vehicleInformation.make,
vehicleData.vehicleInformation.model,
vehicleData.vehicleSpec.engineSize,
vehicleData.vehicleInformation.year,
)
.then((data) => setEbayListings(data as ebayListingsType))
.catch((error) => {
console.error("Failed to fetch vehicle info:", error);
});
}
}, [vehicleData]);
return (
<>
<NextSeo title={`Vehicle Check - ${upperCaseReg}`} />
<h1 className="text-center md:text-5xl text-3xl font-bold">Vehicle Check</h1>
<NumberPlate reg={upperCaseReg} className="self-top" />
{vehicleData && ebayListings !== null ? (
// If there is an error message, the car cannot be found so show the error message
vehicleData.errorMessage ? (
<UIVehicleNotFound reg={upperCaseReg} previousPage="vehicle-check" />
) : (
// If there is no error message, show the vehicle information
<>
<UITaxMot vehicleData={vehicleData} />
<Separator className="md:w-3/4" />
<h2 className="text-center md:text-3xl text-xl font-bold">
Basic Checks
</h2>
<div className="flex flex-col md:flex-row md:justify-center md:flex-wrap gap-4 w-full">
<UIVehicleInformation vehicleData={vehicleData} />
<UIVehicleSpec vehicleData={vehicleData} />
</div>
<Separator className="md:w-3/4" />
<h3 className="text-center md:text-3xl text-xl font-bold">MOT Summary</h3>
<UIMotSummary vehicleData={vehicleData} />
<Link href={`/mot-history/${upperCaseReg}`} passHref>
{vehicleData.motInfo.hasHadMot ? (
<Button className="hover:brightness-75 active:brightness-75">
View MOT History
</Button>
) : null}
</Link>
<Separator className="md:w-3/4" />
<h3 className="text-center md:text-3xl text-xl font-bold">
Mileage Summary
</h3>
<UIMileage vehicleData={vehicleData} />
<Link href={`/mileage-history/${upperCaseReg}`} passHref>
{vehicleData.motInfo.hasHadMot ? (
<Button className="hover:brightness-75 active:brightness-75">
View Mileage History
</Button>
) : null}
</Link>
<Separator className="md:w-3/4" />
{Array.isArray(ebayListings) && ebayListings.length > 0 ? (
<>
<h3 className="text-center md:text-3xl text-xl font-bold">
Similar Vehicles For Sale
</h3>
<UIVehiclesForSale ebayListings={ebayListings} />
</>
) : null}
</>
)
) : (
<Loading />
)}
</>
);
}