Skip to content

Commit

Permalink
Merge pull request #40 from govbins/councilName-all-the-bins
Browse files Browse the repository at this point in the history
council name all the bins
  • Loading branch information
tommyp committed Mar 14, 2021
2 parents 997e1b1 + 38e9664 commit 4a85dc3
Show file tree
Hide file tree
Showing 2 changed files with 532 additions and 330 deletions.
64 changes: 0 additions & 64 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,15 @@
import fetch from "node-fetch";
import data from "../src/bins";
import Bin from "../components/bin";
import RetroBin from "../components/retroBin";
import React from "react";
import Footer from "../components/footer";
import moment from "moment"

const engAuthorityCodes = async () => {
return await fetchCodes(
"https://local-authority-eng.register.gov.uk/records.json?page-size=500"
);
};

const welshAuthorityCodes = async () => {
return await fetchCodes(
"https://principal-local-authority.register.gov.uk/records.json?page-size=500"
);
};

const scotAuthorityCodes = async () => {
return await fetchCodes(
"https://local-authority-sct.register.gov.uk/records.json?page-size=500"
);
};

const niAuthorityCodes = async () => {
return await fetchCodes(
"https://local-authority-nir.register.gov.uk/records.json?page-size=500"
);
};

const fetchCodes = async (url) => {
let codes = {};

try {
const response = await fetch(url);
const results = await response.json();
for (var localAuthorityCode in results) {
codes[localAuthorityCode] =
results[localAuthorityCode]["item"][0]["name"];
}
} catch (e) {
return null;
}

return codes;
};

export async function getStaticProps() {
const eng = await engAuthorityCodes();
const wales = await welshAuthorityCodes();
const scot = await scotAuthorityCodes();
const ni = await niAuthorityCodes();

const councilName = (bin) => {
const { localAuthorityCountry, localAuthorityCode } = bin
switch (localAuthorityCountry) {
case "eng":
return eng[localAuthorityCode];
case "sct":
return scot[localAuthorityCode];
case "wls":
return wales[localAuthorityCode];
case "ni":
return ni[localAuthorityCode];
default:
return "";
}
}

return {
props: {
bins: data.bins.map((bin) => {
bin.fileName = `${process.env.NEXT_PUBLIC_ASSET_ROOT}${bin.fileName}`
bin.councilName = bin.councilName || councilName(bin)
if (bin.presentTwinFileName) {
bin.presentTwinFileName = `${process.env.NEXT_PUBLIC_ASSET_ROOT}${bin.presentTwinFileName}`
}
Expand Down

0 comments on commit 4a85dc3

Please sign in to comment.