Skip to content

Commit 237659c

Browse files
committed
refactor: only log warning message on development mode
1 parent d284481 commit 237659c

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/google-recaptcha-provider.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
createContext,
88
ReactNode
99
} from 'react';
10-
import { cleanGoogleRecaptcha, injectGoogleReCaptchaScript } from './utils';
10+
import {
11+
cleanGoogleRecaptcha,
12+
injectGoogleReCaptchaScript,
13+
logWarningMessage
14+
} from './utils';
1115

1216
enum GoogleRecaptchaError {
1317
SCRIPT_NOT_AVAILABLE = 'Recaptcha script is not available'
@@ -57,7 +61,9 @@ export function GoogleReCaptchaProvider({
5761

5862
useEffect(() => {
5963
if (!reCaptchaKey) {
60-
console.warn('<GoogleReCaptchaProvider /> recaptcha key not provided');
64+
logWarningMessage(
65+
'<GoogleReCaptchaProvider /> recaptcha key not provided'
66+
);
6167

6268
return;
6369
}
@@ -66,7 +72,7 @@ export function GoogleReCaptchaProvider({
6672

6773
const onLoad = () => {
6874
if (!window || !(window as any).grecaptcha) {
69-
console.warn(
75+
logWarningMessage(
7076
`<GoogleRecaptchaProvider /> ${GoogleRecaptchaError.SCRIPT_NOT_AVAILABLE}`
7177
);
7278

@@ -82,13 +88,18 @@ export function GoogleReCaptchaProvider({
8288
});
8389
};
8490

91+
const onError = () => {
92+
logWarningMessage('Error loading google recaptcha script');
93+
};
94+
8595
injectGoogleReCaptchaScript({
8696
reCaptchaKey,
8797
useEnterprise,
8898
useRecaptchaNet,
8999
scriptProps,
90100
language,
91-
onLoad
101+
onLoad,
102+
onError
92103
});
93104

94105
return () => {

src/google-recaptcha.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect } from 'react';
22
import { useGoogleReCaptcha } from './use-google-recaptcha';
3+
import { logWarningMessage } from './utils';
34

45
export interface IGoogleRecaptchaProps {
56
onVerify: (token: string) => void | Promise<void>;
@@ -11,16 +12,16 @@ export function GoogleReCaptcha({ action, onVerify }: IGoogleRecaptchaProps) {
1112

1213
useEffect(() => {
1314
const { executeRecaptcha } = googleRecaptchaContextValue;
14-
const handleExecuteRecaptcha = async () => {
15-
if (!executeRecaptcha) {
16-
console.warn('Execute recaptcha function not defined');
17-
return;
18-
}
1915

16+
if (!executeRecaptcha) {
17+
return;
18+
}
19+
20+
const handleExecuteRecaptcha = async () => {
2021
const token = await executeRecaptcha(action);
2122

2223
if (!onVerify) {
23-
console.warn('Please define an onVerify function');
24+
logWarningMessage('Please define an onVerify function');
2425

2526
return;
2627
}

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ interface InjectGoogleReCaptchaScriptParams {
33
useRecaptchaNet: boolean;
44
useEnterprise: boolean;
55
onLoad: () => void;
6+
onError: () => void;
67
language?: string;
78
scriptProps?: {
89
nonce?: string;
@@ -136,3 +137,20 @@ export const injectGoogleReCaptchaScript = ({
136137

137138
elementToInjectScript.appendChild(js);
138139
};
140+
141+
/**
142+
* Function to log warning message if it's not in production mode
143+
*
144+
* @param message String
145+
* @returns
146+
*/
147+
export const logWarningMessage = (message: string) => {
148+
const isDevelopmentMode =
149+
!!process.env && process.env.NODE_ENV !== 'production';
150+
151+
if (isDevelopmentMode) {
152+
return;
153+
}
154+
155+
console.warn(message);
156+
};

0 commit comments

Comments
 (0)