feat: blogs and pages set author

This commit is contained in:
RizqiSyahrendra 2025-03-05 00:13:49 +07:00
parent fccd846349
commit 807e78aa70
4 changed files with 51 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import formatSlug from "@/utils/payload/formatSlug"; import formatSlug from "@/utils/payload/formatSlug";
import setAuthor from "@/utils/payload/setAuthor";
import { lexicalEditor } from "@payloadcms/richtext-lexical"; import { lexicalEditor } from "@payloadcms/richtext-lexical";
import type { CollectionConfig } from "payload"; import type { CollectionConfig } from "payload";
@ -78,19 +79,23 @@ export const Blogs: CollectionConfig = {
name: "createdBy", name: "createdBy",
type: "relationship", type: "relationship",
relationTo: "users", relationTo: "users",
hooks: {
beforeChange: [setAuthor],
},
admin: {
hidden: true, hidden: true,
// hooks: { },
// beforeChange: [setAuthor],
// },
}, },
{ {
name: "updatedBy", name: "updatedBy",
type: "relationship", type: "relationship",
relationTo: "users", relationTo: "users",
hooks: {
beforeChange: [setAuthor],
},
admin: {
hidden: true, hidden: true,
// hooks: { },
// beforeChange: [setAuthor],
// },
}, },
], ],
admin: { admin: {

View File

@ -8,6 +8,7 @@ import { HorizontalImageContentBlock } from "@/blocks/HorizontalImageContent";
import { ImageSliderBlock } from "@/blocks/ImageSlider"; import { ImageSliderBlock } from "@/blocks/ImageSlider";
import { OurTeamBlock } from "@/blocks/OurTeam"; import { OurTeamBlock } from "@/blocks/OurTeam";
import formatSlug from "@/utils/payload/formatSlug"; import formatSlug from "@/utils/payload/formatSlug";
import setAuthor from "@/utils/payload/setAuthor";
import { CollectionConfig } from "payload"; import { CollectionConfig } from "payload";
export const Pages: CollectionConfig = { export const Pages: CollectionConfig = {
@ -29,9 +30,6 @@ export const Pages: CollectionConfig = {
name: "slug", name: "slug",
label: "Page Slug", label: "Page Slug",
type: "text", type: "text",
admin: {
position: "sidebar",
},
hooks: { hooks: {
beforeValidate: [formatSlug("title")], 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: { admin: {
hideAPIURL: true, hideAPIURL: true,

View File

@ -306,6 +306,8 @@ export interface Page {
description?: string | null; description?: string | null;
cannonical_url?: string | null; cannonical_url?: string | null;
}; };
createdBy?: (number | null) | User;
updatedBy?: (number | null) | User;
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;
} }
@ -747,6 +749,8 @@ export interface PagesSelect<T extends boolean = true> {
description?: T; description?: T;
cannonical_url?: T; cannonical_url?: T;
}; };
createdBy?: T;
updatedBy?: T;
updatedAt?: T; updatedAt?: T;
createdAt?: T; createdAt?: T;
} }

View File

@ -1,14 +1,17 @@
import { FieldHook } from "payload"; import { FieldHook } from "payload";
const setAuthor: FieldHook = ({ req, operation, data, value }) => { const setAuthor: FieldHook = ({ value, req, data, field, operation }) => {
// if (req.user && !!data) { if (req.user && !!data) {
// if (operation === "create") { if (operation === "create") {
// data.createdBy = req.user.id; return req.user.id;
// } } else {
// data.updatedBy = req.user.id; if (field.name === "updatedBy") {
// } return req.user.id;
}
}
}
return data; return value;
}; };
export default setAuthor; export default setAuthor;