Skip to content

Commit

Permalink
[desk-tool] Rewire initial template value templates to use params/pay…
Browse files Browse the repository at this point in the history
…load
  • Loading branch information
rexxars committed Nov 19, 2019
1 parent e452f7b commit babd0e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
16 changes: 9 additions & 7 deletions packages/@sanity/desk-tool/src/DeskToolPanes.js
Expand Up @@ -130,6 +130,13 @@ export default class DeskToolPanes extends React.Component {
return newRouterState
}

const getPaneParameters = () => {
const panes = this.props.router.state.panes || []
const group = panes[groupIndex] || []
const pane = group[siblingIndex]
return pane.params || {}
}

const ctx = {
// Zero-based index (position) of pane, visually
index: flatIndex,
Expand Down Expand Up @@ -220,13 +227,8 @@ export default class DeskToolPanes extends React.Component {
})
},

getPaneView: () => {
const panes = this.props.router.state.panes || []
const group = panes[groupIndex] || []
const pane = group[siblingIndex]
const params = pane.params || {}
return params.view
},
getPaneParameters,
getPaneView: () => getPaneParameters().view,

// Proxied navigation to a given intent. Consider just exposing `router` instead?
navigateIntent: this.props.router.navigateIntent
Expand Down
1 change: 0 additions & 1 deletion packages/@sanity/desk-tool/src/pane/DocumentPane.js
Expand Up @@ -1161,7 +1161,6 @@ export default withInitialValue(
selectedIsLatest: this.findSelectedEvent() === historyState.events[0]
},
onDelete: this.handleDelete,
onClearTransactionResult: this.handleClearTransactionResult,
onPublish: this.handlePublish,
onRestore: this.handleRestoreRevision,
onUnpublish: this.handleUnpublish,
Expand Down
16 changes: 10 additions & 6 deletions packages/@sanity/desk-tool/src/tool.js
Expand Up @@ -38,7 +38,7 @@ function DeskToolPaneStateSyncer(props) {
return <DeskTool {...props} onPaneChange={setActivePanes} />
}

function getIntentState(intentName, params, currentState, jsonParams = {}) {
function getIntentState(intentName, params, currentState, payload) {
const paneSegments = (currentState && currentState.panes) || []
const activePanes = state.activePanes || []
const editDocumentId = params.id || UUID()
Expand All @@ -48,15 +48,19 @@ function getIntentState(intentName, params, currentState, jsonParams = {}) {
for (let i = activePanes.length - 1; i >= 0; i--) {
const pane = activePanes[i]
if (pane.canHandleIntent && pane.canHandleIntent(intentName, params, {pane})) {
const paneParams = isTemplate ? {template: params.template, ...jsonParams} : undefined
return {panes: paneSegments.slice(0, i).concat([{id: editDocumentId, params: paneParams}])}
const paneParams = isTemplate ? {template: params.template} : {}
return {
panes: paneSegments
.slice(0, i)
.concat([[{id: editDocumentId, params: paneParams, payload}]])
}
}
}

return getFallbackIntentState({documentId: editDocumentId, intentName, params, jsonParams})
return getFallbackIntentState({documentId: editDocumentId, intentName, params, payload})
}

function getFallbackIntentState({documentId, intentName, params, jsonParams = {}}) {
function getFallbackIntentState({documentId, intentName, params, payload}) {
const editDocumentId = documentId
const isTemplateCreate = intentName === 'create' && params.template
const template = isTemplateCreate && getTemplateById(params.template)
Expand All @@ -65,7 +69,7 @@ function getFallbackIntentState({documentId, intentName, params, jsonParams = {}
? {
editDocumentId,
type: template.schemaType,
params: {template: params.template, ...jsonParams}
params: {template: params.template, templateParameters: payload}
}
: {editDocumentId, type: params.type || '*'}
}
Expand Down
29 changes: 20 additions & 9 deletions packages/@sanity/desk-tool/src/utils/withInitialValue.js
Expand Up @@ -6,6 +6,7 @@ import {map, switchMap, scan, filter, distinctUntilChanged, catchError} from 'rx
import schema from 'part:@sanity/base/schema'
import {observePaths} from 'part:@sanity/base/preview'
import {getDraftId, getPublishedId} from 'part:@sanity/base/util/draft-utils'
import {PaneRouterContext} from '../../'
import ErrorPane from '../pane/ErrorPane'
import LoadingPane from '../pane/LoadingPane'
import BrokenReferences from '../components/BrokenReferences'
Expand All @@ -16,11 +17,11 @@ import {
resolveInitialValue
} from '@sanity/base/initial-value-templates'

const withInitialValue = Pane =>
streamingComponent(props$ =>
const withInitialValue = Pane => {
const WithInitialValueStream = streamingComponent(props$ =>
props$.pipe(
switchMap(props => {
const {options} = props
const {options, ...paneProps} = props
// See if the document ID has a draft or a published document
return merge(
observePaths(getDraftId(options.id), ['_type']).pipe(map(draft => ({draft}))),
Expand All @@ -45,7 +46,7 @@ const withInitialValue = Pane =>
// when going from missing document to a document that exists
return of(
<BrokenReferences document={{}} type={documentType} schema={schema}>
<Pane {...props} options={paneOptions} />
<Pane {...paneProps} options={paneOptions} />
</BrokenReferences>
)
}
Expand Down Expand Up @@ -84,7 +85,7 @@ const withInitialValue = Pane =>
<LoadingPane {...props} title={title} message="Resolving initial value…" />
) : (
<BrokenReferences document={initialValue} type={documentType} schema={schema}>
<Pane {...props} initialValue={initialValue} options={paneOptions} />
<Pane {...paneProps} initialValue={initialValue} options={paneOptions} />
</BrokenReferences>
)
)
Expand All @@ -96,26 +97,36 @@ const withInitialValue = Pane =>
)
)

const WithInitialValueWrapper = props => (
<PaneRouterContext.Consumer>
{context => <WithInitialValueStream {...props} paneApi={context} />}
</PaneRouterContext.Consumer>
)

return WithInitialValueWrapper
}

function getInitialValueProps(document, props) {
if (document) {
return {}
}

const {template: definedTemplate} = props.options
const {template: urlTemplate, ...urlParameters} = props.urlParameters || {}
const payload = props.paneApi.getPayload() || {}
const urlTemplate = props.paneApi.getPaneParameters().template
const definedTemplate = props.options.template

if (urlTemplate && definedTemplate && definedTemplate !== urlTemplate) {
// eslint-disable-next-line no-console
console.warn(
`Conflicting templates: URL says "${urlParameters.template}", structure node says "${definedTemplate}". Using "${definedTemplate}".`
`Conflicting templates: URL says "${urlTemplate}", structure node says "${definedTemplate}". Using "${definedTemplate}".`
)
}

const {options = {}} = props
const template = options.template || urlTemplate
const typeTemplates = getTemplatesBySchemaType(options.type)

const parameters = {...options.templateParameters, ...urlParameters}
const parameters = {...options.templateParameters, ...payload}
let templateName = template

// If we have not specified a specific template, and we only have a single
Expand Down

0 comments on commit babd0e8

Please sign in to comment.