UNPKG

@gooddata/react-components

Version:

GoodData.UI - A powerful JavaScript library for building analytical applications

144 lines • 7.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { 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 extendStatics(d, b); }; 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) || function () { __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; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); // (C) 2007-2020 GoodData Corporation var React = require("react"); var gooddata_js_1 = require("@gooddata/gooddata-js"); var PropTypes = require("prop-types"); var isEqual = require("lodash/isEqual"); var omit = require("lodash/omit"); var execution_1 = require("../visualizations/proptypes/execution"); var utils_1 = require("../../helpers/utils"); var utils_2 = require("../visualizations/table/totals/utils"); function defaultAdapterFactory(sdk, projectId) { return new gooddata_js_1.DataLayer.ExecuteAfmAdapter(sdk, projectId); } function addDefaultDimensions(afm, resultSpec, generateDefaultDimensions) { return resultSpec && resultSpec.dimensions ? resultSpec : __assign({ dimensions: generateDefaultDimensions(afm) }, resultSpec); } /** * dataSourceProvider * is a function that creates a dataSource and passes it to InnerComponent * @param InnerComponent: React.ComponentClass<T & IDataSourceProviderInjectedProps> * a component that will be pased dataSource prop * @param generateDefaultDimensions - a function that returns default dimensions * @param componentName: string - InnerComponent actual name * @internal */ function dataSourceProvider(InnerComponent, generateDefaultDimensions, componentName, exportTitle) { var _a; return _a = /** @class */ (function (_super) { __extends(WrappedComponent, _super); function WrappedComponent(props) { var _this = _super.call(this, props) || this; _this.state = { dataSource: null, resultSpec: null, }; var sdk = props.sdk || gooddata_js_1.factory(); _this.sdk = sdk.clone(); utils_1.setTelemetryHeaders(_this.sdk, componentName, props); _this.subject = gooddata_js_1.DataLayer.createSubject(function (dataSource) { return _this.setState({ dataSource: dataSource }); }, function (error) { return _this.handleError(error); }); _this.updateTotals = _this.updateTotals.bind(_this); return _this; } WrappedComponent.prototype.componentDidMount = function () { var _a = this.props, projectId = _a.projectId, afm = _a.afm; this.createAdapter(projectId); this.prepareDataSource(afm); }; WrappedComponent.prototype.componentWillReceiveProps = function (nextProps) { var _this = this; var projectId = nextProps.projectId, afm = nextProps.afm, resultSpec = nextProps.resultSpec, sdk = nextProps.sdk; if (projectId !== this.props.projectId) { this.createAdapter(projectId); } if (sdk && sdk !== this.sdk) { this.sdk = nextProps.sdk.clone(); utils_1.setTelemetryHeaders(this.sdk, componentName, nextProps); } if (!isEqual(afm, this.props.afm) || !isEqual(resultSpec, this.props.resultSpec) || projectId !== this.props.projectId) { this.setState({ dataSource: null, }, function () { return _this.prepareDataSource(afm); }); } }; WrappedComponent.prototype.componentWillUnmount = function () { this.subject.unsubscribe(); }; // updateTotals must be called everytime totals are updated // to check if we need to reload datasource to get new native totals // @param nextTotals lists all requested totals WrappedComponent.prototype.updateTotals = function (nextTotals) { var nativeTotals = utils_2.getNativeTotals(nextTotals, this.props.resultSpec); var afm = this.state.dataSource.getAfm(); var nativeTotalsRequested = nativeTotals.length > 0; var hasAfmNativeTotals = !!afm.nativeTotals; var hasUnlistedNativeTotals = nativeTotals.some(function (total) { return !afm.nativeTotals || !afm.nativeTotals.find(function (afmNativeTotal) { return isEqual(afmNativeTotal, total); }); }); if (nativeTotalsRequested && (!hasAfmNativeTotals || hasUnlistedNativeTotals)) { this.prepareDataSource(__assign({}, afm, { nativeTotals: nativeTotals })); } }; WrappedComponent.prototype.render = function () { var dataSource = this.state.dataSource; if (!dataSource) { return null; } // keep projectId in props for exporter var props = omit(this.props, ["afm", "resultSpec", "adapterFactory"]); var resultSpec = addDefaultDimensions(this.props.afm, this.props.resultSpec, generateDefaultDimensions); return (React.createElement(InnerComponent, __assign({}, props, { exportTitle: exportTitle || componentName, dataSource: dataSource, updateTotals: this.updateTotals, resultSpec: resultSpec }))); }; WrappedComponent.prototype.createAdapter = function (projectId) { var adapterFactory = this.props.adapterFactory || defaultAdapterFactory; this.adapter = adapterFactory(this.sdk, projectId); }; WrappedComponent.prototype.handleError = function (error) { throw error; }; WrappedComponent.prototype.prepareDataSource = function (afm) { var promise = this.adapter.createDataSource(afm); this.subject.next(promise); }; return WrappedComponent; }(React.Component)), _a.displayName = componentName ? componentName + "WithDataSource" : "WrappedComponent", _a.propTypes = { projectId: PropTypes.string, afm: execution_1.AfmPropTypesShape.isRequired, resultSpec: execution_1.ResultSpecPropTypesShape, }, _a; } exports.dataSourceProvider = dataSourceProvider; //# sourceMappingURL=DataSourceProvider.js.map