Created
July 10, 2024 21:07
-
-
Save jimlambie/6b05625c07bd1a1e52cd97dbc56c6a50 to your computer and use it in GitHub Desktop.
function to extract event data from CMS item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
@jimlambie So this bit I need to add to the PromoPanel Component itself, so it can parse through the data and populate the item?
So this is how the panel is injected into the visits page as asked in the ticket