-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete all translations document action
- Loading branch information
1 parent
a53f8e5
commit f8ca5d3
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters