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
}
}
@deanerfree
Copy link

deanerfree commented Jul 10, 2024

@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

<Layout
        allowWelcomeMat
        category={tags[0]}
        cookies={cookies}
        tags={tags}
        baseUrl={`/${tags[0]}`}
      >
        {tags[0] === 'visit' && (
          <div className='w-full h-full my-2 overflow-hidden rounded-[4px]'>
            <PromoPanel />
          </div>
        )}
        <Feed
          category={tags[0]}
          filter={{ 'tags.value': tags[tags.length - 1] }}
          flush={flush === '1'}
          onPrepareData={data => prepareData(data)}
        />
      </Layout>
      ```
      
      Do you still feel like this needs to be changed?

@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