Last active
March 30, 2022 12:38
-
-
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
This file contains hidden or 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 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) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should result in a desk structure that looks something like the below. With documents filtered to only those with the field
marketset to the value of the market.As well as initial value templates which populate new documents with the current market.