Add author collection

This commit is contained in:
tobias 2024-05-20 11:40:57 +02:00
parent 9629c93ceb
commit ff564a62ec
5 changed files with 64 additions and 10 deletions

View File

@ -0,0 +1,13 @@
import { Access, FieldAccess } from "payload/types";
import { User } from "../payload-types";
export const isUser: Access<any, User> = ({ req: { user } }) => {
// Return true or false based on if the user has an ssg or admin role
return Boolean(user?.roles?.some(role => ['user', 'editor', 'admin'].includes(role)));
}
export const isUserFieldLevel: FieldAccess<{ id: string }, unknown, User> = ({ req: { user } }) => {
// Return true or false based on if the user has an ssg or admin role
return Boolean(user?.roles?.some(role => ['user', 'editor', 'admin'].includes(role)));
}

View File

@ -0,0 +1,44 @@
import { CollectionConfig } from "payload/types";
import { isAdmin } from "@/access/isAdmin";
import { isEditor } from "@/access/isEditor";
import { isSSG } from "@/access/isSSG";
import { isUser } from "@/access/isUser";
import { isEditorOrSelf } from "@/access/isEditorOrSelf";
const Authors: CollectionConfig = {
slug: "authors",
admin: {
defaultColumns: ["avatar","name"],
useAsTitle: "name",
},
access: {
//TODO: Author can CRUD own post
create: isUser,
read: () => true,
update: isEditor,
delete: isEditor,
},
fields: [
{
name: "name",
type: "text",
required: true
},
{
name: "avatar",
type: "upload",
relationTo: "media",
required: false,
},
{
name: "user",
type: "relationship",
relationTo: "users",
admin: {
description: 'The selected user will be able to edit this author'
},
}
],
};
export default Authors;

View File

@ -2,6 +2,7 @@ import { CollectionConfig } from "payload/types";
import { isAdmin } from "@/access/isAdmin"; import { isAdmin } from "@/access/isAdmin";
import { isEditor } from "@/access/isEditor"; import { isEditor } from "@/access/isEditor";
import { isSSG } from "@/access/isSSG"; import { isSSG } from "@/access/isSSG";
import { isUser } from "@/access/isUser";
const Posts: CollectionConfig = { const Posts: CollectionConfig = {
slug: "posts", slug: "posts",
@ -12,7 +13,7 @@ const Posts: CollectionConfig = {
}, },
access: { access: {
//TODO: Author can CRUD own post //TODO: Author can CRUD own post
create: isEditor, create: isUser,
read: () => true, read: () => true,
update: isEditor, update: isEditor,
delete: isEditor, delete: isEditor,
@ -89,12 +90,11 @@ const Posts: CollectionConfig = {
}, },
}, },
{ {
name: "author", name: "authors",
//TODO: Add active user as default //TODO: Add active user as default
type: 'relationship', type: 'relationship',
relationTo: 'users', relationTo: 'authors',
hasMany: false, hasMany: true,
required: false,
admin: { admin: {
position: "sidebar", position: "sidebar",
}, },

View File

@ -35,10 +35,6 @@ const Users: CollectionConfig = {
update: isAdminFieldLevel, update: isAdminFieldLevel,
}, },
}, },
{
name: 'name',
type: 'text',
}
], ],
}; };

View File

@ -2,6 +2,7 @@ import { buildConfig } from "payload/config";
import path from "path"; import path from "path";
import Posts from "@/collections/Posts"; import Posts from "@/collections/Posts";
import Users from "@/collections/Users"; import Users from "@/collections/Users";
import Authors from "./collections/Authors";
import Media from "@/collections/Media"; import Media from "@/collections/Media";
export default buildConfig({ export default buildConfig({
@ -19,7 +20,7 @@ export default buildConfig({
}, },
}), }),
}, },
collections: [Posts, Users, Media], collections: [Posts, Users, Authors, Media],
typescript: { typescript: {
outputFile: path.resolve("/", "types.ts"), outputFile: path.resolve("/", "types.ts"),
}, },