PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonationForms / resources / app / utilities / generateRequestErrors.ts

generateRequestErrors.ts in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/DonationForms/resources/app/utilities/generateRequestErrors.ts

35 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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