mychips-react-sdk
Version:
MyChips Offerwall
198 lines (191 loc) • 7.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MCOfferwallView = MCOfferwallView;
var React = _interopRequireWildcard(require("react"));
var _reactNativeWebview = require("react-native-webview");
var _reactNative = require("react-native");
var _UriBuilderService = _interopRequireDefault(require("../services/UriBuilderService"));
var _UserService = require("../services/UserService");
var _RatelimitService = require("../services/RatelimitService");
var _netinfo = _interopRequireDefault(require("@react-native-community/netinfo"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function MCOfferwallView({
adunitId
}) {
//about:blank url will break compilation on ios
const [currentUrl, setCurrentUrl] = React.useState(''); // State for the current URL
const [isConnected, setIsConnected] = React.useState(true);
const [isLoading, setIsLoading] = React.useState(true);
const mychipsDomain = 'mychips.io';
const webViewRef = React.useRef(null);
const currentUrlRef = React.useRef(currentUrl); // Ref to keep track of the current URL
const getUserData = async () => {
const userService = new _UserService.UserService();
const userId = await userService.getOrCreateId();
const advertisingId = await userService.getAdvertisingId();
const age = await userService.getAge();
const gender = await userService.getGender();
const totalVirtualCurrency = await userService.getCurrentTotalCurrency();
return {
userId,
advertisingId,
age,
gender,
totalVirtualCurrency
};
};
const buildUrl = async () => {
const userData = await getUserData();
const uriBuilderService = new _UriBuilderService.default();
return uriBuilderService.buildOfferwallUrl(adunitId, userData.userId, userData.advertisingId, userData.gender, userData.age, userData.totalVirtualCurrency);
};
const fetchUserDataAndBuildUrl = async () => {
try {
const url = await buildUrl();
setCurrentUrl(url);
} catch (error) {
console.error('Failed to build offerwall URL', error);
}
};
///get the current navigation url and keep it updated
const onNavigationStateChange = navState => {
const {
url
} = navState;
if (url !== currentUrl) {
setCurrentUrl(url); // Update the state with the new URL
currentUrlRef.current = url;
console.log("onNavigationStateChange:" + url);
}
};
///handle logic for specific link
const onShouldStartLoadWithRequest = request => {
const {
url
} = request;
console.log("onShouldStartLoadWithRequest:" + url);
if (url.includes('blank') || url.includes('file:')) {
return false;
}
if (url.includes('/redirect')) {
_reactNative.Linking.openURL(url).catch(err => console.error("Couldn't load page", err));
return false; // Prevent the WebView from loading the URL
}
if (url.includes('mychips://')) {
return false;
}
return true; // Allow the WebView to load the URL
};
const onLoadStart = () => {
//console.log("WebView Load Started");
};
const onLoad = () => {
//console.log("WebView Loaded");
};
const onLoadEnd = () => {
if (currentUrl == '' || currentUrl == 'about:blank') {
fetchUserDataAndBuildUrl();
console.log("onLoadEnd: fetchUserDataAndBuildUrl");
}
console.log("WebView Load Ended");
if (isLoading && currentUrl.includes(mychipsDomain)) {
setIsLoading(false);
}
};
const onError = error => {
console.error("WebView Error: ", error);
};
//handle navigation logic
const onBackPress = () => {
const url = currentUrlRef.current; // Use
if (url.includes('page=home') || !url.includes('page=')) {
return false; // Allow default back button behavior if URL contains 'page=home'
} else {
var _webViewRef$current;
(_webViewRef$current = webViewRef.current) === null || _webViewRef$current === void 0 || _webViewRef$current.goBack();
return true; // Prevent default back button behavior
}
};
React.useEffect(() => {
_RatelimitService.RateLimitService.enableRequest();
setTimeout(() => {
if (currentUrlRef.current == 'about:blank' || currentUrlRef.current == '') {
fetchUserDataAndBuildUrl();
console.log("01");
}
}, 1000); // Delay to ensure WebView is ready
setTimeout(() => {
if (isLoading) setIsLoading(false);
}, 5000);
const unsubscribeNetInfo = _netinfo.default.addEventListener(state => {
setIsConnected(state.isConnected);
});
_reactNative.BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () => {
_reactNative.BackHandler.removeEventListener('hardwareBackPress', onBackPress);
unsubscribeNetInfo();
};
}, []);
return /*#__PURE__*/React.createElement(_reactNative.View, {
style: {
flex: 1
}
}, /*#__PURE__*/React.createElement(_reactNativeWebview.WebView
// key={currentUrl} // Use URL as key to force re-render
, {
ref: webViewRef,
source: {
uri: currentUrl
},
onNavigationStateChange: onNavigationStateChange,
onShouldStartLoadWithRequest: onShouldStartLoadWithRequest,
onLoadStart: onLoadStart,
onLoad: onLoad,
onLoadEnd: onLoadEnd,
onError: onError
}), isLoading && /*#__PURE__*/React.createElement(_reactNative.View, {
style: styles.loaderContainer
}, /*#__PURE__*/React.createElement(_reactNative.ActivityIndicator, {
size: "large",
style: styles.loader
})), !isConnected && /*#__PURE__*/React.createElement(_reactNative.View, {
style: styles.offlineContainer
}, /*#__PURE__*/React.createElement(_reactNative.Text, {
style: styles.offlineText
}, "No Internet Connection")));
}
const styles = _reactNative.StyleSheet.create({
offlineContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#b52424',
zIndex: 1 // Ensure the offline message is on top
},
offlineText: {
color: '#fff'
},
loaderContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute',
width: '100%',
height: '100%',
zIndex: 1,
// Ensures loader is above other content
backgroundColor: 'rgba(255, 255, 255, 0.8)' // Optional: Adds a semi-transparent background
},
loader: {
// Additional styles if needed
}
});
//# sourceMappingURL=MCOfferwallView.js.map