react-native-klarna-inapp-sdk
Version:
This library wraps Klarna Mobile SDK and exposes its functionality as React Native components.
98 lines (97 loc) • 4.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.KlarnaCheckoutView = void 0;
var _react = _interopRequireWildcard(require("react"));
var _KlarnaCheckoutViewNativeComponent = _interopRequireWildcard(require("./specs/KlarnaCheckoutViewNativeComponent"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
class KlarnaCheckoutView extends _react.Component {
snippet = null;
isCheckoutViewReady = false;
constructor(props) {
super(props);
this.state = {
nativeViewHeight: 0
};
this.checkoutViewRef = /*#__PURE__*/_react.default.createRef();
this.isCheckoutViewReady = false;
}
render() {
return /*#__PURE__*/_react.default.createElement(_KlarnaCheckoutViewNativeComponent.default, {
ref: this.checkoutViewRef
/* eslint-disable-next-line react-native/no-inline-styles */,
style: {
width: '100%',
height: this.state.nativeViewHeight,
flexShrink: 1
},
returnUrl: this.props.returnUrl || '',
onEvent: event => {
var _this$props$onEvent, _this$props;
let params = {};
try {
params = JSON.parse(event.nativeEvent.productEvent.params);
} catch (e) {
console.error('Failed to parse productEvent.params', e);
}
const productEvent = {
action: event.nativeEvent.productEvent.action,
params: params
};
(_this$props$onEvent = (_this$props = this.props).onEvent) === null || _this$props$onEvent === void 0 || _this$props$onEvent.call(_this$props, productEvent);
},
onError: event => {
var _this$props$onError, _this$props2;
const mobileSdkError = {
isFatal: event.nativeEvent.error.isFatal,
message: event.nativeEvent.error.message,
name: event.nativeEvent.error.name
};
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 || _this$props$onError.call(_this$props2, mobileSdkError);
},
onResized: event => {
const newHeight = Number(event.nativeEvent.height);
if (newHeight !== this.state.nativeViewHeight) {
console.log('onResized', newHeight);
this.setState({
nativeViewHeight: newHeight
});
}
},
onCheckoutViewReady: () => {
this.isCheckoutViewReady = true;
console.log('onCheckoutViewReady');
if (this.snippet) {
console.log('Setting the snippet...');
this.setSnippet(this.snippet);
this.snippet = null;
}
}
});
}
setSnippet = snippet => {
this.snippet = snippet;
const view = this.checkoutViewRef.current;
if (view != null && this.isCheckoutViewReady) {
_KlarnaCheckoutViewNativeComponent.Commands.setSnippet(view, snippet);
}
};
suspend = () => {
const view = this.checkoutViewRef.current;
if (view != null && this.isCheckoutViewReady) {
_KlarnaCheckoutViewNativeComponent.Commands.suspend(view);
}
};
resume = () => {
const view = this.checkoutViewRef.current;
if (view != null && this.isCheckoutViewReady) {
_KlarnaCheckoutViewNativeComponent.Commands.resume(view);
}
};
}
exports.KlarnaCheckoutView = KlarnaCheckoutView;
var _default = KlarnaCheckoutView;
exports.default = _default;
//# sourceMappingURL=KlarnaCheckoutView.js.map