Skip to content

Instantly share code, notes, and snippets.

@moxdev
Created October 15, 2020 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moxdev/d31423ef19a1cab4a30f57a6832e8537 to your computer and use it in GitHub Desktop.
Save moxdev/d31423ef19a1cab4a30f57a6832e8537 to your computer and use it in GitHub Desktop.
IDEA: You can create Categories (e.g. Programming Basics). Inside these you can create individual lessons (e.g. What are variables?, what are recursions?). If I want to create a Lesson inside I category now, I don't want to reference the category manually but want the category already selected for reference in the new lesson document. So there s…
import { FaListAlt } from 'react-icons/lib/fa'
export default {
title: "Category",
name: "category",
type: "document",
icon: FaListAlt,
fields: [
{
title: "Name",
name: "name",
type: "string",
validation: Rule => [
Rule.required()
]
},
{
title: "Description",
name: "description",
description: "Describe, what users can expect to learn from completing this category.",
type: "string",
validation: Rule => [
Rule.required()
]
},
],
//...
}
import S from "@sanity/desk-tool/structure-builder"
import { FaFileText, FaEdit, FaComment, FaPlayCircle, FaQuestionCircle, FaCheckCircle } from 'react-icons/lib/fa'
export default () =>
S.list()
.title("App")
.items([
S.listItem()
.title("Content")
.icon(FaFileText)
.child(
S.documentList()
.id('categories')
.title('Categories')
.schemaType('category')
.filter('_type == "category" && !(_id in path("drafts.**"))')
.defaultOrdering([{field: 'position', direction: 'asc'}])
.menuItems([
S.orderingMenuItem({title: 'Position ascending', by: [{ field: "position", direction: "asc" }]}),
S.orderingMenuItem({title: 'Position descending', by: [{ field: "position", direction: "desc" }]})
])
.child(categoryId =>
S.documentList()
.id('lessons')
.title('Lessons of Category')
.schemaType('lesson')
.filter('_type == "lesson" && category._ref == $categoryId && !(_id in path("drafts.**"))')
.params({types: ['lesson-for-category'], categoryId})
//not this works:
.initialValueTemplates([
S.initialValueTemplateItem('lesson-for-category', {categoryId: categoryId})
])
.defaultOrdering([{field: 'position', direction: 'asc'}])
.menuItems([
//nor this:
S.menuItemsFromInitialValueTemplateItems([
S.initialValueTemplateItem('lesson-for-category', {categoryId: categoryId}),
]),
S.menuItem()
.title('edit category')
.icon(FaEdit)
.intent({
type: "edit",
params: {
type: "category",
id: categoryId
}
}),
S.orderingMenuItem({title: 'Position ascending', by: [{ field: "position", direction: "asc" }]}),
S.orderingMenuItem({title: 'Position descending', by: [{ field: "position", direction: "desc" }]})
])
import T from '@sanity/base/initial-value-template-builder'
export default [
...T.defaults(),
T.template({
id: 'lesson-for-category',
title: 'Lesson for Category',
description: 'Creates a lesson for selected category',
schemaType: 'lesson',
parameters: [
{
name: 'categoryId',
type: 'string'
}
],
value: parameters => ({
category: {_type: 'reference', _ref: parameters.categoryId} //if i put here the categoryID manually for testing, the templating works (of course only manually for one specific category). So it has to be the parameter-passing from deskStructure to template.
})
})
]
export default {
title: "Lesson",
name: "lesson",
type: "document",
fields: [
{
title: "Belongs to Category",
name: "category",
type: "reference",
to: [{type: "category"}],
validation: Rule => [
Rule.required()
]
},
{
title: "Name",
name: "name",
type: "string",
validation: Rule => [
Rule.required()
]
},
{
title: "Description",
name: "description",
type: "text",
validation: Rule => [
Rule.required()
]
},
],
//...
}
{
//...
"parts": [
{
"name": "part:@sanity/base/schema",
"path": "./schemas/schema.js"
},
{
"name": "part:@sanity/desk-tool/structure",
"path": "./deskStructure.js"
},
{
"name": "part:@sanity/base/initial-value-templates",
"path": "./initialValueTemplates.js"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment