Nested category system causing view override in Sanity.io
3 replies
Last updated: Feb 22, 2023
A
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
A
// 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
A
// 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
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.