PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / EditMenu / EditorSidebar / controls / LockControl.tsx

LockControl.tsx in Code Snippets 4.0.0-beta.1, at js/components/EditMenu/EditorSidebar/controls/LockControl.tsx

53 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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