Skip to content

Commit

Permalink
Fix answer filtering on area page
Browse files Browse the repository at this point in the history
  • Loading branch information
tewson committed Apr 4, 2024
1 parent 5fb0032 commit 9e715fc
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions src/pages/areas/[localAuthority]/[area].astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
import { type Record as AirtableRecord, type FieldSet } from "airtable";
import lodash from "lodash";
import {
Expand Down Expand Up @@ -39,53 +38,28 @@ export async function getStaticPaths() {
return electoralAreas.map((electoralAreaRecord) => {
const electoralAreaName = getStringFieldValue(electoralAreaRecord, "Name");
const candidatesAnswersForArea = answers.filter(
(answerRecord) =>
getLookupFieldValue(answerRecord, "Candidate area name") ===
electoralAreaName
);
const answersListByCandidate = candidatesAnswersForArea.reduce<
Record<string, AirtableRecord<FieldSet>[]>
>((acc, answerRecord) => {
const candidateFullName = getLookupFieldValue(
answerRecord,
"Candidate full name"
);
const candidateAnswers = candidatesAnswersForArea.filter(
(candidatesAnswersForAreaRecord) =>
getLookupFieldValue(
candidatesAnswersForAreaRecord,
"Candidate full name"
) === candidateFullName
);
return {
...acc,
[candidateFullName]: candidateAnswers,
};
}, {});
const questions = questionRecords
.filter(
(questionRecord) =>
getLookupFieldValue(questionRecord, "Local authority name") ===
getLookupFieldValue(electoralAreaRecord, "Local Authority Name")
)
.map((questionRecord) => getStringFieldValue(questionRecord, "Text"))
.map((question, index) => {
.map((questionRecord) => {
const answersForQuestion = answers.filter(
(answerRecord) =>
getLookupFieldValue(answerRecord, "Candidate area name") ===
electoralAreaName &&
answerRecord.get("Question")?.toString() === questionRecord.id
);
return {
text: question,
answers: candidatesAnswersForArea.map((answerRecord) => {
text: getStringFieldValue(questionRecord, "Text"),
answers: answersForQuestion.map((answerRecord) => {
const candidateFullName = getLookupFieldValue(
answerRecord,
"Candidate full name"
);
const answer =
answersListByCandidate[candidateFullName][index] &&
getStringFieldValue(
answersListByCandidate[candidateFullName][index],
"Text"
);
const answer = getStringFieldValue(answerRecord, "Text");
return {
candidateFullName,
answer,
Expand Down

0 comments on commit 9e715fc

Please sign in to comment.