Skip to content

Commit

Permalink
feat: delete all translations document action
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonGriggs committed May 10, 2023
1 parent a53f8e5 commit f8ca5d3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [Querying with GraphQL](#querying-with-graphql)
- [Allowing the same slug on different language versions](#allowing-the-same-slug-on-different-language-versions)
- [Deleting documents](#deleting-documents)
- [Deleting a single translated document](#deleting-a-single-translated-document)
- [Deleting all translations](#deleting-all-translations)
- [Note on document quotas](#note-on-document-quotas)
- [Content migrations](#content-migrations)
- [License](#license)
Expand Down Expand Up @@ -265,9 +267,11 @@ export async function isUniqueOtherThanLanguage(slug: string, context: SlugValid

## Deleting documents

### Deleting a single translated document

By default, this plugin creates a strong reference between a document and its connected translation metadata document. Because reference integrity is maintained by the API, you cannot delete a document that has a strong reference to it. To offset this difficulty, the plugin exports a document action that will allow you to remove the translation reference from the action, before proceeding to delete the document. It is not added by default to your schema types.

![5e0afc304ec466756b58bced492518ae](https://github.com/sanity-io/document-internationalization/assets/9684022/edccb456-f6e1-4782-9602-b279e9689357)
![delete translation document action](https://github.com/sanity-io/document-internationalization/assets/9684022/edccb456-f6e1-4782-9602-b279e9689357)

Import into your Studio's config file

Expand All @@ -293,6 +297,12 @@ export default defineConfig({
})
```

### Deleting all translations

The metadata document also contains a "Delete all translations" document action which is queued by default for only that schema type. It will delete all of the documents in the `translations` array of references, as well as the metadata document itself.

![delete all translations document action](https://github.com/sanity-io/document-internationalization/assets/9684022/fda956f1-26e7-430a-aeef-1db4166e9cd6)

## Note on document quotas

In previous versions of this plugin, translations were stored as an array of references on the actual documents. This required a base language, lead to messy transaction histories and made deleting documents difficult.
Expand Down
90 changes: 90 additions & 0 deletions src/actions/DeleteMetadataAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {TrashIcon} from '@sanity/icons'
import {ButtonTone, useToast} from '@sanity/ui'
import {useCallback, useMemo, useState} from 'react'
import {
DocumentActionComponent,
KeyedObject,
Reference,
TypedObject,
useClient,
} from 'sanity'

import {API_VERSION, TRANSLATIONS_ARRAY_NAME} from '../constants'

type TranslationReference = TypedObject &
KeyedObject & {
value: Reference
}

export const DeleteMetadataAction: DocumentActionComponent = (props) => {
const {id: documentId, published, draft, onComplete} = props
const doc = draft || published

const [isDialogOpen, setDialogOpen] = useState(false)
const onClose = useCallback(() => setDialogOpen(false), [])
const translations: TranslationReference[] = useMemo(
() => (doc ? (doc[TRANSLATIONS_ARRAY_NAME] as TranslationReference[]) : []),
[doc]
)

const toast = useToast()
const client = useClient({apiVersion: API_VERSION})

// Remove translation reference and delete document in one transaction
const onProceed = useCallback(() => {
const tx = client.transaction()

tx.patch(documentId, (patch) => patch.unset([TRANSLATIONS_ARRAY_NAME]))

if (translations.length > 0) {
translations.forEach((translation) => {
tx.delete(translation.value._ref)
tx.delete(`drafts.${translation.value._ref}`)
})
}

tx.delete(documentId)
// Shouldn't exist as this document type is in liveEdit
tx.delete(`drafts.${documentId}`)

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, translations, documentId, onClose, toast])

return {
label: `Delete all translations`,
disabled: !doc || !translations.length,
icon: TrashIcon,
tone: 'critical' as ButtonTone,
onHandle: () => {
setDialogOpen(true)
},
dialog: isDialogOpen && {
type: 'confirm',
onCancel: onComplete,
onConfirm: () => {
onProceed()
onComplete()
},
tone: 'critical' as ButtonTone,
message:
translations.length === 1
? `Delete 1 translation and this document`
: `Delete all ${translations.length} translations and this document`,
},
}
}
9 changes: 9 additions & 0 deletions src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Stack} from '@sanity/ui'
import {defineField, definePlugin, isSanityDocument} from 'sanity'
import {internationalizedArray} from 'sanity-plugin-internationalized-array'

import {DeleteMetadataAction} from './actions/DeleteMetadataAction'
import {LanguageBadge} from './badges'
import BulkPublish from './components/BulkPublish'
import {DocumentInternationalizationProvider} from './components/DocumentInternationalizationContext'
Expand Down Expand Up @@ -85,6 +86,7 @@ export const documentInternationalization = definePlugin<PluginConfig>(
// Adds:
// - The `Translations` dropdown to the editing form
// - `Badges` to documents with a language value
// - The `DeleteMetadataAction` action to the metadata document type
document: {
unstable_languageFilter: (prev, ctx) => {
const {schemaType, documentId} = ctx
Expand All @@ -103,6 +105,13 @@ export const documentInternationalization = definePlugin<PluginConfig>(
...prev,
]
},
actions: (prev, {schemaType}) => {
if (schemaType === METADATA_SCHEMA_NAME) {
return [...prev, DeleteMetadataAction]
}

return prev
},
},

// Adds:
Expand Down

0 comments on commit f8ca5d3

Please sign in to comment.