Published or Draft?

Thanks for the context around what you are trying to do. I’ll certainly share your feedback with our product team and see if that might be something they’d add.

In the meantime you can use a small variation of what I suggested above to achieve the same result. I don’t know how exactly you are querying Contentful for the content on the example page you shared, but essentially you get a list of entries from the Preview API and make a list of the entry IDs and then query the Delivery API for entries where the sys.id is in that list (e.g., /entries?sys.id[in]=2lBPK3dEm84YOGIGUkEAi,1tnuVuJ6KUk84GMGGKoo68,1jteZMOi8eIoAQQWGOwIQy). The entry IDs that are only in the preview list are draft, ones that are in both lists and the sys.updatedAt property is the same are published and ones that are in both lists and the sys.updatedAt property is different are updated.

Here is a little javascript function I wrote to put a sys.status property on the list of entries:

function setEntryStatus(response) {
  let cpa_entries = response.items
  const cda_client = contentful.createClient({
    space: 'process.env.SPACE_ID',
    accessToken: 'process.env.CDA_TOKEN'
  })
  let cpa_ids = cpa_entries.reduce((a, c) => `${a}${a ? ',' : ''}${c.sys.id}`, '')
  return cda_client.getEntries({'sys.id[in]': cpa_ids}).then(response => {
    let cda_entries = response.items
    for (entry of cpa_entries) {
      let cda_entry = cda_entries.filter(i => i.sys.id == entry.sys.id)[0]
      if (cda_entry) {
        if (cda_entry.sys.updatedAt == entry.sys.updatedAt) {
          entry.sys.status = 'published'
        } else if (cda_entry.sys.updatedAt != entry.sys.updatedAt) {
          entry.sys.status = 'updated'
        }
      } else {
        entry.sys.status = 'draft'
      }
    }
    return cpa_entries
  })
}

Then you can use it like so:

var entries = []
preview_client.getEntries().then(setEntryStatus).then((items) => { 
  entries = items; 
  // now each entry will have a sys.status property
})

This would result in one extra CDA call but then you’d have a sys.status property that could determine the visibility of the edit icons.

hope this helps!

1 Like