feat: blogs and pages set author
This commit is contained in:
parent
fccd846349
commit
807e78aa70
@ -1,4 +1,5 @@
|
||||
import formatSlug from "@/utils/payload/formatSlug";
|
||||
import setAuthor from "@/utils/payload/setAuthor";
|
||||
import { lexicalEditor } from "@payloadcms/richtext-lexical";
|
||||
import type { CollectionConfig } from "payload";
|
||||
|
||||
@ -78,19 +79,23 @@ export const Blogs: CollectionConfig = {
|
||||
name: "createdBy",
|
||||
type: "relationship",
|
||||
relationTo: "users",
|
||||
hooks: {
|
||||
beforeChange: [setAuthor],
|
||||
},
|
||||
admin: {
|
||||
hidden: true,
|
||||
// hooks: {
|
||||
// beforeChange: [setAuthor],
|
||||
// },
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "updatedBy",
|
||||
type: "relationship",
|
||||
relationTo: "users",
|
||||
hooks: {
|
||||
beforeChange: [setAuthor],
|
||||
},
|
||||
admin: {
|
||||
hidden: true,
|
||||
// hooks: {
|
||||
// beforeChange: [setAuthor],
|
||||
// },
|
||||
},
|
||||
},
|
||||
],
|
||||
admin: {
|
||||
|
@ -8,6 +8,7 @@ import { HorizontalImageContentBlock } from "@/blocks/HorizontalImageContent";
|
||||
import { ImageSliderBlock } from "@/blocks/ImageSlider";
|
||||
import { OurTeamBlock } from "@/blocks/OurTeam";
|
||||
import formatSlug from "@/utils/payload/formatSlug";
|
||||
import setAuthor from "@/utils/payload/setAuthor";
|
||||
import { CollectionConfig } from "payload";
|
||||
|
||||
export const Pages: CollectionConfig = {
|
||||
@ -29,9 +30,6 @@ export const Pages: CollectionConfig = {
|
||||
name: "slug",
|
||||
label: "Page Slug",
|
||||
type: "text",
|
||||
admin: {
|
||||
position: "sidebar",
|
||||
},
|
||||
hooks: {
|
||||
beforeValidate: [formatSlug("title")],
|
||||
},
|
||||
@ -75,6 +73,28 @@ export const Pages: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "createdBy",
|
||||
type: "relationship",
|
||||
relationTo: "users",
|
||||
hooks: {
|
||||
beforeChange: [setAuthor],
|
||||
},
|
||||
admin: {
|
||||
hidden: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "updatedBy",
|
||||
type: "relationship",
|
||||
relationTo: "users",
|
||||
hooks: {
|
||||
beforeChange: [setAuthor],
|
||||
},
|
||||
admin: {
|
||||
hidden: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
admin: {
|
||||
hideAPIURL: true,
|
||||
|
@ -306,6 +306,8 @@ export interface Page {
|
||||
description?: string | null;
|
||||
cannonical_url?: string | null;
|
||||
};
|
||||
createdBy?: (number | null) | User;
|
||||
updatedBy?: (number | null) | User;
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
@ -747,6 +749,8 @@ export interface PagesSelect<T extends boolean = true> {
|
||||
description?: T;
|
||||
cannonical_url?: T;
|
||||
};
|
||||
createdBy?: T;
|
||||
updatedBy?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
|
@ -1,14 +1,17 @@
|
||||
import { FieldHook } from "payload";
|
||||
|
||||
const setAuthor: FieldHook = ({ req, operation, data, value }) => {
|
||||
// if (req.user && !!data) {
|
||||
// if (operation === "create") {
|
||||
// data.createdBy = req.user.id;
|
||||
// }
|
||||
// data.updatedBy = req.user.id;
|
||||
// }
|
||||
const setAuthor: FieldHook = ({ value, req, data, field, operation }) => {
|
||||
if (req.user && !!data) {
|
||||
if (operation === "create") {
|
||||
return req.user.id;
|
||||
} else {
|
||||
if (field.name === "updatedBy") {
|
||||
return req.user.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
return value;
|
||||
};
|
||||
|
||||
export default setAuthor;
|
||||
|
Loading…
x
Reference in New Issue
Block a user