| 1 |
import React, { useState } from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { createInterpolateElement } from '@wordpress/element' |
| 4 |
import { useSnippetsAPI } from '../../hooks/useSnippetsAPI' |
| 5 |
import { Button } from './Button' |
| 6 |
import { ConfirmDialog } from './ConfirmDialog' |
| 7 |
import type { Snippet } from '../../types/Snippet' |
| 8 |
import type { ButtonProps } from './Button' |
| 9 |
|
| 10 |
const TrashActiveConfirmMessage = () => |
| 11 |
<> |
| 12 |
<p style={{ marginBlockStart: 0 }}> |
| 13 |
{createInterpolateElement( |
| 14 |
__('This snippet is currently <strong>active</strong>.', 'code-snippets'), |
| 15 |
{ strong: <strong /> } |
| 16 |
)} |
| 17 |
</p> |
| 18 |
|
| 19 |
<p>{__('Moving it to the trash will also deactivate it.', 'code-snippets')}</p> |
| 20 |
</> |
| 21 |
|
| 22 |
const PermanentDeleteConfirmMessage = () => |
| 23 |
<> |
| 24 |
<p style={{ marginBlockStart: 0 }}> |
| 25 |
{createInterpolateElement( |
| 26 |
__('The snippet will be <strong>permanently deleted</strong>.', 'code-snippets'), |
| 27 |
{ strong: <strong /> } |
| 28 |
)} |
| 29 |
</p> |
| 30 |
|
| 31 |
<p>{__('This action cannot be undone.', 'code-snippets')}</p> |
| 32 |
</> |
| 33 |
|
| 34 |
export interface DeleteButtonProps extends ButtonProps { |
| 35 |
snippet: Snippet |
| 36 |
setIsWorking?: (isWorking: boolean) => void |
| 37 |
onSuccess?: () => Promise<void> | void |
| 38 |
onError?: (error: unknown) => void |
| 39 |
} |
| 40 |
|
| 41 |
export const DeleteButton: React.FC<DeleteButtonProps> = ({ |
| 42 |
snippet, |
| 43 |
onSuccess, |
| 44 |
onError, |
| 45 |
className = 'delete-button', |
| 46 |
setIsWorking, |
| 47 |
...buttonProps |
| 48 |
}) => { |
| 49 |
const snippetsAPI = useSnippetsAPI() |
| 50 |
const [isDialogOpen, setIsDialogOpen] = useState(false) |
| 51 |
|
| 52 |
const handleDelete = () => { |
| 53 |
setIsWorking?.(true) |
| 54 |
|
| 55 |
snippetsAPI.delete(snippet) |
| 56 |
.then(() => onSuccess?.()) |
| 57 |
.catch((error: unknown) => onError?.(error)) |
| 58 |
.finally(() => setIsWorking?.(false)) |
| 59 |
} |
| 60 |
|
| 61 |
const handleButtonClick = () => { |
| 62 |
if (snippet.active || snippet.trashed) { |
| 63 |
setIsDialogOpen(true) |
| 64 |
} else { |
| 65 |
handleDelete() |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return ( |
| 70 |
<> |
| 71 |
<Button className={className} {...buttonProps} onClick={handleButtonClick}> |
| 72 |
{snippet.trashed ? __('Delete Permanently', 'code-snippets') : __('Trash', 'code-snippets')} |
| 73 |
</Button> |
| 74 |
|
| 75 |
<ConfirmDialog |
| 76 |
open={isDialogOpen} |
| 77 |
title={__('Are you sure?', 'code-snippets')} |
| 78 |
confirmLabel={snippet.trashed ? __('Delete', 'code-snippets') : __('Trash', 'code-snippets')} |
| 79 |
confirmButtonClassName="is-destructive" |
| 80 |
onCancel={() => setIsDialogOpen(false)} |
| 81 |
onConfirm={() => { |
| 82 |
setIsDialogOpen(false) |
| 83 |
handleDelete() |
| 84 |
}} |
| 85 |
> |
| 86 |
{snippet.trashed ? <PermanentDeleteConfirmMessage /> : <TrashActiveConfirmMessage />} |
| 87 |
</ConfirmDialog> |
| 88 |
</> |
| 89 |
) |
| 90 |
} |
| 91 |
|