refactor: Refactor draft check
This commit is contained in:
@@ -31,13 +31,13 @@ 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) && isObject(innerItem) && (!innerItem.fields || originalItem[index].sys.updatedAt !== publishedItem[index].sys.updatedAt)) {
|
||||
if (!isArray(innerItem) && isLinkDraft(innerItem, publishedItem[index])) {
|
||||
entry.pendingChanges = true
|
||||
return
|
||||
}
|
||||
})
|
||||
// If the field is a single reference we just check if it has pending changes
|
||||
} else if (isObject(item[0]) && (!item[0].fields || !isDateEqual(item[0].sys.updatedAt !== item[1].sys.updatedAt))) {
|
||||
} else if (isLinkDraft(item[0], item[1])) {
|
||||
entry.pendingChanges = true
|
||||
return
|
||||
}
|
||||
@@ -50,8 +50,13 @@ module.exports = async function attachEntryState (entry) {
|
||||
return entry
|
||||
}
|
||||
|
||||
function isLinkDraft (previewLink, deliveryLink) {
|
||||
return isObject(previewLink) && (!previewLink.fields || !isDateEqual(previewLink.sys.updatedAt !== previewLink.sys.updatedAt))
|
||||
}
|
||||
|
||||
function isDateEqual (lhs, rhs) {
|
||||
const lhsDate = new Date(lhs)
|
||||
const rhsDate = new Date(rhs)
|
||||
return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user