diff --git a/src/pages/areas/[localAuthority]/[area].astro b/src/pages/areas/[localAuthority]/[area].astro index 89b79c8..63b62d9 100644 --- a/src/pages/areas/[localAuthority]/[area].astro +++ b/src/pages/areas/[localAuthority]/[area].astro @@ -20,16 +20,12 @@ export async function getStaticPaths() { }) .all(); - const questions = await airtableClient("Questions") + const questionRecords = await airtableClient("Questions") .select({ view: "Grid view", }) .all(); - const questionsList = questions.map((record) => - getStringFieldValue(record, "Text") - ); - const answers = await airtableClient("Answers") .select({ view: "Grid view", @@ -64,27 +60,34 @@ export async function getStaticPaths() { }; }, {}); - const questions = questionsList.map((question, index) => { - return { - text: question, - answers: candidatesAnswersForArea.map((answerRecord) => { - const candidateFullName = getLookupFieldValue( - answerRecord, - "Candidate full name" - ); - const answer = - answersListByCandidate[candidateFullName][index] && - getStringFieldValue( - answersListByCandidate[candidateFullName][index], - "Text" + const questions = questionRecords + .filter( + (questionRecord) => + getLookupFieldValue(questionRecord, "Local authority name") === + getLookupFieldValue(electoralAreaRecord, "Local Authority Name") + ) + .map((questionRecord) => getStringFieldValue(questionRecord, "Text")) + .map((question, index) => { + return { + text: question, + answers: candidatesAnswersForArea.map((answerRecord) => { + const candidateFullName = getLookupFieldValue( + answerRecord, + "Candidate full name" ); - return { - candidateFullName, - answer, - }; - }), - }; - }); + const answer = + answersListByCandidate[candidateFullName][index] && + getStringFieldValue( + answersListByCandidate[candidateFullName][index], + "Text" + ); + return { + candidateFullName, + answer, + }; + }), + }; + }); const hasAnswers = questions.some((question) => question.answers.some((answer) => Boolean(answer.answer)) diff --git a/src/pages/candidates/[candidate].astro b/src/pages/candidates/[candidate].astro index 65bed01..3ecdb82 100644 --- a/src/pages/candidates/[candidate].astro +++ b/src/pages/candidates/[candidate].astro @@ -21,16 +21,12 @@ export async function getStaticPaths() { }) .all(); - const questions = await airtableClient("Questions") + const questionRecords = await airtableClient("Questions") .select({ view: "Grid view", }) .all(); - const questionsList = questions.map((record) => - getStringFieldValue(record, "Text") - ); - const candidatesAnswers = await airtableClient("Answers") .select({ view: "Grid view", @@ -69,27 +65,34 @@ export async function getStaticPaths() { }; }, {}); - const questions = questionsList.map((question, index) => { - return { - text: question, - answers: candidatesAnswersForArea.map((answerRecord) => { - const candidateFullName = getLookupFieldValue( - answerRecord, - "Candidate full name" - ); - const answer = - answersListByCandidate[candidateFullName][index] && - getStringFieldValue( - answersListByCandidate[candidateFullName][index], - "Text" + const questions = questionRecords + .filter( + (questionRecord) => + getLookupFieldValue(questionRecord, "Local authority name") === + getLookupFieldValue(candidateRecord, "Area local authority name") + ) + .map((questionRecord) => getStringFieldValue(questionRecord, "Text")) + .map((question, index) => { + return { + text: question, + answers: candidatesAnswersForArea.map((answerRecord) => { + const candidateFullName = getLookupFieldValue( + answerRecord, + "Candidate full name" ); - return { - candidateFullName, - answer, - }; - }), - }; - }); + const answer = + answersListByCandidate[candidateFullName][index] && + getStringFieldValue( + answersListByCandidate[candidateFullName][index], + "Text" + ); + return { + candidateFullName, + answer, + }; + }), + }; + }); const hasAnswers = questions.some((question) => question.answers.some((answer) => Boolean(answer.answer))