implemented api for search logic

This commit is contained in:
Khalim Conn-Kowlessar 2023-05-28 11:59:29 +01:00
parent a4f6b00a1c
commit a9fe146cdf
3 changed files with 67 additions and 29 deletions

View file

@ -1,19 +0,0 @@
export async function getEPCData(postcode: string) {
const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
const headers = {
Accept: "application/json",
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
};
try {
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Failed to fetch EPC data:", error);
throw error;
}
}

View file

@ -0,0 +1,58 @@
// export async function getEPCData(postcode: string) {
// const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
// const headers = {
// Accept: "application/json",
// Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
// };
// console.log("Fetching EPC data from:", headers);
// try {
// const response = await fetch(url, { headers });
// if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
// }
// return await response.json();
// } catch (error) {
// console.error("Failed to fetch EPC data:", error);
// throw error;
// }
// }
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const postcode = request.nextUrl.searchParams.get("postcode");
// Check if the postcode is provided
if (!postcode) {
return new NextResponse("Postcode parameter is missing", { status: 400 });
}
const headers = {
Accept: "application/json",
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
};
try {
const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
const headers = {
Accept: "application/json",
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
};
const response = await fetch(url, {
headers: headers,
next: { revalidate: 60 },
});
const data = await response.json();
// Handle the response from the external API and return it
return NextResponse.json(data);
} catch (error) {
console.error("Error fetching data:", error);
return NextResponse.json({ message: "Internal server error", status: 500 });
}
}

View file

@ -34,18 +34,17 @@ export default function Search() {
return;
};
const fetchData = async () => {
const response = await fetch(`/api/addresses/${postcode}`);
console.log(data);
if (!response.ok) {
// handle error
console.error("An error occurred while fetching data");
return;
async function fetchData() {
try {
const response = await fetch(`/api/search?postcode=${postcode}`);
const data = await response.json();
setData(data.message);
} catch (error) {
console.error("Error fetching data:", error);
}
const data = await response.json();
setData(data);
};
}
const submitProps = {
buttonDisabled: buttonDisabled,