collection.ts

unlisted ⁨1⁩ ⁨file⁩ 2023-02-02 13:56:35 UTC

collection.ts

Raw
import { CollectionConfig } from "payload/types";
import slug from "../fields/slug";
import activeLocales from "../fields/activeLocales";

import IntroductionBlock from "../blocks/IntroductionBlock";
import HighlightContentBoxBlock from "../blocks/HighlightContentBoxBlock";
import ImageAndTextBlock from "../blocks/ImageAndTextBlock";
import IconListBlock from "../blocks/IconListBlock";
import { authorFieldAccessControl, commonAccessControl } from "../Common";
import MediaGalleryBlock from "../blocks/MediaGalleryBlock";
import backpopulate from "../plugins/backpopulated-relationship/hooks/backpopulate";
import NewRichTextBlock from "../blocks/NewRichTextBlock";
import { getPrice, Shop, getUpdatedProducts } from "../affiliates/amazon";
import payload from "payload";

const Products: CollectionConfig = {
  slug: "products",
  labels: {
    singular: "Product Article",
    plural: "Product Articles",
  },
  admin: {
    useAsTitle: "title",
    description: "Any products",
    group: "📄 Articles",
  },
  access: commonAccessControl,
  versions: {
    maxPerDoc: 50,
    drafts: {
      autosave: false,
    },
  },
  fields: [
    {
      name: "title",
      label: "Title",
      type: "text",
      required: true,
      localized: true,
    },
    activeLocales,
    {
      type: "tabs", // required
      tabs: [
        // required
        {
          label: "review", // required
          description: "Fields for review",
          fields: [
            // required
            {
              name: "author",
              type: "relationship",
              relationTo: "authors",
              access: authorFieldAccessControl,
            },
            slug(),
            {
              name: "updated_override", // required
              type: "date", // required
              label: "Updated (Override)",
              admin: {
                position: "sidebar",
                date: {
                  pickerAppearance: "dayAndTime",
                },
              },
            },
            {
              name: "published_override", // required
              type: "date", // required
              label: "Published (Override)",
              admin: {
                position: "sidebar",
                date: {
                  pickerAppearance: "dayAndTime",
                },
              },
            },
            {
              name: "featured_image",
              label: "Featured Image",
              type: "upload",
              relationTo: "media",
            },
            {
              name: "featured_image_caption",
              label: "Caption for Featured Image",
              type: "text",
            },
            {
              name: "content",
              label: "Content",
              type: "blocks",
              minRows: 0,
              maxRows: 1000,
              blocks: [
                NewRichTextBlock,
                IntroductionBlock,
                HighlightContentBoxBlock,
                ImageAndTextBlock,
                IconListBlock,
                MediaGalleryBlock,
              ],
            },
          ],
        },
        {
          name: "product",
          label: "Product Information", // required
          fields: [
            // required
            {
              name: "alternatives",
              label: "Alternative Products (if this one not available)",
              type: "relationship",
              relationTo: "products",
              hasMany: true,
              hooks: {
                afterChange: [backpopulate],
              },
            },
            {
              name: "shops", // required
              type: "array", // required
              label: "Shops",
              minRows: 0,
              maxRows: 10,
              labels: {
                singular: "Shop",
                plural: "Shops",
              },
              hooks: {
                afterRead: [
                  async ({ data, siblingData, value, req, operation }) => {
                    console.log("Running with", operation);
                 
                    console.log("siblingdata", siblingData);

                  },
                ],
              },
              fields: [
                // required
                {
                  name: "shop",
                  type: "relationship",
                  relationTo: "shops",
                  hasMany: false,
                  hooks: {
                    afterChange: [backpopulate],
                  },
                },
                {
                  name: "link",
                  type: "text",
                },
                {
                  name: "link_affiliate",
                  type: "text",
                },
                {
                  name: "price",
                  type: "number",
                  admin: {
                    readOnly: true,
                  },
                },
                {
                  name: "available", // required
                  type: "checkbox", // required
                  label: "Available",
                  defaultValue: true,
                  admin: {
                    readOnly: true,
                  },
                },
                {
                  name: "last_checked",
                  type: "date",
                  admin: {
                    readOnly: true,
                  },
                },
              ],
            },
          ],
        },
      ],
    },
  ],
};

export default Products;