1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __importStar = (this && this.__importStar) || function (mod) {
|
27 | if (mod && mod.__esModule) return mod;
|
28 | var result = {};
|
29 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
30 | result["default"] = mod;
|
31 | return result;
|
32 | };
|
33 | Object.defineProperty(exports, "__esModule", { value: true });
|
34 | var React = __importStar(require("react"));
|
35 | var bearer_provider_1 = require("./bearer-provider");
|
36 | var Connect = (function (_super) {
|
37 | __extends(Connect, _super);
|
38 | function Connect(props) {
|
39 | var _this = _super.call(this, props) || this;
|
40 | _this.state = {};
|
41 | _this.connect = _this.connect.bind(_this);
|
42 | _this.setError = _this.setError.bind(_this);
|
43 | return _this;
|
44 | }
|
45 | Connect.prototype.setError = function (error) {
|
46 | this.setState(function (state) { return (__assign({}, state, { error: error })); });
|
47 | };
|
48 | Connect.prototype.connect = function (bearer) {
|
49 | var _this = this;
|
50 | return function () {
|
51 | _this.setError(null);
|
52 | bearer
|
53 | .connect(_this.props.integration, _this.props.setupId, { authId: _this.props.authId })
|
54 | .then(function (data) {
|
55 | if (_this.props.integration === data.integration) {
|
56 | _this.props.onSuccess(data);
|
57 | }
|
58 | })
|
59 | .catch(function (error) {
|
60 | if (_this.props.onError) {
|
61 | _this.props.onError({ error: error, authId: _this.props.authId, integration: _this.props.integration });
|
62 | }
|
63 | _this.setError(error);
|
64 | });
|
65 | };
|
66 | };
|
67 | Connect.prototype.render = function () {
|
68 | var _this = this;
|
69 | return (React.createElement(bearer_provider_1.BearerContext.Consumer, null, function (_a) {
|
70 | var bearer = _a.bearer;
|
71 | return _this.props.render({ loading: false, connect: _this.connect(bearer), error: _this.state.error });
|
72 | }));
|
73 | };
|
74 | Connect.contextType = bearer_provider_1.BearerContext;
|
75 | return Connect;
|
76 | }(React.Component));
|
77 | exports.default = Connect;
|