
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeimport PropTypes from 'prop-types'
import React from 'react'
import client from 'part:@sanity/base/client'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import {setIfMissing} from 'part:@sanity/form-builder/patch-event'
import {FormBuilderInput} from 'part:@sanity/form-builder'
export default class CustomObjectInput extends React.PureComponent {
static propTypes = {
type: PropTypes.shape({
title: PropTypes.string,
name: PropTypes.string
}).isRequired,
level: PropTypes.number,
value: PropTypes.shape({
_type: PropTypes.string
}),
focusPath: PropTypes.array.isRequired,
onFocus: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired
}
firstFieldInput = React.createRef()
handleFieldChange = (field, fieldPatchEvent) => {
const {onChange, type , value} = this.props
let region = value.region || ''
let language = value.language || ''
let course = value.courseType
let name = `${region} ${language} ${course}`
// Whenever the field input emits a patch event, we need to make sure to each of the included patches
// are prefixed with its field name, e.g. going from:
// {path: [], set: <nextvalue>} to {path: [<fieldName>], set: <nextValue>}
// and ensure this input's value exists
onChange(fieldPatchEvent.prefixAll(field.name).prepend(setIfMissing({_type: type.name})))
}
focus() {
this.firstFieldInput.current.focus()
}
render() {
const {type, value, level, focusPath, onFocus, onBlur} = this.props
return (
<Fieldset>
<div >
{type.fields.map((field, i) => (
// Delegate to the generic FormBuilderInput. It will resolve and insert the actual input component
// for the given field type
<div style={{marginBottom: `16px`}}>
<FormBuilderInput
level={level + 1}
ref={i === 0 ? this.firstFieldInput : null}
key={field.name}
type={field.type}
value={value && value[field.name]}
onChange={patchEvent => this.handleFieldChange(field, patchEvent)}
path={[field.name]}
focusPath={focusPath}
onFocus={onFocus}
onBlur={onBlur}
/>
</div>
))}
</div>
</Fieldset>
)
}
}import client from 'part:@sanity/base/client' const doc = await client.getDocument(your-ref-doc-id)
import { useState, useEffect } from 'react'
import { useDocumentOperation } from '@sanity/react-hooks'
export function UpdateInstanceName(props) {
const {patch, publish} = useDocumentOperation(props.id, props.type)
const [isPublishing, setIsPublishing] = useState(false)
useEffect(() => {
// if the isPublishing state was set to true and the draft has changed
// to become `null` the document has been published
console.log(isPublishing && !props.draft)
if (isPublishing && !props.draft) {
setIsPublishing(false)
}
}, [props.draft])
return {
disabled: publish.disabled,
label: isPublishing ? 'Publishing…' : 'Publish',
onHandle: () => {
console.log("handle Something")
// This will update the button text
setIsPublishing(true)
// Set publishedAt to current date and time
patch.execute([{set: {name: "Test Name"}}])
// Perform the publish
publish.execute()
// Signal that the action is completed
props.onComplete()
}
}
}console.loghappens so I know it's hooked up but it seems like the
onHandlecallback is never called as the
console.logwithin it never logs. Can you see what I'm doing wrong?
console.logwithin the useEffect logs so that should indicate that it's properly registered, right? It's the
console.login the
onHandlecallback that doesn't log when I publish the document, should it? I assumed it should but maybe that's wrong.
resolveDocumentActions.jslook?
label: isPublishing ? 'Testing…' : 'My test action',
resolveDocumentActions.jslooks like this:
import defaultResolve from 'part:@sanity/base/document-actions'
import { UpdateInstanceName } from '../actions/updateInstanceName'
export default function resolveDocumentActions(props) {
return [...defaultResolve(props), UpdateInstanceName]
}import defaultResolve, {PublishAction} from 'part:@sanity/base/document-actions'
import {UpdateInstanceName} from './updateInstanceName'
export default function resolveDocumentActions(props) {
return defaultResolve(props)
.map(Action =>
Action === PublishAction ? UpdateInstanceName : Action
)
}Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store