From bb7e6df63eaa1c6778c1d5828ff718a37c22e0b4 Mon Sep 17 00:00:00 2001 From: Zhu Zhanyan Date: Sun, 2 Jun 2024 16:06:31 +0800 Subject: [PATCH] feat(cms): add products collection to CMS --- apps/cms/src/collections/Products.ts | 126 +++++++++++++++++++++++++++ apps/cms/src/payload.config.ts | 7 +- apps/cms/src/types.ts | 30 +++++++ 3 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 apps/cms/src/collections/Products.ts diff --git a/apps/cms/src/collections/Products.ts b/apps/cms/src/collections/Products.ts new file mode 100644 index 00000000..f13be5ab --- /dev/null +++ b/apps/cms/src/collections/Products.ts @@ -0,0 +1,126 @@ +import { CollectionConfig } from "payload/types"; + +/** Products collection stores merch products offerings. */ +export const Products: CollectionConfig = { + slug: "products", + admin: { + description: "Merchandise products offerings", + }, + fields: [ + // by default, payload generates an 'id' field each order automatically + { + name: "name", + type: "text", + required: true, + }, + { + name: "colors", + type: "select", + hasMany: true, + options: [ + { + label: "Black", + value: "black", + }, + { + label: "White", + value: "white", + }, + { + label: "Blue", + value: "blue", + }, + ], + }, + { + name: "sizes", + type: "select", + hasMany: true, + options: [ + { + label: "Small", + value: "s", + }, + { + label: "Medium", + value: "m", + }, + { + label: "Large", + value: "l", + }, + { + label: "Extra Large", + value: "xl", + }, + ], + }, + { + name: "images", + type: "array", + fields: [ + { + name: "url", + type: "text", + required: true, + }, + ], + }, + { + name: "is_available", + label: "Is Available", + type: "checkbox", + }, + { + name: "price", + type: "number", + required: true, + admin: { + step: 0.01, + }, + min: 0, + }, + { + name: "category", + type: "select", + required: true, + options: [ + { + label: "Shirt", + value: "shirt", + }, + { + label: "Hat", + value: "hat", + }, + ], + }, + { + name: "size_chart", + type: "text", + }, + { + name: "stock", + type: "array", + fields: [ + { + name: "color", + type: "select", + options: ["black", "white", "blue"], + required: true, + }, + { + name: "quantity", + type: "number", + admin: { + step: 1, + }, + required: true, + min: 0, + }, + ], + }, + ], +}; + +export default Products; diff --git a/apps/cms/src/payload.config.ts b/apps/cms/src/payload.config.ts index 378ea711..7fdae6da 100644 --- a/apps/cms/src/payload.config.ts +++ b/apps/cms/src/payload.config.ts @@ -19,9 +19,10 @@ import MerchProducts from "./admin/views/MerchProducts"; import MerchPromotion from "./admin/views/MerchPromotion"; import { SCSEIcon, SCSELogo } from "./admin/graphics/Logos"; import BeforeNavLinks from "./admin/components/BeforeNavLinks"; -import Order from "./collections/Orders"; +import Orders from "./collections/Orders"; import { isUsingCloudStore } from "./utilities/cloud"; import { mongooseAdapter } from "@payloadcms/db-mongodb"; +import Products from "./collections/Products"; const adapter = createS3Adapter({ config: { @@ -85,7 +86,7 @@ export default buildConfig({ return config }, }, - collections: [Categories, Posts, Tags, Users, Media, Order], + collections: [Categories, Posts, Tags, Users, Media, Orders, Products], csrf: [ // whitelist of domains to allow cookie auth from process.env.PAYLOAD_PUBLIC_SERVER_URL, @@ -115,3 +116,5 @@ export default buildConfig({ ] : [], }); + + diff --git a/apps/cms/src/types.ts b/apps/cms/src/types.ts index 87bbc7cc..3bf62802 100644 --- a/apps/cms/src/types.ts +++ b/apps/cms/src/types.ts @@ -30,6 +30,7 @@ export interface Config { users: User; media: Media; orders: Order; + products: Product; 'payload-preferences': PayloadPreference; 'payload-migrations': PayloadMigration; }; @@ -152,6 +153,35 @@ export interface Order { updatedAt: string; createdAt: string; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "products". + */ +export interface Product { + id: string; + name: string; + colors?: ('black' | 'white' | 'blue')[] | null; + sizes?: ('s' | 'm' | 'l' | 'xl')[] | null; + images?: + | { + url: string; + id?: string | null; + }[] + | null; + is_available?: boolean | null; + price: number; + category: 'shirt' | 'hat'; + size_chart?: string | null; + stock?: + | { + color: 'black' | 'white' | 'blue'; + quantity: number; + id?: string | null; + }[] + | null; + updatedAt: string; + createdAt: string; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "payload-preferences".