Skip to content

Commit

Permalink
feat(cms): Add Product API to CMS (#159)
Browse files Browse the repository at this point in the history
* feat(cms): add products collection to CMS

* refactor(cms): change categories, colors, sizes to has many text fields
  • Loading branch information
mrzzy authored Sep 8, 2024
1 parent 9ffa53b commit 7a961fa
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
93 changes: 93 additions & 0 deletions apps/cms/src/collections/Products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { CollectionConfig } from "payload/types";

/** Products collection stores merch products offerings. */
export const Products: CollectionConfig = {
slug: "products",
admin: {
description: "Merchandise products offerings",
defaultColumns: ["name", "category", "price", "is_available"],
},
fields: [
// by default, payload generates an 'id' field each order automatically
{
name: "name",
type: "text",
required: true,
},
{
name: "colors",
type: "text",
required: true,
hasMany: true,
},
{
name: "sizes",
type: "text",
hasMany: true,
},
{
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: "text",
required: true,
hasMany: true,
minRows: 1,
},
{
name: "size_chart",
label: "Size Chart",
type: "text",
},
{
name: "stock",
type: "array",
fields: [
{
name: "color",
type: "text",
required: true,
},
{
name: "size",
type: "text",
required: true,
},
{
name: "quantity",
type: "number",
admin: {
step: 1,
},
required: true,
min: 0,
},
],
},
],
};

export default Products;
7 changes: 5 additions & 2 deletions apps/cms/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -115,3 +116,5 @@ export default buildConfig({
]
: [],
});


31 changes: 31 additions & 0 deletions apps/cms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Config {
users: User;
media: Media;
orders: Order;
products: Product;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
Expand Down Expand Up @@ -152,6 +153,36 @@ 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: string[];
sizes?: string[] | null;
images?:
| {
url: string;
id?: string | null;
}[]
| null;
is_available?: boolean | null;
price: number;
category: string[];
size_chart?: string | null;
stock?:
| {
color: string;
size: string;
quantity: number;
id?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".
Expand Down

0 comments on commit 7a961fa

Please sign in to comment.