homeflowjs 0.13.52 → 0.13.54

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/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.52",
3
+ "version": "0.13.54",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",
package/utils/index.js CHANGED
@@ -52,7 +52,7 @@ export const findSavedSearch = (savedSearches, search) => {
52
52
  if (savedSearch.tags && search.tags && savedSearch.tags.length !== search.tags.length) return false;
53
53
  if (savedSearch.tags && search.tags
54
54
  && !savedSearch.tags.every(
55
- (value, index) => value?.toLowerCase() === search.tags[index]?.toLowerCase()?.replaceAll('-', ' '),
55
+ (value, index) => value?.toLowerCase()?.replaceAll('-', ' ') === search.tags[index]?.toLowerCase()?.replaceAll('-', ' '),
56
56
  )) return false;
57
57
 
58
58
  return true;