How to use the @sanity/react-hooks.useDocumentOperation function in @sanity/react-hooks

To help you get started, we’ve selected a few @sanity/react-hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sanity-io / sanity / packages / test-studio / src / actions / PublishAction.js View on Github external
export default createAction(function PublishAction(docInfo) {
  if (docInfo.isLiveEditEnabled) {
    return null
  }

  const {publish} = useDocumentOperation(docInfo.id, docInfo.type)

  return {
    disabled: !docInfo.draft,
    label: 'Publish',
    handle: () => {
      publish(doc => omit(doc, 'reviewers'))
    }
  }
})
github sanity-io / sanity / packages / test-studio / src / test-action-tool / actions / SaveAction.js View on Github external
export default createAction(function SaveAction(docInfo) {
  const {commit} = useDocumentOperation(docInfo.id, docInfo.type)
  return {
    label: 'Save',
    onHandle: () => {
      commit.execute()
    }
  }
})
github sanity-io / sanity / packages / test-studio / src / test-action-tool / actions / AsyncAction.js View on Github external
export default createAction(function AsyncAction(docInfo) {
  const [isWorking, setIsWorking] = React.useState(false)

  const {patch} = useDocumentOperation(docInfo.id, docInfo.type)

  return {
    disabled: isWorking,
    showActivityIndicator: isWorking,
    label: isWorking ? 'Working…' : 'Do some work!',
    onHandle: async () => {
      setIsWorking(true)
      const result = await doSomeWork(docInfo.id)
      patch.execute([set('randomNumber', result)])
      setIsWorking(false)
    }
  }
})
github sanity-io / sanity / packages / test-studio / src / test-action-tool / actions / WriteFieldAction.js View on Github external
export default createAction(function WriteFieldAction({id, type, published, draft}) {
  const doc = draft || published

  const [isWriting, setIsWriting] = React.useState(false)
  const {patch} = useDocumentOperation(id, type)
  const currentTitle = (doc && doc.title) || ''
  return {
    label: `Edit title field of ${currentTitle}`,
    onHandle: () => {
      setIsWriting(true)
    },
    dialog: isWriting && (
      <>
        <h2>Edit title field</h2>
        <input value="{currentTitle}" type="text"> patch.execute([set('title', event.currentTarget.value)])}
        /&gt;
        <button> setIsWriting(false)}&gt;OK</button>
github sanity-io / sanity / packages / @sanity / desk-tool / src / actions / DeleteAction.tsx View on Github external
export const DeleteAction = createAction(function DeleteAction({
  id,
  type,
  draft,
  published,
  onComplete
}) {
  const {delete: deleteOp}: any = useDocumentOperation(id, type)
  const [isDeleting, setIsDeleting] = React.useState(false)
  const [isConfirmDialogOpen, setConfirmDialogOpen] = React.useState(false)

  return {
    icon: TrashIcon,
    disabled: Boolean(deleteOp.disabled),
    title: deleteOp.disabled ? `Cannot delete: ${deleteOp.disabled}` : '',
    label: isDeleting ? 'Deleting…' : 'Delete',
    onHandle: () => {
      setConfirmDialogOpen(true)
    },
    dialog: isConfirmDialogOpen && {
      type: 'legacy',
      onClose: onComplete,
      title: 'Delete',
      content: (
github sanity-io / sanity / packages / @sanity / desk-tool / src / actions / DuplicateAction.ts View on Github external
export const DuplicateAction = createAction(function DuplicateAction({id, type, onComplete}) {
  const {duplicate}: any = useDocumentOperation(id, type)
  const router = useRouter()

  const [isDuplicating, setDuplicating] = React.useState(false)
  return {
    icon: ContentCopyIcon,
    disabled: Boolean(isDuplicating || duplicate.disabled),
    label: isDuplicating ? 'Duplicating…' : 'Duplicate',
    title: duplicate.disabled ? `Cannot duplicate: ${duplicate.disabled}` : 'Duplicate',
    onHandle: () => {
      setDuplicating(true)
      duplicate.execute().then(copy => {
        router.navigateIntent('edit', {id: getDraftId(copy._id), type: copy._type})
        onComplete()
      })
    }
  }
github sanity-io / sanity / packages / @sanity / desk-tool / src / actions / HistoryRestoreAction.tsx View on Github external
export const HistoryRestoreAction = createAction(function RestoreRevisionAction({
  id,
  type,
  historyId,
  revision,
  onComplete
}) {
  const {restoreFrom}: any = useDocumentOperation(id, type)
  const router = useRouter()

  return {
    label: 'Restore',
    onHandle: () => {
      restoreFrom.execute(historyId, revision).then(result => {
        router.navigateIntent('edit', {id, type, rev: result.transactionId})
        onComplete()
      })
    }
  }
})
github sanity-io / sanity / packages / @sanity / desk-tool / src / actions / WriteTitleAction.tsx View on Github external
export const WriteTitleAction = createAction(function PublishAction(docInfo) {
  if (docInfo.isLiveEditEnabled) {
    return null
  }

  const {patch, commit}: any = useDocumentOperation(docInfo.id, docInfo.type)

  return {
    label: 'Set title to foo!',
    onHandle: () => {
      patch.execute([{set: {title: 'foo'}}])
      commit.execute()
    }
  }
})
github sanity-io / sanity / packages / @sanity / desk-tool / src / actions / SaveAction.tsx View on Github external
export const SaveAction = createAction(function DeleteAction({id, type, onComplete}) {
  const {commit}: any = useDocumentOperation(id, type)

  return {
    disabled: Boolean(commit.disabled),
    label: 'Save',
    onHandle: async () => {
      await commit.execute()
      onComplete()
    }
  }
})
github sanity-io / sanity / packages / @sanity / desk-tool / src / pane / DocumentPaneProvider.tsx View on Github external
export const DocumentPaneProvider = withInitialValue((props: Props) =&gt; {
  const {patch, commit}: any = useDocumentOperation(props.options.id, props.options.type)
  const editState: any = useEditState(props.options.id, props.options.type)

  const runThrottled = useThrottled(run =&gt; run(), 1000, {leading: true, trailing: true}, [])

  const value = (editState &amp;&amp; (editState.draft || editState.published)) || props.initialValue
  return (
     {
        patch.execute(patches)
        runThrottled(commit.execute)
      }}
      value={value}
      draft={editState &amp;&amp; editState.draft}
      published={editState &amp;&amp; editState.published}
      markers={editState ? editState.validation : []}

@sanity/react-hooks

Officially supported Sanity Studio API building blocks

MIT
Latest version published 9 months ago

Package Health Score

88 / 100
Full package analysis