ConvertFieldAPIRulesToJoi.ts
2 years ago
PrepareFormData.ts
2 years ago
amountFormatter.ts
2 years ago
buildRegisterValidationOptions.ts
2 years ago
conditionOperatorFunctions.js
2 years ago
convertValuesToFormData.ts
2 years ago
createValidationObjectFromFields.js
2 years ago
generateRequestErrors.ts
2 years ago
getCurrentFormUrlData.js
2 years ago
getDefaultValuesFromSections.ts
2 years ago
getDonationFormNodeSettings.ts
2 years ago
getErrorByFieldName.ts
2 years ago
getWindowData.ts
2 years ago
groups.ts
2 years ago
handleFormRedirect.ts
2 years ago
handleFormSubmitRequest.ts
2 years ago
handleValidationRequest.ts
2 years ago
isRouteInlineRedirect.ts
2 years ago
memoNode.ts
2 years ago
mountWindowData.ts
2 years ago
postData.ts
2 years ago
postFormData.ts
2 years ago
registerFieldAndBuildProps.tsx
2 years ago
setDesignSettings.ts
2 years ago
useDonationFormPubSub.ts
2 years ago
generateRequestErrors.ts
22 lines
| 1 | /** |
| 2 | * Takes a try-catch request exception and sets errors for React Hook Form to consume |
| 3 | */ |
| 4 | import {UseFormSetError} from 'react-hook-form'; |
| 5 | import {__} from '@wordpress/i18n'; |
| 6 | |
| 7 | const generateRequestErrors = (values: Record<string, any>, errors: object[], setError: UseFormSetError<any>) => { |
| 8 | Object.entries(errors).forEach(([field, value]) => { |
| 9 | if (Object.keys(values).includes(field)) { |
| 10 | setError(field, {message: Array.isArray(value) ? value[0] : value}); |
| 11 | } else if (field === 'gateway_error') { |
| 12 | setError('FORM_ERROR', {message: Array.isArray(value) ? value[0] : value}); |
| 13 | } else { |
| 14 | setError('FORM_ERROR', { |
| 15 | message: __('Something went wrong, please try again or contact support.', 'give'), |
| 16 | }); |
| 17 | } |
| 18 | }); |
| 19 | }; |
| 20 | |
| 21 | export default generateRequestErrors; |
| 22 |