UNPKG

1.74 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const React = __importStar(require("react"));
11const bearer_provider_1 = require("./bearer-provider");
12class Connect extends React.Component {
13 constructor(props) {
14 super(props);
15 this.state = {};
16 this.connect = this.connect.bind(this);
17 this.setError = this.setError.bind(this);
18 }
19 setError(error) {
20 this.setState(state => (Object.assign({}, state, { error })));
21 }
22 connect(bearer) {
23 return () => {
24 this.setError(null);
25 bearer
26 .connect(this.props.integration, this.props.setupId, { authId: this.props.authId })
27 .then((data) => {
28 if (this.props.integration === data.integration) {
29 this.props.onSuccess(data);
30 }
31 })
32 .catch((error) => {
33 if (this.props.onError) {
34 this.props.onError({ error, authId: this.props.authId, integration: this.props.integration });
35 }
36 this.setError(error);
37 });
38 };
39 }
40 render() {
41 return (React.createElement(bearer_provider_1.BearerContext.Consumer, null, ({ bearer }) => this.props.render({ loading: false, connect: this.connect(bearer), error: this.state.error })));
42 }
43}
44Connect.contextType = bearer_provider_1.BearerContext;
45exports.default = Connect;