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