| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import { addQueryArgs } from '@wordpress/url' |
| 3 |
import { isAxiosError } from 'axios' |
| 4 |
import { useCallback } from 'react' |
| 5 |
import { createSnippetObject, isCondition } from '../utils/snippets/snippets' |
| 6 |
import { useRestAPI } from './useRestAPI' |
| 7 |
import { useSnippetForm } from './useSnippetForm' |
| 8 |
import type { Snippet } from '../types/Snippet' |
| 9 |
|
| 10 |
const snippetMessages = <const> { |
| 11 |
addNew: __('Add New Snippet', 'code-snippets'), |
| 12 |
edit: __('Edit Snippet', 'code-snippets'), |
| 13 |
created: __('Snippet <strong>created</strong>.', 'code-snippets'), |
| 14 |
updated: __('Snippet <strong>updated</strong>.', 'code-snippets'), |
| 15 |
createdActivated: __('Snippet <strong>created</strong> and <strong>activated</strong>.', 'code-snippets'), |
| 16 |
updatedActivated: __('Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets'), |
| 17 |
updatedDeactivated: __('Snippet <strong>updated</strong> and <strong>deactivated</strong>'), |
| 18 |
updatedExecuted: __('Snippet <strong>updated</strong> and <strong>executed</strong>.', 'code-snippets'), |
| 19 |
failedCreate: __('Could not create snippet.', 'code-snippets'), |
| 20 |
failedUpdate: __('Could not update snippet.', 'code-snippets') |
| 21 |
} |
| 22 |
|
| 23 |
const conditionCreated = __('Condition <strong>created</strong>.', 'code-snippets') |
| 24 |
const conditionUpdated = __('Condition <strong>updated</strong>.', 'code-snippets') |
| 25 |
|
| 26 |
const conditionMessages: typeof snippetMessages = { |
| 27 |
addNew: __('Add New Condition', 'code-snippets'), |
| 28 |
edit: __('Edit Condition', 'code-snippets'), |
| 29 |
created: conditionCreated, |
| 30 |
updated: conditionUpdated, |
| 31 |
createdActivated: conditionCreated, |
| 32 |
updatedActivated: conditionUpdated, |
| 33 |
updatedDeactivated: conditionUpdated, |
| 34 |
updatedExecuted: conditionUpdated, |
| 35 |
failedCreate: __('Could not create condition.', 'code-snippets'), |
| 36 |
failedUpdate: __('Could not update condition.', 'code-snippets') |
| 37 |
} |
| 38 |
|
| 39 |
export enum SubmitSnippetAction { |
| 40 |
SAVE = 'save_snippet', |
| 41 |
SAVE_AND_ACTIVATE = 'save_snippet_activate', |
| 42 |
SAVE_AND_EXECUTE = 'save_snippet_execute', |
| 43 |
SAVE_AND_DEACTIVATE = 'save_snippet_deactivate' |
| 44 |
} |
| 45 |
|
| 46 |
const getSuccessNotice = (request: Snippet, response: Snippet, action: SubmitSnippetAction): string => { |
| 47 |
const messages = 'condition' === request.scope ? conditionMessages : snippetMessages |
| 48 |
const wasCreated = 0 === request.id |
| 49 |
|
| 50 |
switch (action) { |
| 51 |
case SubmitSnippetAction.SAVE: |
| 52 |
return wasCreated ? messages.created : messages.updated |
| 53 |
|
| 54 |
case SubmitSnippetAction.SAVE_AND_EXECUTE: |
| 55 |
return messages.updatedExecuted |
| 56 |
|
| 57 |
case SubmitSnippetAction.SAVE_AND_ACTIVATE: |
| 58 |
if ('single-use' === response.scope) { |
| 59 |
return messages.updatedExecuted |
| 60 |
} else { |
| 61 |
return wasCreated |
| 62 |
? messages.createdActivated |
| 63 |
: messages.updatedActivated |
| 64 |
} |
| 65 |
|
| 66 |
case SubmitSnippetAction.SAVE_AND_DEACTIVATE: |
| 67 |
return messages.updatedDeactivated |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
const SUBMIT_ACTION_DELTA: Record<SubmitSnippetAction, Partial<Snippet>> = { |
| 72 |
[SubmitSnippetAction.SAVE]: {}, |
| 73 |
[SubmitSnippetAction.SAVE_AND_ACTIVATE]: { active: true }, |
| 74 |
[SubmitSnippetAction.SAVE_AND_DEACTIVATE]: { active: false }, |
| 75 |
[SubmitSnippetAction.SAVE_AND_EXECUTE]: { active: true } |
| 76 |
} |
| 77 |
|
| 78 |
export interface UseSubmitSnippet { |
| 79 |
submitSnippet: (action?: SubmitSnippetAction) => Promise<Snippet | undefined> |
| 80 |
} |
| 81 |
|
| 82 |
export const useSubmitSnippet = (): UseSubmitSnippet => { |
| 83 |
const { snippetsAPI } = useRestAPI() |
| 84 |
const { setIsWorking, setCurrentNotice, snippet, setSnippet } = useSnippetForm() |
| 85 |
|
| 86 |
const submitSnippet = useCallback(async (action: SubmitSnippetAction = SubmitSnippetAction.SAVE) => { |
| 87 |
setCurrentNotice(undefined) |
| 88 |
|
| 89 |
const result = await (async (): Promise<Snippet | string | undefined> => { |
| 90 |
try { |
| 91 |
const request: Snippet = { ...snippet, ...SUBMIT_ACTION_DELTA[action] } |
| 92 |
const response = await (0 === request.id ? snippetsAPI.create(request) : snippetsAPI.update(request)) |
| 93 |
setIsWorking(false) |
| 94 |
return response.id ? response : undefined |
| 95 |
} catch (error) { |
| 96 |
setIsWorking(false) |
| 97 |
return isAxiosError(error) ? error.message : undefined |
| 98 |
} |
| 99 |
})() |
| 100 |
|
| 101 |
const messages = isCondition(snippet) ? conditionMessages : snippetMessages |
| 102 |
|
| 103 |
if (undefined === result || 'string' === typeof result) { |
| 104 |
const message = [ |
| 105 |
snippet.id ? messages.failedUpdate : messages.failedCreate, |
| 106 |
result ?? __('The server did not send a valid response.', 'code-snippets') |
| 107 |
] |
| 108 |
|
| 109 |
setCurrentNotice(['error', message.filter(Boolean).join(' ')]) |
| 110 |
return undefined |
| 111 |
} else { |
| 112 |
setSnippet(createSnippetObject(result)) |
| 113 |
setCurrentNotice(['updated', getSuccessNotice(snippet, result, action)]) |
| 114 |
|
| 115 |
if (snippet.id && result.id) { |
| 116 |
window.document.title = window.document.title.replace(snippetMessages.addNew, messages.edit) |
| 117 |
window.history.replaceState({}, '', addQueryArgs(window.CODE_SNIPPETS?.urls.edit, { id: result.id })) |
| 118 |
} |
| 119 |
|
| 120 |
return result |
| 121 |
} |
| 122 |
}, [snippetsAPI, setIsWorking, setCurrentNotice, snippet, setSnippet]) |
| 123 |
|
| 124 |
return { submitSnippet } |
| 125 |
} |
| 126 |
|