Skip to content

Commit 7756fdc

Browse files
author
msj
committed
Revert "Remove code blocks for challenge"
This reverts commit 7e088b8.
1 parent 7e088b8 commit 7756fdc

2 files changed

Lines changed: 32 additions & 38 deletions

File tree

map/serializers.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,8 @@ class Meta:
1111
num_permits = serializers.SerializerMethodField()
1212

1313
def get_num_permits(self, obj):
14-
"""
15-
TODO: supplement each community area object with the number
16-
of permits issued in the given year.
14+
year = self.context.get("year", 2026)
1715

18-
e.g. The endpoint /map-data/?year=2017 should return something like:
19-
[
20-
{
21-
"ROGERS PARK": {
22-
area_id: 17,
23-
num_permits: 2
24-
},
25-
"BEVERLY": {
26-
area_id: 72,
27-
num_permits: 2
28-
},
29-
...
30-
}
31-
]
32-
"""
33-
34-
pass
16+
return RestaurantPermit.objects.filter(
17+
community_area_id=str(obj.area_id), issue_date__year=year
18+
).count()

map/static/js/RestaurantPermitMap.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,37 @@ export default function RestaurantPermitMap() {
4040
const [currentYearData, setCurrentYearData] = useState([])
4141
const [year, setYear] = useState(2026)
4242

43-
const yearlyDataEndpoint = `/map-data/?year=${year}`
43+
const currentUrl = `/map-data/?year=${year}`
4444

45+
const totalPermits = currentYearData.reduce(
46+
(acc, curr) => acc + curr.num_permits,
47+
0
48+
)
49+
const maxNumPermits = Math.max(...currentYearData.map((c) => c.num_permits))
50+
51+
const communityAreaDict = Object.fromEntries(
52+
currentYearData.map((d) => [d.name, d])
53+
)
54+
55+
// Get values for community areas
4556
useEffect(() => {
46-
fetch()
57+
fetch(currentUrl)
4758
.then((res) => res.json())
48-
.then((data) => {
49-
/**
50-
* TODO: Fetch the data needed to supply to map with data
51-
*/
52-
})
53-
}, [yearlyDataEndpoint])
59+
.then((data) => setCurrentYearData(data))
60+
}, [currentUrl])
5461

5562
function setAreaInteraction(feature, layer) {
56-
/**
57-
* TODO: use the methods below to:
58-
* 1) shade each area according to how many permits it had in a year
59-
* 2) display a popup with area details during user interaction
60-
*/
61-
layer.setStyle()
62-
layer.on("", () => {
63-
layer.bindPopup("")
63+
const thisAreaData = communityAreaDict[feature.properties.community],
64+
numPermits = thisAreaData.num_permits,
65+
pcntPermits = numPermits / maxNumPermits
66+
layer.setStyle({
67+
fillOpacity: pcntPermits,
68+
})
69+
layer.on("mouseover", () => {
70+
layer.bindPopup(`
71+
<h3>${feature.properties.community}</h3>
72+
<p>Restaurant permits issued during ${year}: ${thisAreaData.num_permits}</p>
73+
`)
6474
layer.openPopup()
6575
})
6676
}
@@ -69,11 +79,11 @@ export default function RestaurantPermitMap() {
6979
<>
7080
<YearSelect filterVal={year} setFilterVal={setYear} />
7181
<p className="fs-4">
72-
Restaurant permits issued this year: {/* TODO: display this value */}
82+
Restaurant permits issued this year: {totalPermits || "0"}
7383
</p>
7484
<p className="fs-4">
7585
Maximum number of restaurant permits in a single area:
76-
{/* TODO: display this value */}
86+
{" " + (maxNumPermits || "0")}
7787
</p>
7888
<MapContainer
7989
id="restaurant-map"

0 commit comments

Comments
 (0)