feat: exposes locale within preview function

This commit is contained in:
James
2021-05-10 09:24:00 -04:00
parent 1612bc8d3a
commit 2d67448d8a
4 changed files with 19 additions and 6 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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
}

View File

@@ -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[];