Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created April 15, 2020 07:35
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmelve/28a1deb84fc3b351183b239e3191a5ae to your computer and use it in GitHub Desktop.
Save kmelve/28a1deb84fc3b351183b239e3191a5ae to your computer and use it in GitHub Desktop.
Example of listing documents in Sanity Studio‘s desk structure with real-time listener
export default {
name: 'department',
type: 'document',
title: 'Department',
fields: [
{
name: 'title',
type: 'string',
title: 'Title',
},
{
name: 'sites',
type: 'array',
title: 'Sites',
of: [
{
type: 'reference',
to: [{type: 'site'}]
}
]
}
]
}
import S from '@sanity/desk-tool/structure-builder'
import documentStore from 'part:@sanity/base/datastore/document'
// remember to add rxjs/operators to your studio dependencies
import { map } from 'rxjs/operators'
// filter down documents of the type “site” that has its _id in the selected department
// parent document array of sites
const query = `*[_type == $type && _id in *[_id == $parentId][0].sites[]._ref]`
export default () =>
S.list()
.title('Content')
.items([
S.listItem()
.title('Sites by department')
.child(
S.documentList()
.title('Department')
.filter('_type == $type')
.params({ type: 'department' })
.child(parentId =>
// set up a listener to make list real-time
documentStore
.listenQuery(
query, {type: 'site', parentId}
)
.pipe(
map(sites =>
S.documentList()
.title('Sites')
.filter('_id in $ids')
.params({ids: sites.map(({_id}) => _id)})
)
)
)
),
...S.documentTypeListItems()
])
export default {
name: 'site',
type: 'document',
title: 'Site',
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment