Addresses rendering nicely

This commit is contained in:
Khalim Conn-Kowlessar 2023-05-29 15:07:11 +01:00
parent a9fe146cdf
commit 53ca4fe7f3
2 changed files with 370 additions and 33 deletions

View file

@ -1,25 +1,3 @@
// 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) {
@ -43,11 +21,301 @@ export async function GET(request: NextRequest) {
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
};
const response = await fetch(url, {
headers: headers,
next: { revalidate: 60 },
});
const data = await response.json();
// const response = await fetch(url, {
// headers: headers,
// next: { revalidate: 60 },
// });
// const data = await response.json();
console.log("PUT ME BACK");
const data = {
rows: [
{
"low-energy-fixed-light-count": "1",
address: "53 Distillery Wharf, Regatta Lane",
"uprn-source": "Address Matched",
"floor-height": "",
"heating-cost-potential": "142",
"unheated-corridor-length": "",
"hot-water-cost-potential": "96",
"construction-age-band": "NO DATA!",
"potential-energy-rating": "B",
"mainheat-energy-eff": "Very Good",
"windows-env-eff": "Very Good",
"lighting-energy-eff": "Very Good",
"environment-impact-potential": "91",
"glazed-type": "NO DATA!",
"heating-cost-current": "142",
address3: "",
"mainheatcont-description":
"Charging system linked to use of community heating, TRVs",
"sheating-energy-eff": "N/A",
"property-type": "Flat",
"local-authority-label": "Hammersmith and Fulham",
"fixed-lighting-outlets-count": "1",
"energy-tariff": "standard tariff",
"mechanical-ventilation": "NO DATA!",
"hot-water-cost-current": "96",
county: "Greater London Authority",
postcode: "W6 9BF",
"solar-water-heating-flag": "",
constituency: "E14000726",
"co2-emissions-potential": "0.8",
"number-heated-rooms": "",
"floor-description": "Average thermal transmittance 0.19 W/m-¦K",
"energy-consumption-potential": "50",
"local-authority": "E09000013",
"built-form": "Mid-Terrace",
"number-open-fireplaces": "0",
"windows-description": "High performance glazing",
"glazed-area": "NO DATA!",
"inspection-date": "2017-04-05",
"mains-gas-flag": "",
"co2-emiss-curr-per-floor-area": "9",
address1: "53 Distillery Wharf",
"heat-loss-corridor": "NO DATA!",
"flat-storey-count": "",
"constituency-label": "Hammersmith",
"roof-energy-eff": "N/A",
"total-floor-area": "89.0",
"building-reference-number": "6013745278",
"environment-impact-current": "91",
"co2-emissions-current": "0.8",
"roof-description": "(other premises above)",
"floor-energy-eff": "Very Good",
"number-habitable-rooms": "",
address2: "Regatta Lane",
"hot-water-env-eff": "Very Good",
posttown: "LONDON",
"mainheatc-energy-eff": "Good",
"main-fuel": "NO DATA!",
"lighting-env-eff": "Very Good",
"windows-energy-eff": "Very Good",
"floor-env-eff": "Very Good",
"sheating-env-eff": "N/A",
"lighting-description": "Low energy lighting in all fixed outlets",
"roof-env-eff": "N/A",
"walls-energy-eff": "Very Good",
"photo-supply": "",
"lighting-cost-potential": "58",
"mainheat-env-eff": "Very Good",
"multi-glaze-proportion": "",
"main-heating-controls": "",
"lodgement-datetime": "2017-04-05 16:52:23",
"flat-top-storey": "",
"current-energy-rating": "B",
"secondheat-description": "None",
"walls-env-eff": "Very Good",
"transaction-type": "new dwelling",
uprn: "34154017",
"current-energy-efficiency": "86",
"energy-consumption-current": "50",
"mainheat-description": "Community scheme",
"lighting-cost-current": "58",
"lodgement-date": "2017-04-05",
"extension-count": "",
"mainheatc-env-eff": "Good",
"lmk-key": "1172153279062017040516522305558463",
"wind-turbine-count": "",
tenure: "unknown",
"floor-level": "mid floor",
"potential-energy-efficiency": "86",
"hot-water-energy-eff": "Very Good",
"low-energy-lighting": "100",
"walls-description": "Average thermal transmittance 0.16 W/m-¦K",
"hotwater-description": "Community scheme",
},
{
"low-energy-fixed-light-count": "1",
address: "55 Distillery Wharf, Regatta Lane",
"uprn-source": "Address Matched",
"floor-height": "",
"heating-cost-potential": "142",
"unheated-corridor-length": "",
"hot-water-cost-potential": "96",
"construction-age-band": "NO DATA!",
"potential-energy-rating": "B",
"mainheat-energy-eff": "Very Good",
"windows-env-eff": "Very Good",
"lighting-energy-eff": "Very Good",
"environment-impact-potential": "91",
"glazed-type": "NO DATA!",
"heating-cost-current": "142",
address3: "",
"mainheatcont-description":
"Charging system linked to use of community heating, TRVs",
"sheating-energy-eff": "N/A",
"property-type": "Flat",
"local-authority-label": "Hammersmith and Fulham",
"fixed-lighting-outlets-count": "1",
"energy-tariff": "standard tariff",
"mechanical-ventilation": "NO DATA!",
"hot-water-cost-current": "96",
county: "Greater London Authority",
postcode: "W6 9BF",
"solar-water-heating-flag": "",
constituency: "E14000726",
"co2-emissions-potential": "0.8",
"number-heated-rooms": "",
"floor-description": "Average thermal transmittance 0.19 W/m-¦K",
"energy-consumption-potential": "50",
"local-authority": "E09000013",
"built-form": "Mid-Terrace",
"number-open-fireplaces": "0",
"windows-description": "High performance glazing",
"glazed-area": "NO DATA!",
"inspection-date": "2017-04-05",
"mains-gas-flag": "",
"co2-emiss-curr-per-floor-area": "9",
address1: "53 Distillery Wharf",
"heat-loss-corridor": "NO DATA!",
"flat-storey-count": "",
"constituency-label": "Hammersmith",
"roof-energy-eff": "N/A",
"total-floor-area": "89.0",
"building-reference-number": "6013745278",
"environment-impact-current": "91",
"co2-emissions-current": "0.8",
"roof-description": "(other premises above)",
"floor-energy-eff": "Very Good",
"number-habitable-rooms": "",
address2: "Regatta Lane",
"hot-water-env-eff": "Very Good",
posttown: "LONDON",
"mainheatc-energy-eff": "Good",
"main-fuel": "NO DATA!",
"lighting-env-eff": "Very Good",
"windows-energy-eff": "Very Good",
"floor-env-eff": "Very Good",
"sheating-env-eff": "N/A",
"lighting-description": "Low energy lighting in all fixed outlets",
"roof-env-eff": "N/A",
"walls-energy-eff": "Very Good",
"photo-supply": "",
"lighting-cost-potential": "58",
"mainheat-env-eff": "Very Good",
"multi-glaze-proportion": "",
"main-heating-controls": "",
"lodgement-datetime": "2017-04-05 16:52:23",
"flat-top-storey": "",
"current-energy-rating": "B",
"secondheat-description": "None",
"walls-env-eff": "Very Good",
"transaction-type": "new dwelling",
uprn: "34154017",
"current-energy-efficiency": "86",
"energy-consumption-current": "50",
"mainheat-description": "Community scheme",
"lighting-cost-current": "58",
"lodgement-date": "2017-04-05",
"extension-count": "",
"mainheatc-env-eff": "Good",
"lmk-key": "117215937906201704320516522305558463",
"wind-turbine-count": "",
tenure: "unknown",
"floor-level": "mid floor",
"potential-energy-efficiency": "86",
"hot-water-energy-eff": "Very Good",
"low-energy-lighting": "100",
"walls-description": "Average thermal transmittance 0.16 W/m-¦K",
"hotwater-description": "Community scheme",
},
{
"low-energy-fixed-light-count": "1",
address: "10 Distillery Wharf, Regatta Lane",
"uprn-source": "Address Matched",
"floor-height": "",
"heating-cost-potential": "142",
"unheated-corridor-length": "",
"hot-water-cost-potential": "96",
"construction-age-band": "NO DATA!",
"potential-energy-rating": "B",
"mainheat-energy-eff": "Very Good",
"windows-env-eff": "Very Good",
"lighting-energy-eff": "Very Good",
"environment-impact-potential": "91",
"glazed-type": "NO DATA!",
"heating-cost-current": "142",
address3: "",
"mainheatcont-description":
"Charging system linked to use of community heating, TRVs",
"sheating-energy-eff": "N/A",
"property-type": "Flat",
"local-authority-label": "Hammersmith and Fulham",
"fixed-lighting-outlets-count": "1",
"energy-tariff": "standard tariff",
"mechanical-ventilation": "NO DATA!",
"hot-water-cost-current": "96",
county: "Greater London Authority",
postcode: "W6 9BF",
"solar-water-heating-flag": "",
constituency: "E14000726",
"co2-emissions-potential": "0.8",
"number-heated-rooms": "",
"floor-description": "Average thermal transmittance 0.19 W/m-¦K",
"energy-consumption-potential": "50",
"local-authority": "E09000013",
"built-form": "Mid-Terrace",
"number-open-fireplaces": "0",
"windows-description": "High performance glazing",
"glazed-area": "NO DATA!",
"inspection-date": "2017-04-05",
"mains-gas-flag": "",
"co2-emiss-curr-per-floor-area": "9",
address1: "53 Distillery Wharf",
"heat-loss-corridor": "NO DATA!",
"flat-storey-count": "",
"constituency-label": "Hammersmith",
"roof-energy-eff": "N/A",
"total-floor-area": "89.0",
"building-reference-number": "6013745278",
"environment-impact-current": "91",
"co2-emissions-current": "0.8",
"roof-description": "(other premises above)",
"floor-energy-eff": "Very Good",
"number-habitable-rooms": "",
address2: "Regatta Lane",
"hot-water-env-eff": "Very Good",
posttown: "LONDON",
"mainheatc-energy-eff": "Good",
"main-fuel": "NO DATA!",
"lighting-env-eff": "Very Good",
"windows-energy-eff": "Very Good",
"floor-env-eff": "Very Good",
"sheating-env-eff": "N/A",
"lighting-description": "Low energy lighting in all fixed outlets",
"roof-env-eff": "N/A",
"walls-energy-eff": "Very Good",
"photo-supply": "",
"lighting-cost-potential": "58",
"mainheat-env-eff": "Very Good",
"multi-glaze-proportion": "",
"main-heating-controls": "",
"lodgement-datetime": "2017-04-05 16:52:23",
"flat-top-storey": "",
"current-energy-rating": "B",
"secondheat-description": "None",
"walls-env-eff": "Very Good",
"transaction-type": "new dwelling",
uprn: "34154017",
"current-energy-efficiency": "86",
"energy-consumption-current": "50",
"mainheat-description": "Community scheme",
"lighting-cost-current": "58",
"lodgement-date": "2017-04-05",
"extension-count": "",
"mainheatc-env-eff": "Good",
"lmk-key": "11721593790620170405165223055583123",
"wind-turbine-count": "",
tenure: "unknown",
"floor-level": "mid floor",
"potential-energy-efficiency": "86",
"hot-water-energy-eff": "Very Good",
"low-energy-lighting": "100",
"walls-description": "Average thermal transmittance 0.16 W/m-¦K",
"hotwater-description": "Community scheme",
},
],
};
// Handle the response from the external API and return it
return NextResponse.json(data);

View file

@ -3,11 +3,55 @@
import { useState } from "react";
import SubmitButton from "../components/search/SubmitButton";
import { useRouter } from "next/navigation";
import { SearchData, SearchResult } from "@/types/epc";
const defaultToggleClass =
"mb-1 block max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700";
const toggledButtonClass =
"text-white mb-1 block max-w-sm rounded-lg border border-gray-200 bg-brandblue p-6 shadow hover:bg-hoverblue dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700";
interface ToggleAddressButtonProps {
rowKey: string;
setButtonDisabled: (value: boolean) => void;
setAddress: (value: string) => void;
setCurrentlyToggled: (value: string) => void;
toggleClassName: string;
address: string;
}
function ToggleAddressButton({
rowKey,
setButtonDisabled,
setAddress,
setCurrentlyToggled,
toggleClassName,
address,
}: ToggleAddressButtonProps) {
const handleOnClick = () => {
setCurrentlyToggled(rowKey);
setAddress(address);
setButtonDisabled(false);
};
return (
<li key={rowKey}>
<a onClick={handleOnClick} className={toggleClassName}>
{address}
</a>
</li>
);
}
export default function Search() {
const [postcode, setPostcode] = useState("");
const [buttonDisabled, setButtonDisabled] = useState(true);
const [data, setData] = useState(null);
const [data, setData] = useState<null | SearchData>(null);
const [address, setAddress] = useState("");
const [toggleDisabled, setToggleDisabled] = useState(true);
// Keep track of which lmk-key is currently toggled. Initially, none
// are toggled
const [currentlyToggled, setCurrentlyToggled] = useState("");
const router = useRouter();
// Do something better than logging the error
@ -34,13 +78,13 @@ export default function Search() {
return;
};
console.log(data);
async function fetchData() {
// TODO - add strict typing to the api response
try {
const response = await fetch(`/api/search?postcode=${postcode}`);
const data = await response.json();
setData(data.message);
const data = (await response.json()) as SearchData;
setData(data);
} catch (error) {
console.error("Error fetching data:", error);
}
@ -78,7 +122,32 @@ export default function Search() {
</div>
<SubmitButton {...submitProps} />
</form>
{data && <div>{JSON.stringify(data, null, 2)}</div>}
{data && (
<div>
<div className="flex justify-center mt-7">
Select the full address from the list
</div>
<ul className="mt-3 overflow-y-auto flex flex-col items-center">
{data.rows.map((row: SearchResult) => {
return (
<ToggleAddressButton
key={row["lmk-key"]}
rowKey={row["lmk-key"]}
setButtonDisabled={setToggleDisabled}
setAddress={setAddress}
setCurrentlyToggled={setCurrentlyToggled}
address={row.address}
toggleClassName={
currentlyToggled === row["lmk-key"]
? toggledButtonClass
: defaultToggleClass
}
/>
);
})}
</ul>
</div>
)}
</div>
</div>
);