Created
April 15, 2020 07:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}] | |
} | |
] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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