You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to learn payload, so I was following joshtriedCoding yt channel. I am trying to build on top of what he was showing. I have a product collection that works well and I want to convert it into stepper that has 3 steps. Each steps will contain some fields from that product collection. How can I create this custom component in the payload cms?
Below is the collection.
`import {
AfterChangeHook,
BeforeChangeHook,
AfterReadHook,
} from "payload/dist/collections/config/types";
import { PRODUCT_CATEGORIES } from "../../config";
import { Access, CollectionConfig } from "payload/types";
import { Product, ProductFile, User } from "../../payload-types";
import { stripe } from "../../lib/stripe";
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to learn payload, so I was following joshtriedCoding yt channel. I am trying to build on top of what he was showing. I have a product collection that works well and I want to convert it into stepper that has 3 steps. Each steps will contain some fields from that product collection. How can I create this custom component in the payload cms?
Below is the collection.
`import {
AfterChangeHook,
BeforeChangeHook,
AfterReadHook,
} from "payload/dist/collections/config/types";
import { PRODUCT_CATEGORIES } from "../../config";
import { Access, CollectionConfig } from "payload/types";
import { Product, ProductFile, User } from "../../payload-types";
import { stripe } from "../../lib/stripe";
const addUser: BeforeChangeHook = async ({ req, data, operation }) => {
if (operation === "create") {
const user = req.user;
return { ...data, user: user.id };
}
};
const syncUser: AfterChangeHook = async ({ req, doc }) => {
// console.log("Syncing user after change", { doc });
const fullUser = await req.payload.findByID({
collection: "users",
id: req.user.id,
});
// console.log("Full user", fullUser);
if (fullUser && typeof fullUser === "object") {
const { products } = fullUser;
} else {
console.log("User not found or invalid user data", { userId: req.user.id });
}
};
const isAdminOrHasAccess =
(): Access =>
({ req: { user: _user } }) => {
const user = _user as User | undefined;
};
const updateProductFilesAfterApproval: AfterChangeHook = async ({
req,
doc,
previousDoc,
}) => {
if (
previousDoc.approvedForSale !== "approved" &&
doc.approvedForSale === "approved"
) {
const productFiles = await req.payload.find({
collection: "product_files",
where: {
id: {
equals: doc.id,
},
},
});
}
};
export const Products: CollectionConfig = {
slug: "products",
admin: {
useAsTitle: "name",
},
access: {
read: isAdminOrHasAccess(),
update: isAdminOrHasAccess(),
delete: isAdminOrHasAccess(),
},
hooks: {
afterChange: [syncUser, updateProductFilesAfterApproval],
beforeChange: [
addUser,
async (args) => {
if (args.operation === "create") {
const data = args.data as Product;
},
fields: [
{
name: "user",
type: "relationship",
relationTo: "users",
required: true,
hasMany: false,
admin: {
condition: () => false,
},
},
{
name: "name",
label: "Name",
type: "text",
required: true,
},
{
name: "description",
type: "textarea",
label: "Product details",
},
{
name: "price",
label: "Price in USD",
min: 0,
max: 1000,
type: "number",
required: true,
},
{
name: "category",
label: "Category",
type: "select",
options: PRODUCT_CATEGORIES.map(({ label, value }) => ({ label, value })),
required: true,
},
{
name: "product_files",
label: "Product file(s)",
type: "relationship",
relationTo: "product_files",
hasMany: false,
required: true,
},
{
name: "approvedForSale",
label: "Product Status",
type: "select",
defaultValue: "pending",
access: {
create: ({ req }) => req.user.role === "admin",
read: ({ req }) => req.user.role === "admin",
update: ({ req }) => req.user.role === "admin",
},
options: [
{
label: "Pending verification",
value: "pending",
},
{
label: "Approved",
value: "approved",
},
{
label: "Denied",
value: "denied",
},
],
},
{
name: "priceId",
access: {
create: () => false,
read: () => false,
update: () => false,
},
type: "text",
admin: {
hidden: true,
},
},
{
name: "stripeId",
access: {
create: () => false,
read: () => false,
update: () => false,
},
type: "text",
admin: {
hidden: true,
},
},
{
name: "images",
type: "array",
label: "Product images",
minRows: 1,
maxRows: 4,
required: true,
labels: {
singular: "Image",
plural: "Images",
},
fields: [
{
name: "image",
type: "upload",
relationTo: "media",
required: true,
},
],
},
],
};
`
Beta Was this translation helpful? Give feedback.
All reactions