feat: publish single locale functionality
This commit is contained in:
@@ -18,27 +18,20 @@ export type DefaultPublishButtonProps = {
|
||||
disabled: boolean
|
||||
id?: string
|
||||
label: string
|
||||
params: any
|
||||
publish: () => void
|
||||
setParams: (params: any) => void
|
||||
publishSpecificLocale: () => void
|
||||
}
|
||||
const DefaultPublishButton: React.FC<DefaultPublishButtonProps> = ({
|
||||
id,
|
||||
canPublish,
|
||||
disabled,
|
||||
label,
|
||||
params,
|
||||
publish,
|
||||
setParams,
|
||||
publishSpecificLocale,
|
||||
}) => {
|
||||
const { code, label: localeLabel } = useLocale()
|
||||
const { label: localeLabel } = useLocale()
|
||||
if (!canPublish) return null
|
||||
|
||||
const publishSpecificLocale = () => {
|
||||
setParams({ ...params, publishSpecificLocale: code })
|
||||
publish()
|
||||
}
|
||||
|
||||
return (
|
||||
<FormSubmit
|
||||
buttonId={id}
|
||||
@@ -49,10 +42,6 @@ const DefaultPublishButton: React.FC<DefaultPublishButtonProps> = ({
|
||||
label: `Publish ${localeLabel} only`,
|
||||
onClick: publishSpecificLocale,
|
||||
},
|
||||
// {
|
||||
// label: `Publish es only`,
|
||||
// onClick: publishSpecificLocale,
|
||||
// },
|
||||
]}
|
||||
size="small"
|
||||
type="button"
|
||||
@@ -70,7 +59,6 @@ export const Publish: React.FC<Props> = ({ CustomComponent }) => {
|
||||
const { code } = useLocale()
|
||||
const { id, collection, global, publishedDoc, unpublishedVersions } = useDocumentInfo()
|
||||
const [hasPublishPermission, setHasPublishPermission] = React.useState(false)
|
||||
const [params, setParams] = React.useState<any>({ locale: code || undefined })
|
||||
const { getData, submit } = useForm()
|
||||
const modified = useFormModified()
|
||||
const {
|
||||
@@ -81,29 +69,41 @@ export const Publish: React.FC<Props> = ({ CustomComponent }) => {
|
||||
|
||||
const hasNewerVersions = unpublishedVersions?.totalDocs > 0
|
||||
const canPublish = modified || hasNewerVersions || !publishedDoc
|
||||
|
||||
const publish = useCallback(() => {
|
||||
let action
|
||||
if (collection) {
|
||||
action = `${serverURL}${api}/${collection.slug}/${id}?${qs.stringify(params)}`
|
||||
const publishSpecificLocale = useCallback(() => {
|
||||
const params = {
|
||||
locale: code,
|
||||
publishSpecificLocale: code,
|
||||
}
|
||||
|
||||
if (global) {
|
||||
action = `${serverURL}${api}/globals/${global.slug}?${qs.stringify(params)}`
|
||||
}
|
||||
const action = `${serverURL}${api}${
|
||||
global ? `/globals/${global.slug}` : `/${collection.slug}/${id ? id : ''}`
|
||||
}?${qs.stringify(params)}`
|
||||
|
||||
void submit({
|
||||
action,
|
||||
overrides: {
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
}, [submit, collection, global, serverURL, api, id, params])
|
||||
}, [submit])
|
||||
|
||||
const publish = useCallback(() => {
|
||||
void submit({
|
||||
overrides: {
|
||||
_status: 'published',
|
||||
},
|
||||
})
|
||||
}, [submit])
|
||||
|
||||
React.useEffect(() => {
|
||||
const fetchPublishAccess = async () => {
|
||||
let docAccessURL: string
|
||||
let operation = 'update'
|
||||
|
||||
const params = {
|
||||
locale: code || undefined,
|
||||
}
|
||||
|
||||
if (global) {
|
||||
docAccessURL = `/globals/${global.slug}/access`
|
||||
} else if (collection) {
|
||||
@@ -134,7 +134,7 @@ export const Publish: React.FC<Props> = ({ CustomComponent }) => {
|
||||
}
|
||||
|
||||
void fetchPublishAccess()
|
||||
}, [api, code, collection, getData, global, id, serverURL, params])
|
||||
}, [api, code, collection, getData, global, id, serverURL])
|
||||
|
||||
return (
|
||||
<RenderCustomComponent
|
||||
@@ -146,9 +146,8 @@ export const Publish: React.FC<Props> = ({ CustomComponent }) => {
|
||||
canPublish: hasPublishPermission,
|
||||
disabled: !canPublish,
|
||||
label: t('publishChanges'),
|
||||
params,
|
||||
publish,
|
||||
setParams,
|
||||
publishSpecificLocale,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -92,6 +92,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
const { password } = data
|
||||
const shouldSaveDraft = Boolean(draftArg && collectionConfig.versions.drafts)
|
||||
const shouldSavePassword = Boolean(password && collectionConfig.auth && !shouldSaveDraft)
|
||||
const publishSpecificLocale = req.query?.publishSpecificLocale
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Access
|
||||
@@ -117,6 +118,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
id,
|
||||
config: collectionConfig,
|
||||
payload,
|
||||
published: publishSpecificLocale !== undefined ? true : false,
|
||||
query: findOneArgs,
|
||||
req,
|
||||
})
|
||||
|
||||
@@ -10,6 +10,7 @@ type Args = {
|
||||
config: SanitizedCollectionConfig
|
||||
id: number | string
|
||||
payload: Payload
|
||||
published?: boolean
|
||||
query: FindOneArgs
|
||||
req?: PayloadRequest
|
||||
}
|
||||
@@ -18,17 +19,22 @@ export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
|
||||
id,
|
||||
config,
|
||||
payload,
|
||||
published,
|
||||
query,
|
||||
req,
|
||||
}: Args): Promise<T> => {
|
||||
let latestVersion: TypeWithVersion<T>
|
||||
|
||||
const whereQuery = published
|
||||
? { and: [{ parent: { equals: id } }, { 'version._status': { equals: 'published' } }] }
|
||||
: { parent: { equals: id } }
|
||||
|
||||
if (config.versions?.drafts) {
|
||||
const { docs } = await payload.db.findVersions<T>({
|
||||
collection: config.slug,
|
||||
req,
|
||||
sort: '-updatedAt',
|
||||
where: { parent: { equals: id } },
|
||||
where: whereQuery,
|
||||
})
|
||||
;[latestVersion] = docs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user