Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): fix version list status for unpublished documents #11983

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/next/src/views/Version/SelectComparison/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import type { PaginatedDocs, Where } from 'payload'

import { fieldBaseClass, Pill, ReactSelect, useConfig, useTranslation } from '@payloadcms/ui'
import {
fieldBaseClass,
Pill,
ReactSelect,
useConfig,
useDocumentInfo,
useTranslation,
} from '@payloadcms/ui'
import { formatDate } from '@payloadcms/ui/shared'
import { stringify } from 'qs-esm'
import React, { useCallback, useEffect, useState } from 'react'
Expand Down Expand Up @@ -37,6 +44,8 @@ export const SelectComparison: React.FC<Props> = (props) => {
},
} = useConfig()

const { hasPublishedDoc } = useDocumentInfo()

const [options, setOptions] = useState<
{
label: React.ReactNode | string
Expand Down Expand Up @@ -109,7 +118,10 @@ export const SelectComparison: React.FC<Props> = (props) => {
},
published: {
currentLabel: t('version:currentPublishedVersion'),
latestVersion: latestPublishedVersion,
// The latest published version does not necessarily equal the current published version,
// because the latest published version might have been unpublished in the meantime.
// Hence, we should only use the latest published version if there is a published document.
latestVersion: hasPublishedDoc ? latestPublishedVersion : undefined,
pillStyle: 'success',
previousLabel: t('version:previouslyPublished'),
},
Expand Down
33 changes: 27 additions & 6 deletions packages/next/src/views/Versions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,34 @@ export async function VersionsView(props: DocumentViewServerProps) {
payload,
status: 'draft',
})
latestPublishedVersion = await getLatestVersion({
slug: collectionSlug,
type: 'collection',
parentID: id,
payload,
status: 'published',
const publishedDoc = await payload.count({
collection: collectionSlug,
depth: 0,
overrideAccess: true,
req,
where: {
id: {
equals: id,
},
_status: {
equals: 'published',
},
},
})

// If we pass a latestPublishedVersion to buildVersionColumns,
// this will be used to display it as the "current published version".
// However, the latest published version might have been unpublished in the meantime.
// Hence, we should only pass the latest published version if there is a published document.
latestPublishedVersion =
publishedDoc.totalDocs > 0 &&
(await getLatestVersion({
slug: collectionSlug,
type: 'collection',
parentID: id,
payload,
status: 'published',
}))
}
} catch (err) {
logError({ err, payload })
Expand Down
40 changes: 40 additions & 0 deletions test/versions/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,46 @@ describe('Versions', () => {
await expect(page.locator('#field-title')).toHaveValue('v1')
})

test('should show currently published version status in versions view', async () => {
const publishedDoc = await payload.create({
collection: draftCollectionSlug,
data: {
_status: 'published',
title: 'title',
description: 'description',
},
overrideAccess: true,
})

await page.goto(`${url.edit(publishedDoc.id)}/versions`)
await expect(page.locator('main.versions')).toContainText('Current Published Version')
})

test('should show unpublished version status in versions view', async () => {
const publishedDoc = await payload.create({
collection: draftCollectionSlug,
data: {
_status: 'published',
title: 'title',
description: 'description',
},
overrideAccess: true,
})

// Unpublish the document
await payload.update({
collection: draftCollectionSlug,
id: publishedDoc.id,
data: {
_status: 'draft',
},
draft: false,
})

await page.goto(`${url.edit(publishedDoc.id)}/versions`)
await expect(page.locator('main.versions')).toContainText('Previously Published')
})

test('should show global versions view level action in globals versions view', async () => {
const global = new AdminUrlUtil(serverURL, draftGlobalSlug)
await page.goto(`${global.global(draftGlobalSlug)}/versions`)
Expand Down
Loading