Skip to content

Commit cb3f6e2

Browse files
authored
Merge pull request #105 from t49tran/refactor-log-warning-message-only-in-development
Refactor log warning message only in development
2 parents 2dbece4 + f1a5b0f commit cb3f6e2

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

package-lock.json

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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;
@@ -135,3 +136,20 @@ export const injectGoogleReCaptchaScript = ({
135136

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

0 commit comments

Comments
 (0)