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)
|
const publishedEntry = await getPublishedEntry(entry)
|
||||||
entry.draft = false
|
entry.draft = false
|
||||||
entry.pendingChanges = false
|
entry.pendingChanges = false
|
||||||
|
|
||||||
|
// If there is no published version, it is a draft and can't have pending changes
|
||||||
if (!publishedEntry) {
|
if (!publishedEntry) {
|
||||||
entry.draft = true
|
entry.draft = true
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
// We group fields of type link (i.e. Only objects/array) from the same entry in preview and delivery
|
// 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 entriesToCompare = Object.keys(entry.fields).map((key) => {
|
||||||
const field = entry.fields[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 the field is an array of reference we need to check its item if they have pending changes
|
||||||
if (isArray(originalItem)) {
|
if (isArray(originalItem)) {
|
||||||
originalItem.forEach((innerItem, index) => {
|
originalItem.forEach((innerItem, index) => {
|
||||||
if (!isArray(innerItem) && isLinkDraft(innerItem, publishedItem[index])) {
|
if (!isArray(innerItem)) {
|
||||||
entry.pendingChanges = true
|
setEntryState(entry, innerItem, publishedItem[index])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
// If the field is a single reference we just check if it has pending changes
|
// If the field is a single reference we just check if it has pending changes
|
||||||
} else if (isLinkDraft(item[0], item[1])) {
|
setEntryState(entry, originalItem, publishedItem)
|
||||||
entry.pendingChanges = true
|
|
||||||
return entry
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// We check if the root element has pending changes
|
// We check if the root element has pending changes
|
||||||
if (entry && publishedEntry && (entry.sys.updatedAt !== publishedEntry.sys.updatedAt)) {
|
if (entry && publishedEntry) {
|
||||||
entry.pendingChanges = true
|
setEntryState(entry, entry, publishedEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
function isLinkDraft (previewLink, deliveryLink) {
|
function isLinkDraft (previewLink, deliveryLink) {
|
||||||
if (isObject(previewLink)) {
|
return previewLink && !deliveryLink
|
||||||
return !previewLink.fields || !isDateEqual(previewLink.sys.updatedAt, previewLink.sys.updatedAt)
|
}
|
||||||
|
|
||||||
|
function isLinkPendingChanges (previewLink, deliveryLink) {
|
||||||
|
if (previewLink && deliveryLink) {
|
||||||
|
return !isDateEqual(previewLink.sys.updatedAt, deliveryLink.sys.updatedAt)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -63,3 +69,12 @@ function isDateEqual (lhs, rhs) {
|
|||||||
return lhsDate.setMilliseconds(0) === rhsDate.setMilliseconds(0)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "~1.15.2",
|
"body-parser": "~1.15.2",
|
||||||
"contentful": "^5.0.2",
|
"contentful": "^5.1.1",
|
||||||
"cookie-parser": "~1.4.3",
|
"cookie-parser": "~1.4.3",
|
||||||
"dotenv": "^4.0.0",
|
"dotenv": "^4.0.0",
|
||||||
"execa": "^0.8.0",
|
"execa": "^0.8.0",
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ module.exports.initClients = (options) => {
|
|||||||
accessToken: config.deliveryToken,
|
accessToken: config.deliveryToken,
|
||||||
// Environment variable is used here to enable testing this app internally at Contentful.
|
// Environment variable is used here to enable testing this app internally at Contentful.
|
||||||
// You can just omit the host since it defaults to 'cdn.contentful.com'
|
// You can just omit the host since it defaults to 'cdn.contentful.com'
|
||||||
host: process.env.CONTENTFUL_DELIVERY_API_HOST
|
host: process.env.CONTENTFUL_DELIVERY_API_HOST,
|
||||||
|
removeUnresolved: true
|
||||||
})
|
})
|
||||||
previewClient = createClient({
|
previewClient = createClient({
|
||||||
application: applicationName,
|
application: applicationName,
|
||||||
@@ -36,7 +37,8 @@ module.exports.initClients = (options) => {
|
|||||||
accessToken: config.previewToken,
|
accessToken: config.previewToken,
|
||||||
// Environment variable is used here to enable testing this app internally at Contentful.
|
// Environment variable is used here to enable testing this app internally at Contentful.
|
||||||
// You should use 'preview.contentful.com' as host to use the preview api
|
// You should use 'preview.contentful.com' as host to use the preview api
|
||||||
host: process.env.CONTENTFUL_PREVIEW_API_HOST
|
host: process.env.CONTENTFUL_PREVIEW_API_HOST,
|
||||||
|
removeUnresolved: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ mixin editorialFeatures(entry)
|
|||||||
if settings.editorialFeatures
|
if settings.editorialFeatures
|
||||||
.editorial-features
|
.editorial-features
|
||||||
if currentApi.id === 'cpa' && (entry.draft || entry.pendingChanges)
|
if currentApi.id === 'cpa' && (entry.draft || entry.pendingChanges)
|
||||||
.editorial-features__item
|
|
||||||
+entryState(entry)
|
+entryState(entry)
|
||||||
.editorial-features__item
|
.editorial-features__item
|
||||||
a.editorial-features__text(
|
a.editorial-features__text(
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
mixin entryState(entry)
|
mixin entryState(entry)
|
||||||
if entry.draft
|
if entry.draft
|
||||||
|
.editorial-features__item
|
||||||
.pill.pill--draft #{translate('draftLabel', currentLocale.code)}
|
.pill.pill--draft #{translate('draftLabel', currentLocale.code)}
|
||||||
if entry.pendingChanges
|
if entry.pendingChanges
|
||||||
|
.editorial-features__item
|
||||||
.pill.pill--pending-changes #{translate('pendingChangesLabel', currentLocale.code)}
|
.pill.pill--pending-changes #{translate('pendingChangesLabel', currentLocale.code)}
|
||||||
|
|||||||
Reference in New Issue
Block a user