Skip to content

Instantly share code, notes, and snippets.

@moxdev
Created October 15, 2020 17:55
Show Gist options
  • Save moxdev/d31423ef19a1cab4a30f57a6832e8537 to your computer and use it in GitHub Desktop.
Save moxdev/d31423ef19a1cab4a30f57a6832e8537 to your computer and use it in GitHub Desktop.

Revisions

  1. @brataaaan brataaaan revised this gist Feb 14, 2020. 3 changed files with 62 additions and 1 deletion.
    28 changes: 28 additions & 0 deletions category.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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()
    ]
    },
    ],
    //...
    }
    2 changes: 1 addition & 1 deletion initialValueTemplates.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ export default [
    }
    ],
    value: parameters => ({
    category: {_type: 'reference', _ref: parameters.categoryId}
    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.
    })
    })
    ]
    33 changes: 33 additions & 0 deletions lesson.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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()
    ]
    },
    ],
    //...
    }
  2. @brataaaan brataaaan revised this gist Feb 14, 2020. No changes.
  3. @brataaaan brataaaan created this gist Feb 14, 2020.
    51 changes: 51 additions & 0 deletions deskStructure.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    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" }]})
    ])
    21 changes: 21 additions & 0 deletions initialValueTemplates.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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}
    })
    })
    ]
    17 changes: 17 additions & 0 deletions sanity.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    //...
    "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"
    }
    ]
    }