| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import { isAxiosError } from 'axios' |
| 3 |
import React, { useCallback } from 'react' |
| 4 |
import { describeRequestError } from '../utils/errors' |
| 5 |
import { useSnippetForm } from '../components/EditMenu/SnippetForm/WithSnippetFormContext' |
| 6 |
import { createSnippetObject, isCondition } from '../utils/snippets/snippets' |
| 7 |
import { buildUrl } from '../utils/urls' |
| 8 |
import { useSnippetsAPI } from './useSnippetsAPI' |
| 9 |
import type { Snippet } from '../types/Snippet' |
| 10 |
import type { ScreenNotice } from '../types/ScreenNotice' |
| 11 |
|
| 12 |
const snippetMessages = { |
| 13 |
addNew: __('Create New Snippet', 'code-snippets'), |
| 14 |
edit: __('Edit Snippet', 'code-snippets'), |
| 15 |
created: __('Snippet <strong>created</strong>.', 'code-snippets'), |
| 16 |
updated: __('Snippet <strong>updated</strong>.', 'code-snippets'), |
| 17 |
createdActivated: __('Snippet <strong>created</strong> and <strong>activated</strong>.', 'code-snippets'), |
| 18 |
updatedActivated: __('Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets'), |
| 19 |
updatedDeactivated: __('Snippet <strong>updated</strong> and <strong>deactivated</strong>'), |
| 20 |
updatedExecuted: __('Snippet <strong>updated</strong> and <strong>executed</strong>.', 'code-snippets'), |
| 21 |
failedCreate: __('Could not create snippet.', 'code-snippets'), |
| 22 |
failedUpdate: __('Could not update snippet.', 'code-snippets'), |
| 23 |
} as const |
| 24 |
|
| 25 |
const conditionCreated = __('Condition <strong>created</strong>.', 'code-snippets') |
| 26 |
const conditionUpdated = __('Condition <strong>updated</strong>.', 'code-snippets') |
| 27 |
|
| 28 |
const conditionMessages: typeof snippetMessages = { |
| 29 |
addNew: __('Create New Condition', 'code-snippets'), |
| 30 |
edit: __('Edit Condition', 'code-snippets'), |
| 31 |
created: conditionCreated, |
| 32 |
updated: conditionUpdated, |
| 33 |
createdActivated: conditionCreated, |
| 34 |
updatedActivated: conditionUpdated, |
| 35 |
updatedDeactivated: conditionUpdated, |
| 36 |
updatedExecuted: conditionUpdated, |
| 37 |
failedCreate: __('Could not create condition.', 'code-snippets'), |
| 38 |
failedUpdate: __('Could not update condition.', 'code-snippets') |
| 39 |
} |
| 40 |
|
| 41 |
export enum SubmitSnippetAction { |
| 42 |
SAVE = 'save_snippet', |
| 43 |
SAVE_AND_ACTIVATE = 'save_snippet_activate', |
| 44 |
SAVE_AND_EXECUTE = 'save_snippet_execute', |
| 45 |
SAVE_AND_DEACTIVATE = 'save_snippet_deactivate' |
| 46 |
} |
| 47 |
|
| 48 |
const getSuccessNotice = ( |
| 49 |
request: Partial<Snippet>, |
| 50 |
response: Snippet, |
| 51 |
action: SubmitSnippetAction = SubmitSnippetAction.SAVE |
| 52 |
): string => { |
| 53 |
const messages = 'condition' === request.scope ? conditionMessages : snippetMessages |
| 54 |
const wasCreated = 0 === request.id |
| 55 |
|
| 56 |
switch (action) { |
| 57 |
case SubmitSnippetAction.SAVE: |
| 58 |
return wasCreated ? messages.created : messages.updated |
| 59 |
|
| 60 |
case SubmitSnippetAction.SAVE_AND_EXECUTE: |
| 61 |
return messages.updatedExecuted |
| 62 |
|
| 63 |
case SubmitSnippetAction.SAVE_AND_ACTIVATE: |
| 64 |
if ('single-use' === response.scope) { |
| 65 |
return messages.updatedExecuted |
| 66 |
} else { |
| 67 |
return wasCreated |
| 68 |
? messages.createdActivated |
| 69 |
: messages.updatedActivated |
| 70 |
} |
| 71 |
|
| 72 |
case SubmitSnippetAction.SAVE_AND_DEACTIVATE: |
| 73 |
return messages.updatedDeactivated |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
const getActivationErrorNotice = (snippet: Snippet): ScreenNotice => [ |
| 78 |
'error', |
| 79 |
__('Snippet could not be activated.', 'code-snippets'), |
| 80 |
<span key="code-snippets-activation-error"> |
| 81 |
{__('The snippet was saved, but remains inactive due to this error:', 'code-snippets')} |
| 82 |
{' '} |
| 83 |
<strong>{snippet.code_error?.[0] ?? ''}</strong> |
| 84 |
</span>, |
| 85 |
snippet.code_error_trace ?? undefined |
| 86 |
] |
| 87 |
|
| 88 |
export interface UseSubmitSnippet { |
| 89 |
submitSnippet: (snippet: Partial<Snippet> & Pick<Snippet, 'network'>, action?: SubmitSnippetAction) => Promise<Snippet | undefined> |
| 90 |
} |
| 91 |
|
| 92 |
export const useSubmitSnippet = (): UseSubmitSnippet => { |
| 93 |
const api = useSnippetsAPI() |
| 94 |
const { acceptSnippet, setIsWorking, setCurrentNotice } = useSnippetForm() |
| 95 |
|
| 96 |
const submitSnippet: UseSubmitSnippet['submitSnippet'] = useCallback(async (snippet, action) => { |
| 97 |
setCurrentNotice(undefined) |
| 98 |
setIsWorking(true) |
| 99 |
|
| 100 |
const request = { ...snippet } |
| 101 |
|
| 102 |
if (SubmitSnippetAction.SAVE_AND_ACTIVATE === action) { |
| 103 |
request.active = true |
| 104 |
} else if (SubmitSnippetAction.SAVE_AND_DEACTIVATE === action) { |
| 105 |
request.active = false |
| 106 |
} |
| 107 |
|
| 108 |
const result = await (async (): Promise<Snippet | string | undefined> => { |
| 109 |
try { |
| 110 |
const { id } = request |
| 111 |
|
| 112 |
const response = await (undefined === id || 0 === id |
| 113 |
? api.create(request) |
| 114 |
: api.update({ ...request, id })) |
| 115 |
|
| 116 |
return response.id ? createSnippetObject(response) : undefined |
| 117 |
} catch (error: unknown) { |
| 118 |
return isAxiosError(error) ? describeRequestError(error) : undefined |
| 119 |
} finally { |
| 120 |
setIsWorking(false) |
| 121 |
} |
| 122 |
})() |
| 123 |
|
| 124 |
const messages = isCondition(snippet) ? conditionMessages : snippetMessages |
| 125 |
|
| 126 |
if (undefined === result || 'string' === typeof result) { |
| 127 |
const message = [ |
| 128 |
request.id ? messages.failedUpdate : messages.failedCreate, |
| 129 |
result ?? __('The server did not send a valid response.', 'code-snippets') |
| 130 |
] |
| 131 |
|
| 132 |
setCurrentNotice(['error', message.filter(Boolean).join(' ')]) |
| 133 |
return undefined |
| 134 |
} |
| 135 |
|
| 136 |
acceptSnippet(result) |
| 137 |
|
| 138 |
if (result.code_error && SubmitSnippetAction.SAVE_AND_ACTIVATE === action) { |
| 139 |
setCurrentNotice(getActivationErrorNotice(result)) |
| 140 |
} else { |
| 141 |
setCurrentNotice(['updated', getSuccessNotice(snippet, result, action)]) |
| 142 |
} |
| 143 |
|
| 144 |
if (request.id && result.id) { |
| 145 |
window.document.title = window.document.title.replace(snippetMessages.addNew, messages.edit) |
| 146 |
window.history.replaceState({}, '', buildUrl(window.CODE_SNIPPETS?.urls.edit, { id: result.id })) |
| 147 |
} |
| 148 |
|
| 149 |
return result |
| 150 |
}, [acceptSnippet, api, setIsWorking, setCurrentNotice]) |
| 151 |
|
| 152 |
return { submitSnippet } |
| 153 |
} |
| 154 |
|