From 504c27d8fd053f6ea609ce68ede66d18c0ebc905 Mon Sep 17 00:00:00 2001 From: Tewson Seeoun Date: Thu, 4 Apr 2024 03:09:19 +0100 Subject: [PATCH] Fix answer filtering on candidate page --- src/pages/candidates/[candidate].astro | 65 ++++++-------------------- 1 file changed, 15 insertions(+), 50 deletions(-) diff --git a/src/pages/candidates/[candidate].astro b/src/pages/candidates/[candidate].astro index 3ecdb82..1b847d9 100644 --- a/src/pages/candidates/[candidate].astro +++ b/src/pages/candidates/[candidate].astro @@ -27,7 +27,7 @@ export async function getStaticPaths() { }) .all(); - const candidatesAnswers = await airtableClient("Answers") + const answerRecords = await airtableClient("Answers") .select({ view: "Grid view", }) @@ -40,30 +40,6 @@ export async function getStaticPaths() { candidateRecord, "Area local authority name" ); - const candidatesAnswersForArea = candidatesAnswers.filter( - (answerRecord) => - getLookupFieldValue(answerRecord, "Candidate") === candidateRecord.id - ); - - const answersListByCandidate = candidatesAnswersForArea.reduce< - Record[]> - >((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( @@ -71,32 +47,23 @@ export async function getStaticPaths() { 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: { @@ -140,9 +107,7 @@ const { questions.map((question) => ( <> {question.text} - {question.answers.map((answer) => ( -

{answer.answer}

- ))} +

{question.answer}

)) ) : (