Skip to content

Commit

Permalink
🐛 fix wordpress authors display
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Oct 13, 2023
1 parent 86d2a00 commit 7c6e599
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions db/wpdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
uniqBy,
sortBy,
DataPageRelatedResearch,
isString,
OwidGdocType,
} from "@ourworldindata/utils"
import { Topic } from "@ourworldindata/grapher"
Expand Down Expand Up @@ -651,10 +652,21 @@ export const getRelatedChartsForVariable = async (
`)
}

interface RelatedResearchQueryResult {
linkTargetSlug: string
chartSlug: string
title: string
postSlug: string
chartId: number
authors: string
thumbnail: string
pageviews: number
post_source: string
}
export const getRelatedResearchAndWritingForVariable = async (
variableId: number
): Promise<DataPageRelatedResearch[]> => {
const wp_posts = await db.queryMysql(
const wp_posts: RelatedResearchQueryResult[] = await db.queryMysql(
`-- sql
select
distinct
Expand Down Expand Up @@ -691,7 +703,7 @@ export const getRelatedResearchAndWritingForVariable = async (
[variableId]
)

const gdocs_posts = await db.queryMysql(
const gdocs_posts: RelatedResearchQueryResult[] = await db.queryMysql(
`-- sql
select
distinct
Expand Down Expand Up @@ -725,17 +737,27 @@ export const getRelatedResearchAndWritingForVariable = async (
)

const combined = [...wp_posts, ...gdocs_posts]

// we could do the sorting in the SQL query if we'd union the two queries
// but it seemed easier to understand if we do the sort here
const sorted = sortBy(combined, (post) => -post.pageviews)

return sorted.map((post) => ({
title: post.title,
url: `/${post.postSlug}`,
variantName: "",
authors: JSON.parse(post.authors),
imageUrl: post.thumbnail,
}))
return sorted.map((post) => {
const parsedAuthors = JSON.parse(post.authors)
// The authors in the gdocs table are just a list of strings, but in the wp_posts table
// they are a list of objects with an "author" key and an "order" key. We want to normalize this so that
// we can just use the same code to display the authors in both cases.
const authors = parsedAuthors.map((author: any) =>
!isString(author) ? author.author : author
)
return {
title: post.title,
url: `/${post.postSlug}`,
variantName: "",
authors,
imageUrl: post.thumbnail,
}
})
}

export const getRelatedArticles = async (
Expand Down

0 comments on commit 7c6e599

Please sign in to comment.