| 1 |
import React from 'react' |
| 2 |
import classnames from 'classnames' |
| 3 |
import { __ } from '@wordpress/i18n' |
| 4 |
import { useSnippetsAPI } from '../../../../hooks/useSnippetsAPI' |
| 5 |
import { TooltipButton } from '../../../common/TooltipButton' |
| 6 |
import { useSnippetForm } from '../../SnippetForm/WithSnippetFormContext' |
| 7 |
|
| 8 |
export const LockControl: React.FC = () => { |
| 9 |
const { update } = useSnippetsAPI() |
| 10 |
const { acceptSnippet, snippet, setSnippet, isWorking, setIsWorking, setCurrentNotice } = useSnippetForm() |
| 11 |
|
| 12 |
const handleToggle = () => { |
| 13 |
setIsWorking(true) |
| 14 |
setSnippet(previous => ({ ...previous, locked: !previous.locked })) |
| 15 |
|
| 16 |
update({ id: snippet.id, network: snippet.network, locked: !snippet.locked }) |
| 17 |
.then(result => { |
| 18 |
acceptSnippet(result) |
| 19 |
|
| 20 |
setCurrentNotice(['updated', result.locked |
| 21 |
? __('Snippet <strong>locked</strong>.', 'code-snippets') |
| 22 |
: __('Snippet <strong>unlocked</strong>.', 'code-snippets')]) |
| 23 |
}) |
| 24 |
.catch(() => setCurrentNotice(['error', __('Unable to lock snippet.', 'code-snippets')])) |
| 25 |
.finally(() => setIsWorking(false)) |
| 26 |
} |
| 27 |
|
| 28 |
return ( |
| 29 |
<div className={classnames('snippet-lock-control', { 'tooltip tooltip-block tooltip-end': !isWorking })}> |
| 30 |
<TooltipButton |
| 31 |
small block end |
| 32 |
primary={snippet.locked} |
| 33 |
className="snippet-lock-button" |
| 34 |
containerClassName="snippet-lock-control" |
| 35 |
disabled={isWorking} |
| 36 |
onClick={handleToggle} |
| 37 |
tooltip={snippet.locked |
| 38 |
? __('Unlock this snippet to allow editing and deletion.', 'code-snippets') |
| 39 |
: __('Mark this snippet as read-only to prevent accidental changes or deletion.', 'code-snippets')} |
| 40 |
> |
| 41 |
{snippet.locked |
| 42 |
? <span className="dashicons dashicons-unlock" aria-hidden="true" /> |
| 43 |
: <span className="dashicons dashicons-lock" aria-hidden="true" />} |
| 44 |
</TooltipButton> |
| 45 |
</div> |
| 46 |
) |
| 47 |
} |
| 48 |
|