-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (53 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, {useEffect, useState} from 'react';
export default props => {
const [showOverlay, setShowOverlay] = useState(false);
useEffect(()=>{
const script = document.createElement('script');
script.src = 'https://accounts.google.com/gsi/client';
script.async = true;
script.defer = true;
document.querySelector('body').appendChild(script);
window.onGoogleLibraryLoad = () => {
oneTapInit();
}
}, []);
const handleMoment = (momentType, action, reason) => {
if(props.overlay && action === 'displayed'){
setShowOverlay(true);
}else if(props.overlay && action !== 'notdisplayed'){
setShowOverlay(false);
}
props.onPrompt && props.onPrompt(momentType, action, reason);
}
const checkIfGoogleOneTapToBeShown = () => {
if(props.doNotDisplay && props.doNotDisplay()){
return false;
}
return true;
}
const oneTapCallback = (response) => {
props.onSuccess && props.onSuccess(response);
}
const oneTapInit = () => {
if(!google){
props.onError && props.onError({msg: 'Some error occured while loading the Google Script.'});
}
if(!checkIfGoogleOneTapToBeShown()){
return;
}
google.accounts.id.initialize({client_id: props.clientId, callback: oneTapCallback});
google.accounts.id.prompt(notification => {
const momentType = notification.getMomentType();
if(notification.isDisplayed()) {
setTimeout(()=>{ handleMoment(momentType, 'displayed', 'displayed'); }, 500);
} else if(notification.isNotDisplayed()){
handleMoment(momentType, 'notdisplayed', notification.getNotDisplayedReason());
} else if(notification.isSkippedMoment()) {
handleMoment(momentType, 'skipped', notification.getSkippedReason());
} else if(notification.isDismissedMoment()) {
handleMoment(momentType, 'dismissed', notification.getDismissedReason());
}
});
}
return props.overlay && showOverlay ? props.overlay : null;
}