Skip to content

Commit

Permalink
District API route - Request electroral district data and cache the d…
Browse files Browse the repository at this point in the history
…ata #74 Add DistrictCard component #73
  • Loading branch information
Scobiform committed Oct 18, 2024
1 parent 533c668 commit d001d34
Show file tree
Hide file tree
Showing 7 changed files with 156,181 additions and 87 deletions.
1 change: 0 additions & 1 deletion src/components/Card/DistrictCard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const renderExternalDistrictData = (data) => {
console.log(data);
const renderValue = (key, value) => {
// Handle null or undefined values
if (value === null || value === undefined) {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Map/DynamicMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ const DynamicMap = ({ polygons = [],
setSelectedMap(event.target.value);
};

const fetchDistrictData = async (code) => {
const fetchDistrictData = async (code, state) => {
try {
const response = await fetch(`/api/district?code=${code}`);
const response = await fetch(`/api/district?code=${code}&state=${state}`);
if (!response.ok) {
throw new Error('Failed to fetch data');
}
Expand All @@ -118,14 +118,14 @@ const DynamicMap = ({ polygons = [],
}
};

const onEachFeature = (feature, layer) => {
const onEachFeature = (feature, layer, state) => {
// Check if feature has a code (for fetching data) or use its properties directly
if (feature.properties.code) {
// Add a click event listener to the layer
layer.on('click', async (e) => {
try {
// Fetch district data asynchronously
const districtData = await fetchDistrictData(feature.properties.code);
const districtData = await fetchDistrictData(feature.properties.code, state);

// Render the DistrictCard component with the fetched data
const popupContent = ReactDOMServer.renderToString(
Expand Down Expand Up @@ -285,11 +285,11 @@ const DynamicMap = ({ polygons = [],
/>
<GeoJSON data={hessenGeoData}
style={() => ({ color: 'green', weight: 1, fillColor: 'green', fillOpacity: 0.1 })}
onEachFeature={onEachFeature}
onEachFeature={(feature, layer) => onEachFeature(feature, layer, 'hessen')}
/>
<GeoJSON data={rheinlandPfalzGeoData}
style={() => ({ color: 'green', weight: 1, fillColor: 'green', fillOpacity: 0.1 })}
onEachFeature={onEachFeature}
onEachFeature={(feature, layer) => onEachFeature(feature, layer, 'rlp')}
/>
</>
)}
Expand Down
62 changes: 62 additions & 0 deletions src/data/hesse/geo - Kopie.json

Large diffs are not rendered by default.

156,114 changes: 156,053 additions & 61 deletions src/data/hesse/geo.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/data/hesse/getBezeichnung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json

# Load wk-vec-tree.json
with open('wk-vec-tree.json', 'r', encoding='utf-8') as wk_vec_file:
wk_vec_data = json.load(wk_vec_file)

# Load geo.json
with open('geo.json', 'r', encoding='utf-8') as geo_file:
geo_data = json.load(geo_file)

# Loop through the features in geo.json
for feature in geo_data['features']:
lwk = int(feature['properties']['LWK']) # Get LWK value, convert to int
index = lwk # Adjust LWK to 0-based index

# Check if the index is valid for wk-vec-tree.json
if 0 <= index < len(wk_vec_data):
bezeichnung = wk_vec_data[index].get('bezeichnung')
# Add the "code" key with the corresponding bezeichnung
feature['properties']['code'] = bezeichnung

# Write the updated geo.json back to a file
with open('geo.json', 'w', encoding='utf-8') as updated_geo_file:
json.dump(geo_data, updated_geo_file, ensure_ascii=False, indent=4)

print("geo.json updated successfully with 'code' from wk-vec-tree.json!")
1 change: 1 addition & 0 deletions src/data/hesse/wk-vec-tree.json

Large diffs are not rendered by default.

52 changes: 33 additions & 19 deletions src/pages/api/district.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
// pages/api/district.js

export default async function handler(req, res) {
const { code } = req.query; // Get the district code from the query string

if (!code) {
return res.status(400).json({ error: "District code is required" });
}

const fetchResponse = async (url, res) => {
try {
const response = await fetch(`https://wahlen.rlp-ltw-2021.23degrees.eu/assets/json/${code}.json`);

if (!response.ok) {
throw new Error('Failed to fetch data');
const response = await fetch(url);

if (!response.ok) {
throw new Error('Failed to fetch data');
}

const data = await response.json();
res.status(200).json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}

const data = await response.json();
res.status(200).json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}

const fetchData = async (code, state, res) => {
const stateStruc = {
'rlp': `https://wahlen.rlp-ltw-2021.23degrees.eu/assets/json/${code}.json`,
'hessen': `https://wahlen.hessen-ltw23.23degrees.eu/assets/json/${code}.json`
}
}

switch(state) {
case 'rlp':
return fetchResponse(stateStruc[state], res);
case 'hessen':
return fetchResponse(stateStruc[state], res);
default:
return stateStruc['rlp'];
}
};

export default async function handler(req, res) {
const { code } = req.query;
const { state } = req.query;
const data = await fetchData(code, state, res);
}

0 comments on commit d001d34

Please sign in to comment.