react-native-klarna-inapp-sdk
Version:
This library wraps Klarna Mobile SDK and exposes its functionality as React Native components.
105 lines (104 loc) • 4.34 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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
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;
}
componentWillUnmount() {
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: new height is ${newHeight}`);
this.setState({
nativeViewHeight: newHeight
});
}
},
onCheckoutViewReady: () => {
this.isCheckoutViewReady = true;
console.log('Native checkout view is ready.');
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);
} else {
console.log('setSnippet: checkout view is not ready yet. Will set snippet once ready.');
}
};
suspend = () => {
const view = this.checkoutViewRef.current;
if (view != null && this.isCheckoutViewReady) {
_KlarnaCheckoutViewNativeComponent.Commands.suspend(view);
} else {
console.log('suspend: checkout view is not ready.');
}
};
resume = () => {
const view = this.checkoutViewRef.current;
if (view != null && this.isCheckoutViewReady) {
_KlarnaCheckoutViewNativeComponent.Commands.resume(view);
} else {
console.log('resume: checkout view is not ready.');
}
};
}
exports.KlarnaCheckoutView = KlarnaCheckoutView;
var _default = exports.default = KlarnaCheckoutView;
//# sourceMappingURL=KlarnaCheckoutView.js.map