| 1 |
import React from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { useSnippetsAPI } from '../../../../hooks/useSnippetsAPI' |
| 4 |
import { Tooltip } from '../../../common/Tooltip' |
| 5 |
import { useSnippetForm } from '../../SnippetForm/WithSnippetFormContext' |
| 6 |
|
| 7 |
export const LockControl: React.FC = () => { |
| 8 |
const { snippet, setSnippet, isWorking, setIsWorking, setCurrentNotice } = useSnippetForm() |
| 9 |
const { update } = useSnippetsAPI() |
| 10 |
|
| 11 |
const handleToggle = () => { |
| 12 |
setSnippet(previous => ({ ...previous, locked: !previous.locked })) |
| 13 |
setIsWorking(true) |
| 14 |
|
| 15 |
update({ id: snippet.id, network: snippet.network, locked: !snippet.locked }) |
| 16 |
.then(result => { |
| 17 |
setSnippet(result) |
| 18 |
|
| 19 |
setCurrentNotice(['updated', result.locked |
| 20 |
? __('Snippet <strong>locked</strong>.', 'code-snippets') |
| 21 |
: __('Snippet <strong>unlocked</strong>.', 'code-snippets')]) |
| 22 |
}) |
| 23 |
.catch(() => setCurrentNotice(['error', __('Unable to lock snippet.', 'code-snippets')])) |
| 24 |
.finally(() => setIsWorking(false)) |
| 25 |
} |
| 26 |
|
| 27 |
return ( |
| 28 |
<div className="inline-form-field lock-control-container"> |
| 29 |
<label htmlFor="snippet-lock" id="snippet-lock-label"> |
| 30 |
{__('Lock snippet', 'code-snippets')} |
| 31 |
</label> |
| 32 |
|
| 33 |
<Tooltip block end> |
| 34 |
{__('Mark this snippet as read-only to prevent accidental changes or deletion.', 'code-snippets')} |
| 35 |
</Tooltip> |
| 36 |
|
| 37 |
<span className="lock-status-text"> |
| 38 |
{snippet.locked ? __('Locked', 'code-snippets') : __('Unlocked', 'code-snippets')} |
| 39 |
</span> |
| 40 |
|
| 41 |
<input |
| 42 |
id="snippet-lock" |
| 43 |
type="checkbox" |
| 44 |
checked={snippet.locked} |
| 45 |
disabled={isWorking} |
| 46 |
className="switch" |
| 47 |
onChange={handleToggle} |
| 48 |
aria-labelledby="snippet-lock-label" |
| 49 |
/> |
| 50 |
</div> |
| 51 |
) |
| 52 |
} |
| 53 |
|