| 1 |
import React from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { SubmitSnippetAction } from '../../../hooks/useSubmitSnippet' |
| 4 |
import { isCondition } from '../../../utils/snippets/snippets' |
| 5 |
import { isNetworkAdmin } from '../../../utils/screen' |
| 6 |
import { useSnippetForm } from '../../../hooks/useSnippetForm' |
| 7 |
import { SubmitButton } from '../../common/SubmitButton' |
| 8 |
import type { SubmitButtonProps } from '../../common/SubmitButton' |
| 9 |
|
| 10 |
const SaveButton = (props: SubmitButtonProps) => { |
| 11 |
const { snippet } = useSnippetForm() |
| 12 |
|
| 13 |
return ( |
| 14 |
<SubmitButton |
| 15 |
large |
| 16 |
name={SubmitSnippetAction.SAVE} |
| 17 |
text={isCondition(snippet) |
| 18 |
? __('Save Condition', 'code-snippets') |
| 19 |
: __('Save Snippet', 'code-snippets')} |
| 20 |
{...props} |
| 21 |
/> |
| 22 |
) |
| 23 |
} |
| 24 |
|
| 25 |
interface ActivateOrDeactivateButtonProps { |
| 26 |
primaryActivate: boolean |
| 27 |
} |
| 28 |
|
| 29 |
const ActivateOrDeactivateButton: React.FC<ActivateOrDeactivateButtonProps> = ({ primaryActivate }) => { |
| 30 |
const { snippet, isWorking } = useSnippetForm() |
| 31 |
|
| 32 |
switch (true) { |
| 33 |
case isCondition(snippet) || snippet.shared_network && isNetworkAdmin(): |
| 34 |
return null |
| 35 |
|
| 36 |
case 'single-use' === snippet.scope: |
| 37 |
return ( |
| 38 |
<SubmitButton |
| 39 |
large |
| 40 |
name={SubmitSnippetAction.SAVE_AND_EXECUTE} |
| 41 |
disabled={isWorking} |
| 42 |
text={__('Save and Execute Once', 'code-snippets')} |
| 43 |
/> |
| 44 |
) |
| 45 |
|
| 46 |
case snippet.active: |
| 47 |
return ( |
| 48 |
<SubmitButton |
| 49 |
name={SubmitSnippetAction.SAVE_AND_DEACTIVATE} |
| 50 |
disabled={isWorking} |
| 51 |
large |
| 52 |
text={__('Save and Deactivate', 'code-snippets')} |
| 53 |
/> |
| 54 |
) |
| 55 |
|
| 56 |
default: |
| 57 |
case !snippet.active: |
| 58 |
return ( |
| 59 |
<SubmitButton |
| 60 |
name={SubmitSnippetAction.SAVE_AND_ACTIVATE} |
| 61 |
primary={primaryActivate} |
| 62 |
disabled={isWorking} |
| 63 |
large |
| 64 |
text={__('Save and Activate', 'code-snippets')} |
| 65 |
/> |
| 66 |
) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
export const SubmitButtons: React.FC = () => { |
| 71 |
const { snippet } = useSnippetForm() |
| 72 |
|
| 73 |
const activateByDefault = |
| 74 |
!!window.CODE_SNIPPETS_EDIT?.activateByDefault && |
| 75 |
!snippet.active && 'single-use' !== snippet.scope && |
| 76 |
(!snippet.shared_network || !isNetworkAdmin()) |
| 77 |
|
| 78 |
return <> |
| 79 |
{activateByDefault && <SaveButton primary={!activateByDefault} />} |
| 80 |
<ActivateOrDeactivateButton primaryActivate={activateByDefault} /> |
| 81 |
{!activateByDefault && <SaveButton primary />} |
| 82 |
</> |
| 83 |
} |
| 84 |
|