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