UNPKG

2.35 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");
12exports.withInvoke = function (integrationId, functionName) {
13 return (WrappedComponent) => { var _a; return _a = class FetcherComponent extends React.Component {
14 constructor(props) {
15 super(props);
16 this.invoke = (params = {}) => {
17 this.setState(Object.assign({}, this.state, { error: null, loading: true }));
18 return this.context.bearer
19 .invoke(integrationId, functionName, params)
20 .then(({ data }) => {
21 this.setState(Object.assign({}, this.state, { data }));
22 if (this.props.onSuccess) {
23 this.props.onSuccess(data);
24 }
25 })
26 .catch(({ error }) => {
27 this.setState(Object.assign({}, this.state, { error }));
28 if (this.props.onFail) {
29 this.props.onFail(error);
30 }
31 })
32 .then(() => {
33 this.setState(Object.assign({}, this.state, { loading: false }));
34 });
35 };
36 this.state = {
37 loading: false
38 };
39 }
40 render() {
41 return (React.createElement(WrappedComponent, Object.assign({}, this.props, { error: this.state.error, data: this.state.data, invoke: this.invoke, loading: this.state.loading })));
42 }
43 },
44 _a.displayName = `withBearerInvoke(${functionName})(${getDisplayName(WrappedComponent)})`,
45 _a.contextType = bearer_provider_1.BearerContext,
46 _a; };
47};
48// TODO: add typing
49function getDisplayName(WrappedComponent) {
50 return WrappedComponent.displayName || WrappedComponent.name || 'Component';
51}