sanity-plugin-workflow 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-workflow",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "A demonstration of a custom content publishing workflow using Sanity.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,22 +1,44 @@
1
1
  import {CheckmarkIcon} from '@sanity/icons'
2
+ import {ToastContextValue, useToast} from '@sanity/ui'
2
3
  import {useCallback} from 'react'
3
- import {DocumentActionProps, useClient} from 'sanity'
4
+ import {DocumentActionProps, SanityClient, useClient} from 'sanity'
4
5
 
5
6
  import {useWorkflowContext} from '../components/WorkflowContext'
6
7
  import {API_VERSION} from '../constants'
7
8
 
9
+ export const handleDeleteMetadata = async (
10
+ client: SanityClient,
11
+ toast: ToastContextValue,
12
+ id: string
13
+ ) => {
14
+ try {
15
+ await client.delete(`workflow-metadata.${id}`)
16
+ toast.push({
17
+ status: 'success',
18
+ title: 'Workflow completed',
19
+ })
20
+ } catch (error) {
21
+ console.error(error)
22
+ toast.push({
23
+ status: 'error',
24
+ title: 'Could not complete Workflow',
25
+ })
26
+ }
27
+ }
28
+
8
29
  export function CompleteWorkflow(props: DocumentActionProps) {
9
30
  const {id} = props
10
31
  const {metadata, loading, error, states} = useWorkflowContext(id)
11
32
  const client = useClient({apiVersion: API_VERSION})
33
+ const toast = useToast()
12
34
 
13
35
  if (error) {
14
36
  console.error(error)
15
37
  }
16
38
 
17
- const handle = useCallback(() => {
18
- client.delete(`workflow-metadata.${id}`)
19
- }, [id, client])
39
+ const handle = useCallback(async () => {
40
+ await handleDeleteMetadata(client, toast, id)
41
+ }, [client, toast, id])
20
42
 
21
43
  if (!metadata) {
22
44
  return null
@@ -33,8 +55,9 @@ export function CompleteWorkflow(props: DocumentActionProps) {
33
55
  title: isLastState
34
56
  ? `Removes the document from the Workflow process`
35
57
  : `Cannot remove from workflow until in the last state`,
36
- onHandle: () => {
37
- handle()
58
+ onHandle: async () => {
59
+ await handle()
60
+ props.onComplete()
38
61
  },
39
62
  color: 'positive',
40
63
  }
@@ -3,6 +3,7 @@ import {Box, Button, Text, Tooltip, useToast} from '@sanity/ui'
3
3
  import React from 'react'
4
4
  import {useClient} from 'sanity'
5
5
 
6
+ import {handleDeleteMetadata} from '../../actions/CompleteWorkflow'
6
7
  import {API_VERSION} from '../../constants'
7
8
 
8
9
  type CompleteButtonProps = {
@@ -24,20 +25,7 @@ export default function CompleteButton(props: CompleteButtonProps) {
24
25
  return
25
26
  }
26
27
 
27
- client
28
- .delete(`workflow-metadata.${id}`)
29
- .then(() => {
30
- toast.push({
31
- status: 'success',
32
- title: 'Workflow completed',
33
- })
34
- })
35
- .catch(() => {
36
- toast.push({
37
- status: 'error',
38
- title: 'Could not complete Workflow',
39
- })
40
- })
28
+ handleDeleteMetadata(client, toast, id)
41
29
  },
42
30
  [client, toast]
43
31
  )