Skip to content

Commit

Permalink
Fix answer filtering on candidate page
Browse files Browse the repository at this point in the history
  • Loading branch information
tewson committed Apr 4, 2024
1 parent 9e715fc commit 504c27d
Showing 1 changed file with 15 additions and 50 deletions.
65 changes: 15 additions & 50 deletions src/pages/candidates/[candidate].astro
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getStaticPaths() {
})
.all();
const candidatesAnswers = await airtableClient("Answers")
const answerRecords = await airtableClient("Answers")
.select({
view: "Grid view",
})
Expand All @@ -40,63 +40,30 @@ export async function getStaticPaths() {
candidateRecord,
"Area local authority name"
);
const candidatesAnswersForArea = candidatesAnswers.filter(
(answerRecord) =>
getLookupFieldValue(answerRecord, "Candidate") === candidateRecord.id
);
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(candidateRecord, "Area local authority name")
)
.map((questionRecord) => getStringFieldValue(questionRecord, "Text"))
.map((question, index) => {
.map((questionRecord, index) => {
const answerRecord = answerRecords.filter((answerRecord) => {
return (
getLookupFieldValue(answerRecord, "Candidate full name") ===
candidateFullName &&
answerRecord.get("Question")?.toString() === questionRecord.id
);
})[0];
return {
text: question,
answers: candidatesAnswersForArea.map((answerRecord) => {
const candidateFullName = getLookupFieldValue(
answerRecord,
"Candidate full name"
);
const answer =
answersListByCandidate[candidateFullName][index] &&
getStringFieldValue(
answersListByCandidate[candidateFullName][index],
"Text"
);
return {
candidateFullName,
answer,
};
}),
text: getStringFieldValue(questionRecord, "Text"),
answer: answerRecord
? getStringFieldValue(answerRecord, "Text")
: undefined,
};
});
const hasAnswers = questions.some((question) =>
question.answers.some((answer) => Boolean(answer.answer))
);
const hasAnswers = questions.some((question) => Boolean(question.answer));
return {
params: {
Expand Down Expand Up @@ -140,9 +107,7 @@ const {
questions.map((question) => (
<>
<QuestionText>{question.text}</QuestionText>
{question.answers.map((answer) => (
<p class="pb-4">{answer.answer}</p>
))}
<p class="pb-4">{question.answer}</p>
</>
))
) : (
Expand Down

0 comments on commit 504c27d

Please sign in to comment.