| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | import { __ } from '@wordpress/i18n' |
| 2 | 2 | import { isAxiosError } from 'axios' |
| 3 | 3 | import React, { useCallback } from 'react' |
| 4 | +import { describeRequestError } from '../utils/errors' | |
| 4 | 5 | import { useSnippetForm } from '../components/EditMenu/SnippetForm/WithSnippetFormContext' |
| 5 | 6 | import { createSnippetObject, isCondition } from '../utils/snippets/snippets' |
| 6 | 7 | import { buildUrl } from '../utils/urls' |
| 7 | 8 | import { useSnippetsAPI } from './useSnippetsAPI' |
| @@ -17,9 +18,9 @@ | ||
| 17 | 18 | updatedActivated: __('Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets'), |
| 18 | 19 | updatedDeactivated: __('Snippet <strong>updated</strong> and <strong>deactivated</strong>'), |
| 19 | 20 | updatedExecuted: __('Snippet <strong>updated</strong> and <strong>executed</strong>.', 'code-snippets'), |
| 20 | 21 | failedCreate: __('Could not create snippet.', 'code-snippets'), |
| 21 | - failedUpdate: __('Could not update snippet.', 'code-snippets') | |
| 22 | + failedUpdate: __('Could not update snippet.', 'code-snippets'), | |
| 22 | 23 | } as const |
| 23 | 24 | |
| 24 | 25 | const conditionCreated = __('Condition <strong>created</strong>.', 'code-snippets') |
| 25 | 26 | const conditionUpdated = __('Condition <strong>updated</strong>.', 'code-snippets') |
| @@ -89,9 +90,9 @@ | ||
| 89 | 90 | } |
| 90 | 91 | |
| 91 | 92 | export const useSubmitSnippet = (): UseSubmitSnippet => { |
| 92 | 93 | const api = useSnippetsAPI() |
| 93 | - const { setIsWorking, setCurrentNotice, setSnippet } = useSnippetForm() | |
| 94 | + const { acceptSnippet, setIsWorking, setCurrentNotice } = useSnippetForm() | |
| 94 | 95 | |
| 95 | 96 | const submitSnippet: UseSubmitSnippet['submitSnippet'] = useCallback(async (snippet, action) => { |
| 96 | 97 | setCurrentNotice(undefined) |
| 97 | 98 | setIsWorking(true) |
| @@ -112,10 +113,10 @@ | ||
| 112 | 113 | ? api.create(request) |
| 113 | 114 | : api.update({ ...request, id })) |
| 114 | 115 | |
| 115 | 116 | return response.id ? createSnippetObject(response) : undefined |
| 116 | - } catch (error) { | |
| 117 | - return isAxiosError(error) ? error.message : undefined | |
| 117 | + } catch (error: unknown) { | |
| 118 | + return isAxiosError(error) ? describeRequestError(error) : undefined | |
| 118 | 119 | } finally { |
| 119 | 120 | setIsWorking(false) |
| 120 | 121 | } |
| 121 | 122 | })() |
| @@ -131,9 +132,9 @@ | ||
| 131 | 132 | setCurrentNotice(['error', message.filter(Boolean).join(' ')]) |
| 132 | 133 | return undefined |
| 133 | 134 | } |
| 134 | 135 | |
| 135 | - setSnippet(result) | |
| 136 | + acceptSnippet(result) | |
| 136 | 137 | |
| 137 | 138 | if (result.code_error && SubmitSnippetAction.SAVE_AND_ACTIVATE === action) { |
| 138 | 139 | setCurrentNotice(getActivationErrorNotice(result)) |
| 139 | 140 | } else { |
| @@ -145,8 +146,8 @@ | ||
| 145 | 146 | window.history.replaceState({}, '', buildUrl(window.CODE_SNIPPETS?.urls.edit, { id: result.id })) |
| 146 | 147 | } |
| 147 | 148 | |
| 148 | 149 | return result |
| 149 | - }, [api, setIsWorking, setCurrentNotice, setSnippet]) | |
| 150 | + }, [acceptSnippet, api, setIsWorking, setCurrentNotice]) | |
| 150 | 151 | |
| 151 | 152 | return { submitSnippet } |
| 152 | 153 | } |