-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeleteTranslationAction.tsx
More file actions
123 lines (112 loc) · 4.07 KB
/
Copy pathDeleteTranslationAction.tsx
File metadata and controls
123 lines (112 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { TrashIcon } from '@sanity/icons'
import { type ButtonTone, useToast } from '@sanity/ui'
import { useCallback, useState } from 'react'
import { type DocumentActionComponent, type SanityDocument, useClient } from 'sanity'
import DeleteTranslationDialog from './components/DeleteTranslationDialog'
import DeleteTranslationFooter from './components/DeleteTranslationFooter'
import { useDocumentInternationalizationContext } from '@sanity/document-internationalization'
import { apiVersion } from '../../sanity.client'
import { defaultLanguage } from '../../languages'
// eslint-disable-next-line import/namespace
import { Patch, Transaction } from '@sanity/client'
const TRANSLATIONS_ARRAY_NAME = 'translations'
export const DeleteTranslationAction: DocumentActionComponent = (props) => {
const { languageField } = useDocumentInternationalizationContext()
const doc = props.draft || props.published
const documentLanguage = doc ? doc[languageField] : null
const isDefaultLanguageDocument = defaultLanguage.iso === documentLanguage
const { id: documentId } = props
const [isDialogOpen, setDialogOpen] = useState(false)
const [translations, setTranslations] = useState<SanityDocument[]>([])
const [hasOtherReferences, setHasOtherReferences] = useState<boolean>(false)
const onClose = useCallback(() => setDialogOpen(false), [])
const toast = useToast()
const client = useClient({ apiVersion: apiVersion })
const unsetAndDeleteCurrentTranslation = (tx: Transaction) => {
//unset current translation reference from metadata
translations.forEach((translation) => {
tx.patch(translation._id, (patch: Patch) =>
patch.unset([`${TRANSLATIONS_ARRAY_NAME}[_key == "${documentLanguage}"]`]),
)
})
// delete the current document
tx.delete(documentId)
tx.delete(`drafts.${documentId}`)
}
const unsetAndDeleteDefaultWithMetaData = (tx: Transaction) => {
//unset translation references array in metadata
translations.forEach((translation) => {
tx.patch(translation._id, (patch) => patch.unset([TRANSLATIONS_ARRAY_NAME]))
})
// delete each of the translations and delete metadata
if (translations.length > 0) {
translations.forEach((translation: any) => {
translation.translations.forEach((it: any) => {
tx.delete(it.id)
tx.delete(`drafts.${it.id}`)
})
tx.delete(translation._id)
// Shouldn't exist as this document type is in liveEdit
tx.delete(`drafts.${translation._id}`)
})
} else {
//simply a doc without metadata.. so delete it.
tx.delete(documentId)
tx.delete(`drafts.${documentId}`)
}
}
// Remove translation reference and delete document in one transaction
const onProceed = useCallback(() => {
const tx = client.transaction()
if (isDefaultLanguageDocument) {
unsetAndDeleteDefaultWithMetaData(tx)
} else {
unsetAndDeleteCurrentTranslation(tx)
}
tx.commit()
.then(() => {
onClose()
toast.push({
status: 'success',
title: 'Deleted document and translations',
})
})
.catch((err) => {
toast.push({
status: 'error',
title: 'Failed to delete document and translations',
description: err.message,
})
})
}, [client, onClose, toast, isDefaultLanguageDocument])
return {
label: `Delete translation...`,
disabled: !doc || !documentLanguage,
icon: TrashIcon,
tone: 'critical' as ButtonTone,
onHandle: () => {
setDialogOpen(true)
},
dialog: isDialogOpen && {
type: 'dialog',
onClose,
header: 'Delete translation',
content: doc ? (
<DeleteTranslationDialog
doc={doc}
documentId={documentId}
setTranslations={setTranslations}
setHasOtherReferences={setHasOtherReferences}
/>
) : null,
footer: (
<DeleteTranslationFooter
onClose={onClose}
onProceed={onProceed}
translations={translations}
disable={hasOtherReferences}
/>
),
},
}
}