fix(entry-state): detect draft and pending changes at the same time
This commit is contained in:
committed by
Benedikt Rötsch
parent
8abd754f02
commit
ff305e7d34
@@ -13,10 +13,13 @@ module.exports = async function attachEntryState (entry) {
|
||||
const publishedEntry = await getPublishedEntry(entry)
|
||||
entry.draft = false
|
||||
entry.pendingChanges = false
|
||||
|
||||
// If there is no published version, it is a draft and can't have pending changes
|
||||
if (!publishedEntry) {
|
||||
entry.draft = true
|
||||
return entry
|
||||
}
|
||||
|
||||
// We group fields of type link (i.e. Only objects/array) from the same entry in preview and delivery
|
||||
const entriesToCompare = Object.keys(entry.fields).map((key) => {
|
||||
const field = entry.fields[key]
|
||||
@@ -31,28 +34,31 @@ module.exports = async function attachEntryState (entry) {
|
||||
// If the field is an array of reference we need to check its item if they have pending changes
|
||||
if (isArray(originalItem)) {
|
||||
originalItem.forEach((innerItem, index) => {
|
||||
if (!isArray(innerItem) && isLinkDraft(innerItem, publishedItem[index])) {
|
||||
entry.pendingChanges = true
|
||||
if (!isArray(innerItem)) {
|
||||
setEntryState(entry, innerItem, publishedItem[index])
|
||||
return
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// If the field is a single reference we just check if it has pending changes
|
||||
} else if (isLinkDraft(item[0], item[1])) {
|
||||
entry.pendingChanges = true
|
||||
return entry
|
||||
setEntryState(entry, originalItem, publishedItem)
|
||||
}
|
||||
})
|
||||
// We check if the root element has pending changes
|
||||
if (entry && publishedEntry && (entry.sys.updatedAt !== publishedEntry.sys.updatedAt)) {
|
||||
entry.pendingChanges = true
|
||||
if (entry && publishedEntry) {
|
||||
setEntryState(entry, entry, publishedEntry)
|
||||
}
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
function isLinkDraft (previewLink, deliveryLink) {
|
||||
if (isObject(previewLink)) {
|
||||
return !previewLink.fields || !isDateEqual(previewLink.sys.updatedAt, previewLink.sys.updatedAt)
|
||||
return previewLink && !deliveryLink
|
||||
}
|
||||
|
||||
function isLinkPendingChanges (previewLink, deliveryLink) {
|
||||
if (previewLink && deliveryLink) {
|
||||
return !isDateEqual(previewLink.sys.updatedAt, deliveryLink.sys.updatedAt)
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -63,3 +69,12 @@ function isDateEqual (lhs, rhs) {
|
||||
return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0)
|
||||
}
|
||||
|
||||
function setEntryState (entry, originalItem, publishedItem) {
|
||||
if (isLinkDraft(originalItem, publishedItem)) {
|
||||
entry.draft = true
|
||||
}
|
||||
if (isLinkPendingChanges(originalItem, publishedItem)) {
|
||||
entry.pendingChanges = true
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user