pointzi-react
Version:
This is an IOS/Android/Web wrapper file for Pointzi that allows the use of Pointzi in React Native Enviroment
135 lines (119 loc) • 3.39 kB
JavaScript
import { NativeModules, Platform } from "react-native";
var pz;
if(Platform.OS === 'web'){
pz = {};
}else{
pz = Object.create(NativeModules.PointziReact);
}
pz.tagCuid = function (tag) {
if(Platform.OS === 'web'){
window.Pointzi.tag.string('sh_cuid', tag);
}else{
NativeModules.PointziReact.tagCuid(String(tag));
}
};
pz.tagString = function (key, tag) {
if(Platform.OS === 'web'){
window.Pointzi.tag.string(String(key), String(tag));
}else{
NativeModules.PointziReact.tagString(String(key), String(tag));
}
};
pz.tagNumeric = function (key, tag) {
if(Platform.OS === 'web'){
window.Pointzi.tag.numeric(key, tag);
}else{
NativeModules.PointziReact.tagNumeric(key, tag);
}
};
pz.tagDatetime = function (key, date) {
if(Platform.OS === 'web'){
window.Pointzi.tag.datetime(String(key), String(date));
}else{
NativeModules.PointziReact.tagDatetime(String(key), String(date));
}
};
pz.incrementTag = function (key, value) {
if(Platform.OS === 'web'){
window.Pointzi.tag.increment(String(key));
}else if(!value){
NativeModules.PointziReact.incrementTag(key);
}else{
NativeModules.PointziReact.incrementTag(key, value);
}
};
pz.removeTag = function (key) {
if(Platform.OS === 'web'){
console.warn('Web SDK does not support the remove tag feature.')
}else{
NativeModules.PointziReact.removeTag(String(key));
}
};
pz.tagUserLanguage = function (language) {
if(Platform.OS === 'web'){
window.Pointzi.tag.string('sh_language', String(language));
}else{
NativeModules.PointziReact.tagUserLanguage(String(language));
}
};
pz.showGuide = function (id) {
if(Platform.OS === 'web'){
window.Pointzi.showOnDemandCampaign(String(id));
}else{
NativeModules.PointziReact.showGuide(String(id));
}
};
pz.pointziReactWillDidMount = function (reactViewName) {
if(Platform.OS === 'web'){
console.warn('Web SDK does not support the ' +
'pointziReactWillDidMount method.')
}else{
NativeModules.PointziReact.pointziReactWillDidMount(String(reactViewName));
}
};
pz.viewWillRender = function (reactViewName) {
if(Platform.OS === 'web'){
console.warn('Web SDK does not support the viewWillRender method.')
}else{
NativeModules.PointziReact.viewWillRender(String(reactViewName));
}
};
// >>> Web only
pz.register = function(appKey, cuid){
if(Platform.OS === 'web'){
return window.Pointzi.register(appKey,cuid);
}else{
return Promise.resolve();
}
}
// Sometime SDK file will be loaded after customer, so use this to make sure
// SDK ready before call any features.
pz.ready = function(cb) {
if(Platform.OS === 'web'){
var timeout = 0;
var interval = setInterval(function() {
if (window.Pointzi.register || timeout == 1200) {
clearInterval(interval);
if (timeout == 1200) {
console.error('Time out! Pointzi sdk not loaded.');
} else {
if (typeof cb === 'function') {
cb();
}
}
return;
}
timeout++;
}, 100);
}else{
cb();
}
}
// Getter for Web SDK object
Object.defineProperty(pz, 'web', {
get: function () {
return window.Pointzi;
}
})
// <<<
export default pz;