@kartikbhalla/react-native-truecaller
Version:
react native sdk integration of truecaller for both android and ios
147 lines (145 loc) • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "TRUECALLER_ANDROID_CUSTOMIZATIONS", {
enumerable: true,
get: function () {
return _constants.TRUECALLER_ANDROID_CUSTOMIZATIONS;
}
});
Object.defineProperty(exports, "TRUECALLER_ANDROID_EVENTS", {
enumerable: true,
get: function () {
return _constants.TRUECALLER_ANDROID_EVENTS;
}
});
exports.useTruecaller = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _axios = _interopRequireDefault(require("axios"));
var _constants = require("./constants.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const TruecallerAndroid = _reactNative.NativeModules.TruecallerAndroidModule;
const TruecallerIOS = _reactNative.NativeModules.TruecallerIOS;
const initialize = ({
androidButtonColor,
androidButtonTextColor,
androidButtonStyle,
androidButtonText,
androidFooterButtonText,
androidConsentHeading,
iosAppKey,
iosAppLink
}) => {
if (_reactNative.Platform.OS === 'android') TruecallerAndroid.initialize(androidButtonColor || _constants.DEFAULT_BUTTON_COLOR, androidButtonTextColor || _constants.DEFAULT_BUTTON_TEXT_COLOR, androidButtonText || _constants.TRUECALLER_ANDROID_CUSTOMIZATIONS.BUTTON_TEXTS.ACCEPT, androidButtonStyle || _constants.TRUECALLER_ANDROID_CUSTOMIZATIONS.BUTTON_STYLES.ROUND, androidFooterButtonText || _constants.TRUECALLER_ANDROID_CUSTOMIZATIONS.FOOTER_BUTTON_TEXTS.ANOTHER_METHOD, androidConsentHeading || _constants.TRUECALLER_ANDROID_CUSTOMIZATIONS.CONSENT_HEADING_TEXTS.CHECKOUT_WITH);else if (_reactNative.Platform.OS === 'ios' && iosAppKey && iosAppLink) TruecallerIOS.initialize(iosAppKey, iosAppLink);else {
//TODO error handling
}
};
const openTruecallerModal = () => {
if (_reactNative.Platform.OS === 'android') TruecallerAndroid.invoke();else if (_reactNative.Platform.OS === 'ios') TruecallerIOS.requestProfile();
};
const isTruecallerSupported = () => {
if (_reactNative.Platform.OS === 'android') return TruecallerAndroid.isUsable();else if (_reactNative.Platform.OS === 'ios') return TruecallerIOS.isSupported();
return false;
};
const useTruecaller = ({
androidClientId,
iosAppKey,
iosAppLink,
androidButtonColor,
androidButtonStyle,
androidButtonText,
androidButtonTextColor,
androidConsentHeading,
androidFooterButtonText
}) => {
const [user, setUser] = (0, _react.useState)(null);
const [errorCode, setErrorCode] = (0, _react.useState)(null);
const [error, setError] = (0, _react.useState)(null);
(0, _react.useEffect)(() => {
if (_reactNative.Platform.OS !== 'android' || !androidClientId) return;
_reactNative.DeviceEventEmitter.addListener(_constants.TRUECALLER_ANDROID_EVENTS.TRUECALLER_SUCCESS, ({
authorizationCode,
codeVerifier
}) => {
_axios.default.post('https://oauth-account-noneu.truecaller.com/v1/token',
//TODO Constants
{
grant_type: 'authorization_code',
client_id: androidClientId,
code: authorizationCode,
code_verifier: codeVerifier
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(response => {
const accessToken = response.data.access_token;
_axios.default.get('https://oauth-account-noneu.truecaller.com/v1/userinfo', {
//TODO Constants
headers: {
Authorization: `Bearer ${accessToken}`
}
}).then(resp => setUser({
firstName: resp.data.given_name,
lastName: resp.data.family_name || null,
mobileNumber: `+${resp.data.phone_number}`,
countryCode: resp.data.phone_number_country_code,
gender: resp.data.gender || null,
email: resp.data.email || null,
profileUrl: resp.data.profile || null
}));
//TODO create fixed user interface.
}).catch(() => {
//TODO error handling
});
});
_reactNative.DeviceEventEmitter.addListener(_constants.TRUECALLER_ANDROID_EVENTS.TRUECALLER_FAILURE, ({
errorMessage: errorMessageAndroid,
errorCode: errorCodeAndroid
}) => {
setError(errorMessageAndroid);
setErrorCode(errorCodeAndroid);
});
}, [androidClientId]);
(0, _react.useEffect)(() => {
if (_reactNative.Platform.OS !== 'ios' || !iosAppKey || !iosAppLink) return;
const eventEmitter = new _reactNative.NativeEventEmitter(TruecallerIOS);
eventEmitter.addListener(_constants.TRUECALLER_IOS_EVENTS.TRUECALLER_FAILURE, ({
errorMessage: errorMessageIOS,
errorCode: errorCodeIOS
}) => {
setError(errorMessageIOS);
setErrorCode(errorCodeIOS);
});
eventEmitter.addListener(_constants.TRUECALLER_IOS_EVENTS.TRUECALLER_SUCCESS, profile => setUser({
firstName: profile.firstName,
lastName: profile.lastName,
email: profile.email || null,
countryCode: profile.countryCode,
gender: _constants.IOSGender?.[profile.gender] || null,
mobileNumber: profile.phoneNumber,
profileUrl: profile.profileUrl || null
}));
}, [iosAppKey, iosAppLink]);
return {
initializeTruecaller: () => initialize({
iosAppKey,
iosAppLink,
androidButtonColor,
androidButtonStyle,
androidButtonText,
androidButtonTextColor,
androidConsentHeading,
androidFooterButtonText
}),
openTruecallerModal,
isTruecallerSupported,
error,
errorCode,
user
};
};
exports.useTruecaller = useTruecaller;
//# sourceMappingURL=index.js.map