homeflowjs 0.13.53 → 0.13.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/recaptcha.js CHANGED
@@ -60,46 +60,65 @@ export default () => {
60
60
  }
61
61
  }
62
62
 
63
- function addRecaptchaV2(formSelectors) {
64
- const invisibleRecaptcha = document.createElement('div');
65
- invisibleRecaptcha.classList.add('g-recaptcha');
66
- invisibleRecaptcha.dataset.sitekey = '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY';
67
- invisibleRecaptcha.dataset.callback = 'submitRecaptchaForm';
68
- invisibleRecaptcha.dataset.size = 'invisible';
69
-
63
+ const appendRecaptchaScript = () => {
70
64
  const recaptchaScript = document.createElement('script');
71
65
  recaptchaScript.src = 'https://www.google.com/recaptcha/api.js';
72
66
  recaptchaScript.id = 'recaptcha-script';
73
67
 
68
+ document.body.appendChild(recaptchaScript);
69
+ }
70
+
71
+ const onRecaptchaReady = (formSelectors) => {
72
+ if (window.recaptchaLoadingScriptInterval) {
73
+ clearTimeout(window.recaptchaLoadingScriptInterval);
74
+ window.recaptchaLoadingScriptInterval = undefined;
75
+ }
76
+
74
77
  const forms = document.querySelectorAll(formSelectors.join(', '));
75
- forms.forEach((form) => {
76
- form.appendChild(invisibleRecaptcha);
77
- });
78
78
 
79
- // add the recaptcha script only after a form input is focused
80
- for (let i = 0; i < formSelectors.length; i++) {
81
- const formInputs = document.querySelectorAll(`${formSelectors[i]} input, ${formSelectors[i]} textarea`);
82
- formInputs.forEach((input) => {
83
- input.addEventListener('focus', () => {
84
- if (!document.getElementById('recaptcha-script')) {
85
- document.body.appendChild(recaptchaScript);
86
- }
87
- });
79
+ forms.forEach((form, i) => {
80
+ const formRecaptchaWrapper = document.createElement('div');
81
+ formRecaptchaWrapper.id = `recaptcha-wrapper-${i}`;
82
+
83
+ form.appendChild(formRecaptchaWrapper);
84
+
85
+ const onRecaptchaSubmit = () => {
86
+ form.submit();
87
+ Homeflow.kickEvent('after_successful_recaptcha', form);
88
+ }
89
+
90
+ const widget = window.grecaptcha.render(`recaptcha-wrapper-${i}`, {
91
+ sitekey: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
92
+ callback: onRecaptchaSubmit,
93
+ size: 'invisible',
88
94
  });
89
95
 
90
- const forms = document.querySelectorAll(formSelectors[i]);
91
- forms.forEach((form) => {
92
- form.addEventListener('submit', (e) => {
93
- if (e.target.classList.contains('hfjs-form-invalid')) return;
94
- if (!document.getElementById('g-recaptcha-response') || !document.getElementById('g-recaptcha-response').value) {
95
- e.preventDefault();
96
- Homeflow.set('submitted_form', e.target);
97
- grecaptcha.execute();
98
- }
99
- });
100
- form.classList.add('has-recaptcha');
96
+ window.widgetIds = window.widgetIds
97
+ ? [...window.widgetIds, widget]
98
+ : [widget]
99
+
100
+ form.addEventListener('submit', (e) => {
101
+ if (e.target.classList.contains('hfjs-form-invalid')) {
102
+ return;
103
+ }
104
+
105
+ e.preventDefault();
106
+ Homeflow.set('submitted_form', e.target);
107
+ grecaptcha.execute(widget);
101
108
  });
109
+ });
110
+ }
111
+
112
+ function addRecaptchaV2(formSelectors) {
113
+ if (!document.getElementById('recaptcha-script')) {
114
+ appendRecaptchaScript();
102
115
  }
116
+
117
+ window.recaptchaLoadingScriptInterval = setInterval(() => {
118
+ if (window.grecaptcha?.render) {
119
+ onRecaptchaReady(formSelectors)
120
+ }
121
+ }, 300);
103
122
  }
104
123
 
105
124
  if (Homeflow.get('recaptcha_version') === 3) {
package/hooks/index.js CHANGED
@@ -5,6 +5,7 @@ import { useOnScreen } from './use-on-screen';
5
5
  import usePropertyInfiniteScroll from './use-property-inifinite-scroll';
6
6
  import useLoadBranches from './use-load-branches';
7
7
  import useLoadPreviousProperties from './use-load-previous-properties';
8
+ import useRecaptcha from './use-recaptcha';
8
9
 
9
10
  export {
10
11
  useDefaultSort,
@@ -14,4 +15,5 @@ export {
14
15
  usePropertyInfiniteScroll,
15
16
  useLoadBranches,
16
17
  useLoadPreviousProperties,
18
+ useRecaptcha
17
19
  };
@@ -0,0 +1,142 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+
3
+ // Currenly supports only Recaptcha V2
4
+
5
+ const RECAPTCHA_CONFIG = {
6
+ KEY: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
7
+ FILED_NAME: 'g-recaptcha-response',
8
+ SCRIPT_ID: 'recaptcha-script',
9
+ SCRIPT_LINK: 'https://www.google.com/recaptcha/api.js?render=explicit'
10
+ };
11
+
12
+ const useRecaptcha = ({
13
+ formID,
14
+ formRef,
15
+ onRecaptchaSubmit,
16
+ shouldKickGAEvent = true,
17
+ recaptchaSize = 'invisible',
18
+ }) => {
19
+ const RECAPTCHA_CONTAINER_ID = `recaptcha-wrapper-${formID}`;
20
+
21
+ const [stateWidget, setStateWidget] = useState(null);
22
+ const [stateIsRecaptchaLoaded, setStateIsRecaptchaLoaded] = useState(false);
23
+ const [stateIntervalId, setStateIntervalId] = useState(null);
24
+
25
+ const loadRecaptchaScript = () => new Promise(
26
+ (resolve, reject) => {
27
+ const recaptchaScript = document.createElement('script');
28
+
29
+ recaptchaScript.onload = resolve;
30
+ recaptchaScript.onerror = reject;
31
+ recaptchaScript.id = RECAPTCHA_CONFIG.SCRIPT_ID;
32
+ recaptchaScript.src = RECAPTCHA_CONFIG.SCRIPT_LINK;
33
+ document.body.appendChild(recaptchaScript);
34
+ },
35
+ );
36
+
37
+ const onAppendRecaptchaContainer = () => {
38
+ const recaptchaContainer = document.createElement('div');
39
+ recaptchaContainer.id = RECAPTCHA_CONTAINER_ID;
40
+
41
+ formRef.current.appendChild(recaptchaContainer);
42
+ };
43
+
44
+ const onResetRecaptcha = useCallback(() => {
45
+ window.grecaptcha.reset(stateWidget);
46
+ const currentRecaptchaContainer = document.getElementById(RECAPTCHA_CONTAINER_ID);
47
+
48
+ if (currentRecaptchaContainer) {
49
+ currentRecaptchaContainer.remove();
50
+ }
51
+ }, [stateWidget]);
52
+
53
+ const waitForRecaptchaScript = () => {
54
+ const intervalId = setInterval(() => {
55
+ if (window.grecaptcha?.render) {
56
+ setStateIsRecaptchaLoaded(true);
57
+ }
58
+ }, 500);
59
+ setStateIntervalId(intervalId);
60
+ };
61
+
62
+ /*
63
+ * has to be rendered on form submit and not earlier
64
+ * so that onRecaptchaSubmit function has updated reference
65
+ * and uses updated React states
66
+ */
67
+ const triggerRecaptchaRender = useCallback(() => {
68
+ const currentRecaptchaContainer = document.getElementById(RECAPTCHA_CONTAINER_ID);
69
+
70
+ if (!currentRecaptchaContainer) {
71
+ onAppendRecaptchaContainer();
72
+ }
73
+
74
+ const widget = window.grecaptcha.render(`recaptcha-wrapper-${formID}`, {
75
+ sitekey: RECAPTCHA_CONFIG.KEY,
76
+ callback: (token) => onRecaptchaSubmit({
77
+ [RECAPTCHA_CONFIG.FILED_NAME]: token
78
+ }),
79
+ size: recaptchaSize,
80
+ });
81
+
82
+ setStateWidget(widget);
83
+ }, [onRecaptchaSubmit]);
84
+
85
+ useEffect(() => {
86
+ if (stateIsRecaptchaLoaded) {
87
+ clearInterval(stateIntervalId);
88
+ triggerRecaptchaRender();
89
+ }
90
+ }, [stateIsRecaptchaLoaded]);
91
+
92
+ const onRecaptchaRender = useCallback(() => {
93
+ const isRecaptchaTokenReady = formRef
94
+ .current
95
+ .querySelector(`[name="${RECAPTCHA_CONFIG.FILED_NAME}"]`);
96
+
97
+ if (isRecaptchaTokenReady){
98
+ return;
99
+ } else {
100
+ if (window.grecaptcha?.render) {
101
+ triggerRecaptchaRender();
102
+ } else {
103
+ waitForRecaptchaScript();
104
+ }
105
+ }
106
+ }, [triggerRecaptchaRender]);
107
+
108
+ const onRejectRecaptcha = () => {
109
+ console.error('Recaptcha setup error');
110
+ };
111
+
112
+ const executeRecaptcha = () => {
113
+ window.grecaptcha.execute(stateWidget);
114
+ /*
115
+ * some of the theme calls GA event on form submition
116
+ * so in order to prevent duplication please pass shouldKickGAEvent prop
117
+ */
118
+ if (shouldKickGAEvent) {
119
+ Homeflow.kickEvent('after_successful_recaptcha', formRef.current);
120
+ }
121
+ };
122
+
123
+ useEffect(() => {
124
+ if (stateWidget !== null) {
125
+ executeRecaptcha();
126
+ }
127
+ }, [stateWidget]);
128
+
129
+ useEffect(() => {
130
+ if (!document.getElementById('recaptcha-script')) {
131
+ loadRecaptchaScript()
132
+ .catch(onRejectRecaptcha);
133
+ }
134
+ }, []);
135
+
136
+ return {
137
+ onRecaptchaRender,
138
+ onResetRecaptcha,
139
+ };
140
+ };
141
+
142
+ export default useRecaptcha;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "0.13.53",
3
+ "version": "0.13.55",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",
@@ -28,8 +28,13 @@ const UserRegisterForm = (props) => {
28
28
 
29
29
  const handleSubmit = (e) => {
30
30
  e.preventDefault();
31
+ const { body } = Object.fromEntries(new FormData(e.target));
32
+ const formData = {
33
+ ...localUser,
34
+ body,
35
+ };
31
36
 
32
- const userParams = doNotContact ? { ...localUser, do_not_contact: 1 } : localUser;
37
+ const userParams = doNotContact ? { ...formData, do_not_contact: 1 } : formData;
33
38
  userParams.marketing_preferences = {
34
39
  ...localUser.marketing_preferences, opt_in_marketing_statement: marketingStatement,
35
40
  };
@@ -58,6 +63,11 @@ const UserRegisterForm = (props) => {
58
63
 
59
64
  return (
60
65
  <form onSubmit={handleSubmit} {...otherProps}>
66
+ <input
67
+ type="text"
68
+ name="body"
69
+ style={{ height: 0, width: 0, opacity: 0 }}
70
+ />
61
71
  {children}
62
72
  </form>
63
73
  );