| 1 |
import { addQueryArgs } from '@wordpress/url' |
| 2 |
import React, { useState } from 'react' |
| 3 |
import { __ } from '@wordpress/i18n' |
| 4 |
import { Button } from '../../common/Button' |
| 5 |
import { ConfirmDialog } from '../../common/ConfirmDialog' |
| 6 |
import { useSnippetsAPI } from '../../../hooks/useSnippets' |
| 7 |
import { useSnippetForm } from '../../../hooks/useSnippetForm' |
| 8 |
|
| 9 |
export const DeleteButton: React.FC = () => { |
| 10 |
const api = useSnippetsAPI() |
| 11 |
const { snippet, setIsWorking, isWorking, handleRequestError } = useSnippetForm() |
| 12 |
const [isDialogOpen, setIsDialogOpen] = useState(false) |
| 13 |
|
| 14 |
return ( |
| 15 |
<> |
| 16 |
<Button |
| 17 |
name="delete_snippet" |
| 18 |
onClick={() => setIsDialogOpen(true)} |
| 19 |
disabled={isWorking} |
| 20 |
> |
| 21 |
{__('Delete', 'code-snippets')} |
| 22 |
</Button> |
| 23 |
|
| 24 |
<ConfirmDialog |
| 25 |
open={isDialogOpen} |
| 26 |
title={__('Permanently delete?', 'code-snippets')} |
| 27 |
confirmLabel={__('Delete', 'code-snippets')} |
| 28 |
confirmButtonClassName="is-destructive" |
| 29 |
onCancel={() => setIsDialogOpen(false)} |
| 30 |
onConfirm={() => { |
| 31 |
setIsDialogOpen(false) |
| 32 |
setIsWorking(true) |
| 33 |
|
| 34 |
api.delete(snippet) |
| 35 |
.then(() => { |
| 36 |
setIsWorking(false) |
| 37 |
window.location.replace(addQueryArgs(window.CODE_SNIPPETS?.urls.manage, { result: 'deleted' })) |
| 38 |
}) |
| 39 |
.catch((error: unknown) => handleRequestError(error, __('Could not delete snippet.', 'code-snippets'))) |
| 40 |
}} |
| 41 |
> |
| 42 |
<p> |
| 43 |
{__('You are about to permanently delete this snippet.', 'code-snippets')}{' '} |
| 44 |
{__('Are you sure?', 'code-snippets')} |
| 45 |
</p> |
| 46 |
<p><strong>{__('This action cannot be undone.', 'code-snippets')}</strong></p> |
| 47 |
</ConfirmDialog> |
| 48 |
</> |
| 49 |
) |
| 50 |
} |
| 51 |
|