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
1 year ago
getCurrentFormUrlData.js
1 year 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
1 year ago
handleFormSubmitRequest.ts
1 year ago
handleValidationRequest.ts
2 years ago
isRouteInlineRedirect.ts
2 years ago
memoNode.ts
2 years ago
mountWindowData.ts
1 year 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
35 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 | const fieldElement: HTMLInputElement = document.querySelector('input[name="' + field + '"]'); |
| 11 | const canFocus = fieldElement && fieldElement.type !== 'hidden'; |
| 12 | |
| 13 | setError(field, {message: Array.isArray(value) ? value[0] : value}, {shouldFocus: canFocus}); |
| 14 | |
| 15 | if (!canFocus) { |
| 16 | // In fields that aren't inputs by default or are hidden inputs, we need to use this workaround because the "shouldFocus" option will not work in these cases. |
| 17 | if (!fieldElement) { |
| 18 | const fieldElementContainer = document.querySelector('.givewp-fields-' + field); //E.g: <div class="givewp-fields-giftAid">content...</div> |
| 19 | fieldElementContainer?.scrollIntoView({behavior: 'smooth'}); |
| 20 | } else { |
| 21 | fieldElement.parentElement.scrollIntoView({behavior: 'smooth'}); // E.g: <input type="hidden" name="state"> |
| 22 | } |
| 23 | } |
| 24 | } else if (field === 'gateway_error') { |
| 25 | setError('FORM_ERROR', {message: Array.isArray(value) ? value[0] : value}); |
| 26 | } else { |
| 27 | setError('FORM_ERROR', { |
| 28 | message: __('Something went wrong, please try again or contact support.', 'give'), |
| 29 | }); |
| 30 | } |
| 31 | }); |
| 32 | }; |
| 33 | |
| 34 | export default generateRequestErrors; |
| 35 |