From c98d28cc00327e12e6e99dabdb59f0575c59fc17 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Sun, 28 May 2023 08:44:32 +0100 Subject: [PATCH] working on addresses page, turned on serverActions --- src/app/addresses/api.tsx | 22 ++++++++ src/app/addresses/page.tsx | 91 +++++++++++++++++++++++++++++++ src/app/addresses/types.tsx | 105 ++++++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 src/app/addresses/api.tsx create mode 100644 src/app/addresses/page.tsx create mode 100644 src/app/addresses/types.tsx diff --git a/src/app/addresses/api.tsx b/src/app/addresses/api.tsx new file mode 100644 index 0000000..e25df57 --- /dev/null +++ b/src/app/addresses/api.tsx @@ -0,0 +1,22 @@ +"use server"; + +import type { SearchData } from "./types"; + +export async function fetchAddresses(postcode: string) { + let url: string; + url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`; + + const headers = new Headers({ + Accept: "application/json", + Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`, + }); + + const request = await fetch(url, { + headers: headers, + next: { revalidate: 60 }, + }); + // Explicitly assert typing of the response + const data = (await request.json()) as SearchData; + + return data; +} diff --git a/src/app/addresses/page.tsx b/src/app/addresses/page.tsx new file mode 100644 index 0000000..571fcb2 --- /dev/null +++ b/src/app/addresses/page.tsx @@ -0,0 +1,91 @@ +"use client"; + +import { ReactElement } from "react"; +import { useRouter, useSearchParams } from "next/navigation"; +import type { SearchData, SearchResult } from "./types"; +import SubmitButton from "../components/search/SubmitButton"; +import { fetchAddresses } from "./api"; + +interface ToggleAddressButtonProps { + rowKey: string; + setButtonDisabled: (value: boolean) => void; + setAddress: (value: string) => void; + setCurrentlyToggled: (value: string) => void; + toggleClassName: string; + address: string; +} + +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-purple-500 p-6 shadow hover:bg-purple-500 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700"; + +const ToggleAddressButton = ({ + rowKey, + setButtonDisabled, + setAddress, + setCurrentlyToggled, + toggleClassName, + address, +}: ToggleAddressButtonProps) => { + const handleOnClick = () => { + setCurrentlyToggled(rowKey); + setAddress(address); + setButtonDisabled(false); + }; + + return ( +
  • + + {address} + +
  • + ); +}; + +export default async function Addresses() { + const searchParams = useSearchParams(); + // console.log("HELLo"); + // console.log(searchParams); + + // const postcode = searchParams.get("postcode") ?? ""; + + // const addresses = await fetchAddresses(postcode); + + // function redirectToEpc() { + // const res = data.rows.find( + // (row: SearchResult) => row["lmk-key"] === currentlyToggled + // ); + + // // router + // // .push({ + // // pathname: "/epc", + // // query: { + // // address: address, + // // res: encodeURIComponent(JSON.stringify(res)), + // // }, + // // }) + // // .catch((error) => console.log(error)); + // } + + function buttonDisabled() { + return true; + } + function redirectToEpc() { + return; + } + + return ( + <> +
    +

    Select your address

    + + {/* */} +
    + + ); +} diff --git a/src/app/addresses/types.tsx b/src/app/addresses/types.tsx new file mode 100644 index 0000000..650c737 --- /dev/null +++ b/src/app/addresses/types.tsx @@ -0,0 +1,105 @@ +type SearchData = { + "columns-names": string[]; + rows: SearchResult[]; +}; + +type SearchResult = { + "low-energy-fixed-light-count": string; + address: string; + "uprn-source": string; + "floor-height": string; + "heating-cost-potential": string; + "unheated-corridor-length": string; + "hot-water-cost-potential": string; + "construction-age-band": string; + "potential-energy-rating": string; + "mainheat-energy-eff": string; + "windows-env-eff": string; + "lighting-energy-eff": string; + "environment-impact-potential": string; + "glazed-type": string; + "heating-cost-current": string; + address3: string; + "mainheatcont-description": string; + "sheating-energy-eff": string; + "property-type": string; + "local-authority-label": string; + "fixed-lighting-outlets-count": string; + "energy-tariff": string; + "mechanical-ventilation": string; + "hot-water-cost-current": string; + county: string; + postcode: string; + "solar-water-heating-flag": string; + constituency: string; + "co2-emissions-potential": string; + "number-heated-rooms": string; + "floor-description": string; + "energy-consumption-potential": string; + "local-authority": string; + "built-form": string; + "number-open-fireplaces": string; + "windows-description": string; + "glazed-area": string; + "inspection-date": string; + "mains-gas-flag": string; + "co2-emiss-curr-per-floor-area": string; + address1: string; + "heat-loss-corridor": string; + "flat-storey-count": string; + "constituency-label": string; + "roof-energy-eff": string; + "total-floor-area": string; + "building-reference-number": string; + "environment-impact-current": string; + "co2-emissions-current": string; + "roof-description": string; + "floor-energy-eff": string; + "number-habitable-rooms": string; + address2: string; + "hot-water-env-eff": string; + posttown: string; + "mainheatc-energy-eff": string; + "main-fuel": string; + "lighting-env-eff": string; + "windows-energy-eff": string; + "floor-env-eff": string; + "sheating-env-eff": string; + "lighting-description": string; + "roof-env-eff": string; + "walls-energy-eff": string; + "photo-supply": string; + "lighting-cost-potential": string; + "mainheat-env-eff": string; + "multi-glaze-proportion": string; + "main-heating-controls": string; + "lodgement-datetime": string; + "flat-top-storey": string; + "current-energy-rating": string; + "secondheat-description": string; + "walls-env-eff": string; + "transaction-type": string; + uprn: string; + "current-energy-efficiency": string; + "energy-consumption-current": string; + "mainheat-description": string; + "lighting-cost-current": string; + "lodgement-date": string; + "extension-count": string; + "mainheatc-env-eff": string; + "lmk-key": string; + "wind-turbine-count": string; + tenure: string; + "floor-level": string; + "potential-energy-efficiency": string; + "hot-water-energy-eff": string; + "low-energy-lighting": string; + "walls-description": string; + "hotwater-description": string; +}; + +interface EpcDataProps { + data: SearchData; +} + +export type { SearchData, SearchResult, EpcDataProps };