UNPKG

@gooddata/react-components

Version:
123 lines 6.04 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; exports.__esModule = true; var React = require("react"); var GoodData = require("gooddata"); var isEqual = require("lodash/isEqual"); var omit = require("lodash/omit"); var data_layer_1 = require("@gooddata/data-layer"); var errorStates_1 = require("../../constants/errorStates"); var promise_1 = require("../../helpers/promise"); function simpleDataAdapterProvider(InnerComponent, type) { return _a = /** @class */ (function (_super) { __extends(WrappedComponent, _super); function WrappedComponent(props) { var _this = _super.call(this, props) || this; _this.state = { type: type, adapter: null, dataSource: null, metadataSource: null }; return _this; } WrappedComponent.prototype.componentDidMount = function () { var _this = this; var _a = this.props, projectId = _a.projectId, afm = _a.afm, transformation = _a.transformation; this.prepareDataSource(this.prepareAdapter(projectId), afm) .then(function (dataSource) { _this.prepareMDSource(dataSource, _this.state.type, afm, transformation); }); }; WrappedComponent.prototype.componentWillReceiveProps = function (nextProps) { var _this = this; var projectId = nextProps.projectId, afm = nextProps.afm, transformation = nextProps.transformation; if (projectId !== this.props.projectId || !isEqual(afm, this.props.afm)) { this.prepareDataSource(this.prepareAdapter(projectId), afm) .then(function (dataSource) { _this.prepareMDSource(dataSource, _this.state.type, afm, transformation); }); return; } if (!isEqual(transformation, this.props.transformation)) { this.prepareMDSource(this.state.dataSource, this.state.type, afm, transformation); } }; WrappedComponent.prototype.componentWillUnmount = function () { if (this.prepareDataSourceCancellable) { this.prepareDataSourceCancellable.cancel(); } if (this.prepareMetadataSourceCancellable) { this.prepareMetadataSourceCancellable.cancel(); } }; WrappedComponent.prototype.render = function () { if (!this.state.dataSource || !this.state.metadataSource) { return null; } var _a = this.state, dataSource = _a.dataSource, metadataSource = _a.metadataSource; return (React.createElement(InnerComponent, __assign({}, omit(this.props, ['afm', 'projectId']), { dataSource: dataSource, metadataSource: metadataSource }))); }; WrappedComponent.prototype.prepareAdapter = function (projectId) { var adapter = new data_layer_1.SimpleExecutorAdapter(GoodData, projectId); this.setState({ adapter: adapter }); return adapter; }; WrappedComponent.prototype.handleError = function (error) { if (error !== errorStates_1.ErrorStates.PROMISE_CANCELLED) { throw error; } }; WrappedComponent.prototype.prepareDataSource = function (adapter, afm) { if (this.prepareDataSourceCancellable) { this.prepareDataSourceCancellable.cancel(); } this.prepareDataSourceCancellable = promise_1.getCancellable(adapter.createDataSource(afm)); return this.prepareDataSourceCancellable.promise["catch"](this.handleError); }; WrappedComponent.prototype.prepareMDSource = function (dataSource, type, afm, transformation) { var _this = this; if (this.prepareMetadataSourceCancellable) { this.prepareMetadataSourceCancellable.cancel(); } this.prepareMetadataSourceCancellable = promise_1.getCancellable(dataSource.getData(transformation)); this.prepareMetadataSourceCancellable.promise .then(function (result) { var md = data_layer_1.Converters.toVisObj(type, afm, transformation, result.headers); var metadataSource = new data_layer_1.SimpleMetadataSource(md, {}); _this.setState({ dataSource: dataSource, metadataSource: metadataSource }); }, this.handleError); return this.prepareMetadataSourceCancellable.promise; }; return WrappedComponent; }(React.Component)), _a.defaultProps = { transformation: {} }, _a; var _a; } exports.simpleDataAdapterProvider = simpleDataAdapterProvider; //# sourceMappingURL=SimpleDataAdapterProvider.js.map