feat: exposes locale within preview function
This commit is contained in:
@@ -70,7 +70,10 @@ Collection `admin` options can accept a `preview` function that will be used to
|
||||
|
||||
If the function is specified, a Preview button will automatically appear in the corresponding collection's Edit view. Clicking the Preview button will link to the URL that is generated by the function.
|
||||
|
||||
The preview function accepts the document being edited as an argument.
|
||||
**The preview function accepts two arguments:**
|
||||
|
||||
1. The document being edited
|
||||
1. An `options` object, containing `locale` and `token` properties. The `token` is the currently logged in user's JWT.
|
||||
|
||||
**Example collection with preview function:**
|
||||
|
||||
@@ -85,9 +88,9 @@ The preview function accepts the document being edited as an argument.
|
||||
}
|
||||
]
|
||||
admin: {
|
||||
preview: (doc) => {
|
||||
preview: (doc, { locale }) => {
|
||||
if (doc?.slug) {
|
||||
return `https://bigbird.com/preview/posts/${doc.slug}`,
|
||||
return `https://bigbird.com/preview/posts/${doc.slug}?locale=${locale}`,
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -2,14 +2,16 @@ import React from 'react';
|
||||
import { useAuth } from '@payloadcms/config-provider';
|
||||
import Button from '../Button';
|
||||
import { Props } from './types';
|
||||
import { useLocale } from '../../utilities/Locale';
|
||||
|
||||
const baseClass = 'preview-btn';
|
||||
|
||||
const PreviewButton: React.FC<Props> = ({ generatePreviewURL, data }) => {
|
||||
const locale = useLocale();
|
||||
const { token } = useAuth();
|
||||
|
||||
if (generatePreviewURL && typeof generatePreviewURL === 'function') {
|
||||
const url = generatePreviewURL(data, token);
|
||||
const url = generatePreviewURL(data, { locale, token });
|
||||
|
||||
return (
|
||||
<Button
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Data } from '../../forms/Form/types';
|
||||
import { GeneratePreviewURL } from '../../../../collections/config/types';
|
||||
|
||||
export type Props = {
|
||||
generatePreviewURL?: (data: unknown, token: string) => string,
|
||||
generatePreviewURL?: GeneratePreviewURL,
|
||||
data?: Data
|
||||
}
|
||||
|
||||
@@ -90,6 +90,13 @@ export type AfterForgotPasswordHook = (args?: {
|
||||
args?: any;
|
||||
}) => any;
|
||||
|
||||
type GeneratePreviewURLOptions = {
|
||||
locale: string
|
||||
token: string
|
||||
}
|
||||
|
||||
export type GeneratePreviewURL = (doc: Document, options: GeneratePreviewURLOptions) => string
|
||||
|
||||
export type PayloadCollectionConfig = {
|
||||
slug: string;
|
||||
labels?: {
|
||||
@@ -108,7 +115,7 @@ export type PayloadCollectionConfig = {
|
||||
}
|
||||
};
|
||||
enableRichTextRelationship?: boolean
|
||||
preview?: (doc: Document, token: string) => string
|
||||
preview?: GeneratePreviewURL
|
||||
};
|
||||
hooks?: {
|
||||
beforeOperation?: BeforeOperationHook[];
|
||||
|
||||
Reference in New Issue
Block a user