Skip to content

Commit

Permalink
adds ability to filter deaths by occupation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandersimoes committed Dec 27, 2024
1 parent 51e89df commit 9dbf3dc
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ async function getOccupation(occupationId) {
return Array.isArray(data) && data.length > 0 ? data[0] : {};
}

async function getPeopleDiedThisYear(yearNum, occupation) {
async function getPeopleDiedThisYear(yearNum) {
const res = await fetch(
`${BASE_API}/person?alive=is.false&deathdate=gte.01-01-${yearNum}&deathdate=lte.12-31-${yearNum}&occupation=eq.${occupation}&select=bplace_country(demonym),dplace_country(id,country,slug),dplace_geonameid(id,place,slug,lat,lon),occupation(*),occupation_id:occupation,name,slug,id,hpi,hpi_prev,gender,birthyear,birthdate,deathyear,deathdate,alive&order=deathdate.asc`,
`${BASE_API}/person?alive=is.false&deathdate=gte.01-01-${yearNum}&deathdate=lte.12-31-${yearNum}&select=bplace_country(demonym),dplace_country(id,country,slug),dplace_geonameid(id,place,slug,lat,lon),occupation(*),occupation_id:occupation,name,slug,id,hpi,hpi_prev,gender,birthyear,birthdate,deathyear,deathdate,alive&order=deathdate.asc`,
{
next: {revalidate: REVALIDATE_PERIODS.DEFAULT},
}
Expand Down Expand Up @@ -61,9 +61,9 @@ export default async function Page({params: {id: year, occupationId}}) {

const occupation = await getOccupation(occupationId);

const peopleDiedThisYear = await getPeopleDiedThisYear(
yearNum,
occupation.id
const peopleDiedThisYear = await getPeopleDiedThisYear(yearNum);
const peopleDiedThisYearFiltered = peopleDiedThisYear.filter(
person => person.occupation_id === occupation.id
);

const sections = [
Expand All @@ -74,7 +74,7 @@ export default async function Page({params: {id: year, occupationId}}) {
<TopPeople
occupation={occupation}
year={year}
people={peopleDiedThisYear}
people={peopleDiedThisYearFiltered}
/>
),
},
Expand All @@ -85,15 +85,19 @@ export default async function Page({params: {id: year, occupationId}}) {
<DeathsByMonth
occupation={occupation}
year={year}
people={peopleDiedThisYear}
people={peopleDiedThisYearFiltered}
/>
),
},
];

return (
<div className="person">
<Header occupation={occupation} year={year} people={peopleDiedThisYear} />
<Header
occupation={occupation}
year={year}
people={peopleDiedThisYearFiltered}
/>
<div className="about-section">
<ProfileNav sections={sections} />
<Intro
Expand Down
35 changes: 34 additions & 1 deletion components/common/Intro.css
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,37 @@
.year-navigation-link {
@mixin bodyLink;
color: #666;
}
}

.occupation-filter {
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
margin: 2rem 0;
}

.occupation-filter select {
padding: 0.5rem 1rem;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 1rem;
min-width: 200px;
background-color: white;
cursor: pointer;
}

.occupation-filter label {
font-weight: 500;
color: var(--colorMatza);
}

.occupation-filter select:hover {
border-color: #999;
}

.occupation-filter select:focus {
outline: none;
border-color: #666;
box-shadow: 0 0 0 2px rgba(0,0,0,0.1);
}
3 changes: 3 additions & 0 deletions components/deaths/DeathsByMonth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export default async function DeathsByMonth({year, people}) {
{months.map((monthName, index) => {
const month = index + 1;
const peopleInMonth = groupedByMonth[month] || [];
if (peopleInMonth.length === 0) {
return null;
}
return (
<div key={monthName} className="month-section">
<h3>
Expand Down
56 changes: 36 additions & 20 deletions components/deaths/Intro.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
"use client";

import AnchorList from "../utils/AnchorList";
import {plural} from "pluralize";
import {toTitleCase} from "../utils/vizHelpers";
import {FORMATTERS} from "../utils/consts";
import "../common/Intro.css";
import {useRouter} from "next/navigation";

export default function Intro({year, people, occupation}) {
const peopleSortedByHPI = people.sort((a, b) => b.hpi - a.hpi);
const countryBornCounts = people.reduce((acc, person) => {
if (person.dplace_country) {
const countryId = person.dplace_country.id;
if (!acc[countryId]) {
acc[countryId] = {
count: 0,
country: person.dplace_country,
};
}
acc[countryId].count++;
}
return acc;
}, {});
const router = useRouter();
const peopleSortedByHPI = people
.filter(person =>
occupation ? person.occupation.id === occupation.id : true
)
.sort((a, b) => b.hpi - a.hpi);

const occupationCounts = people.reduce((acc, person) => {
if (person.occupation) {
const occupationId = person.occupation.id;
Expand All @@ -33,11 +28,7 @@ export default function Intro({year, people, occupation}) {
return acc;
}, []);

const topCountries = Object.values(countryBornCounts)
.sort((a, b) => b.count - a.count)
.slice(0, 3);

const cityDiedCounts = people.reduce((acc, person) => {
const cityDiedCounts = peopleSortedByHPI.reduce((acc, person) => {
if (person.dplace_geonameid) {
const cityId = person.dplace_geonameid.id;
if (!acc[cityId]) {
Expand Down Expand Up @@ -130,6 +121,31 @@ export default function Intro({year, people, occupation}) {
</p>
</div>
</div>
<div className="occupation-filter">
<label htmlFor="occupation-select">Filter by Occupation: </label>
<select
id="occupation-select"
onChange={e => {
const path = e.target.value
? `/profile/deaths/${year}/occupation/${e.target.value}`
: `/profile/deaths/${year}`;
router.push(path);
}}
value={occupation?.occupation_slug || ""}
>
<option value="">All Occupations</option>
{Object.values(occupationCounts)
.sort((a, b) => b.count - a.count)
.map(({occupation, count}) => (
<option
key={occupation.occupation_slug}
value={occupation.occupation_slug}
>
{toTitleCase(occupation.occupation)} ({count})
</option>
))}
</select>
</div>
<div className="year-navigation">
<div>
<a
Expand Down
16 changes: 12 additions & 4 deletions components/deaths/PeopleGrid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import dayjs from "dayjs";
import advancedFormat from "dayjs/plugin/advancedFormat";
import {toTitleCase} from "../utils/vizHelpers";
import Link from "next/link";
import {plural} from "pluralize";

dayjs.extend(advancedFormat);

const PeopleGrid = ({bios}) => (
const PeopleGrid = ({bios, occupation, year}) => (
<>
<div className="people-grid">
{bios.map(profile => (
Expand Down Expand Up @@ -47,9 +48,16 @@ const PeopleGrid = ({bios}) => (
))}
</div>
<div className="view-more-link">
<Link href="/explore/rankings?show=people&years=2024,2024&yearType=deathyear">
View Full List of 2024 Deaths Ranked by HPI →
</Link>
{occupation ? (
<Link href="/explore/rankings?show=people&years=2024,2024&yearType=deathyear">
View Full List of {plural(toTitleCase(occupation.occupation))} that
died in {year}
</Link>
) : (
<Link href="/explore/rankings?show=people&years=2024,2024&yearType=deathyear">
View Full List of {year} Deaths Ranked by HPI
</Link>
)}
</div>
</>
);
Expand Down

0 comments on commit 9dbf3dc

Please sign in to comment.