Skip to content

Instantly share code, notes, and snippets.

@SimeonGriggs
Last active March 30, 2022 12:38
Show Gist options
  • Select an option

  • Save SimeonGriggs/88bdb9a4f449b6955a176198d15e7816 to your computer and use it in GitHub Desktop.

Select an option

Save SimeonGriggs/88bdb9a4f449b6955a176198d15e7816 to your computer and use it in GitHub Desktop.
Structure Builder for "markets" that share schema and populate an initial value template
import React from 'react'
import S from '@sanity/desk-tool/structure-builder'
import schema from 'part:@sanity/base/schema'
import userStore from 'part:@sanity/base/user'
// npm i pluralize
import pluralize from 'pluralize'
// BYO "Flag" component for an Icon
// import Flag from '../components/Flag/index'
// Customise as required
export const MARKETS = [
{
name: `DE`,
title: `Germany`,
languages: [`de`, `en`],
},
{
name: `US`,
title: `USA`,
languages: [`en`],
},
{
name: `CA`,
title: `Canada`,
languages: [`en`, `fr-ca`],
},
]
// These must be registered to your schema
export const SCHEMAS = [`article`, `product`, `page`, `settings`]
const schemaItem = (market, schemaType) =>
S.listItem()
.id(`${market.name}-${schemaType}`)
.title(`${pluralize.plural(schema.get(schemaType).title)}`)
.schemaType(schemaType)
.child(
S.documentTypeList(schemaType)
.title(`${market.title} ${pluralize.plural(schema.get(schemaType).title)}`)
.filter(`_type == $schemaType && market == $market`)
.params({schemaType, market: market.name})
// You will need to separate setup an initial value template which expects a string param of "market"
.initialValueTemplates([
S.initialValueTemplateItem(schemaType, {
market: market.name,
}),
])
)
const allSchemaItems = (market) => SCHEMAS.map((schemaType) => schemaItem(market, schemaType))
const marketItem = (market) =>
S.listItem(market.name)
.id(`${market.name}-` + `market`)
.title(market.title)
// .icon(() => <Flag market={market.name} />)
.child(S.list().title(`${market.title} Market Content`).items(allSchemaItems(market)))
export default () => {
const title = `All Markets`
const items = MARKETS.map((market) => marketItem(market))
return S.list().title(title).items(items)
}
@SimeonGriggs
Copy link
Author

This should result in a desk structure that looks something like the below. With documents filtered to only those with the field market set to the value of the market.

As well as initial value templates which populate new documents with the current market.

Screenshot 2022-03-30 at 13 37 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment