Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Nested category system causing view override in Sanity.io

3 replies
Last updated: Feb 22, 2023
Hey, I'm trying to build a nested category system which allows for creation/modification of categories on the fly. I've been using the structure builder. My main problem is that when I set the child property for one of the category lists, it overrides the view for that category document type and the only way to alter the categories is to alter them directly, rather than from within the structure that i'm creating. Ideally I would filter the category document out of the main desk. Been pulling my hair out for a couple of days now, any help would be appreciated.
Feb 22, 2023, 4:15 AM
// deskStructure.js
export default (S) =>
  S.list()
    .id("root")
    .title("Content")
    .items([
      S.listItem({
        id: "subcategories-by-maincategory",
        title: "Subcategories by Main category",
        schemaType: "sub_category",
        child: () =>
          S.documentTypeList("main_category").child((mainCategoryId) =>
            S.documentTypeList("sub_category")

              .title("Subcategories")
              .filter(
                `_type == $type && parent_category._ref == $mainCategoryId`
              )
              .params({ type: "sub_category", mainCategoryId })
              .initialValueTemplates([
                S.initialValueTemplateItem("subcategories-by-maincategory", {
                  mainCategoryId,
                }),
              ])
              .child((subCategoryId) =>
                S.documentTypeList("project")

                  .title("Projects")
                  .filter(
                    `_type == $type && main_category._ref == $mainCategoryId && sub_category._ref == $subCategoryId`
                  )
                  .params({ type: "project", mainCategoryId, subCategoryId })
                  .initialValueTemplates([
                    S.initialValueTemplateItem("projects-by-categories", {
                      mainCategoryId,
                      subCategoryId,
                    }),
                  ])
              )
          ),
      }),

      ...S.documentTypeListItems().filter(
        (listItem) =>
          ![
            // "project",
            // "siteSettings",
            // "menuBackgroundImage",
            // "sub_category",
            // "main_category",
            "media.tag",
          ].includes(listItem.getId())
      ),
    ]);
Feb 22, 2023, 4:47 AM
// sanity.config.js excerpt
templates: [
      {
        id: "subcategories-by-maincategory",
        title: "Sub Categories by Main Category",
        description: "Sub Categories under specific Main category",
        schemaType: "sub_category",
        parameters: [{ name: "mainCategoryId", type: "string" }],
        value: (params) => ({
          parent_category: { _type: "reference", _ref: params.mainCategoryId },
        }),
      },
      {
        id: "projects-by-categories",
        title: "Projects",
        description: "Projects under main category and sub category",
        schemaType: "project",
        parameters: [
          { name: "mainCategoryId", type: "string" },
          { name: "subCategoryId", type: "string" },
        ],
        value: (params) => ({
          main_category: { _type: "reference", _ref: params.mainCategoryId },
          sub_category: { _type: "reference", _ref: params.subCategoryId },
        }),
      },
    ],
Feb 22, 2023, 4:48 AM
Followed up in your other thread.
Feb 22, 2023, 6:01 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?