Skip to content

Instantly share code, notes, and snippets.

@jimlambie
Created July 10, 2024 21:07
Show Gist options
  • Save jimlambie/6b05625c07bd1a1e52cd97dbc56c6a50 to your computer and use it in GitHub Desktop.
Save jimlambie/6b05625c07bd1a1e52cd97dbc56c6a50 to your computer and use it in GitHub Desktop.
function to extract event data from CMS item
const PostUtils = require('./path/to/lib/post.js')
const prepareEventData = contentItem => {
try {
const postUtils = new PostUtils(contentItem)
const event = contentItem && {
...contentItem,
...postUtils.combinePostSections()
}
// Find the "data" bodyText component, which is used to control date, link, etc
const dataSection = (event.bodyTextSections || []).find(section => {
if (section.body && section.body.indexOf('Date:') > -1) {
return section
} else {
return false
}
})
const data = {}
const lines = dataSection ? dataSection.body.split('\n\n') : []
lines.forEach(line => {
const lineParts = [
line.substring(0, line.indexOf(':')),
line.substring(line.indexOf(':') + 1)
]
data[lineParts[0].trim().toLowerCase()] = lineParts[1].trim()
if (lineParts[1].trim().toLowerCase() === 'public') {
data.isPublic = true
}
if (lineParts[1].trim().toLowerCase() === 'external') {
data.isExternal = true
}
if (lineParts[0].trim().toLowerCase() === 'ticket info') {
data.ticketInfo = lineParts[1].trim()
}
if (lineParts[0].trim().toLowerCase() === 'link') {
data.link = lineParts[1].trim()
}
if (lineParts[0].trim().toLowerCase() === 'link text') {
data.linkText = lineParts[1].trim()
}
if (lineParts[0].trim().toLowerCase() === 'event id') {
data.eventId = lineParts[1].trim()
}
})
const item = {
title: event.title,
slug: event.slug,
subtitle: event.subtitleSection?.content,
date: data.date,
link: data.link,
linkText: data.linkText,
img: getImageUrl(event.primaryImage),
isExternal: data.isExternal,
isPublic: data.isPublic,
ticketInfo: data.ticketInfo,
eventId: data.eventId
}
return item
} catch (error) {
console.log('Error parsing event :> ', error)
return null
}
}
@jimlambie
Copy link
Author

looks normal enough.

i'd probably put the function above into a lib/utils file or similar, rather than in the component. then when you have fetched the data, you can pass it into the above function and get back an object with the right properties for the promo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment