UNPKG

@gooddata/react-components

Version:
95 lines 4 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 __()); }; })(); exports.__esModule = true; var React = require("react"); var GoodData = require("gooddata"); var get = require("lodash/get"); var isEqual = require("lodash/isEqual"); var data_layer_1 = require("@gooddata/data-layer"); var errorStates_1 = require("../constants/errorStates"); function dataTableFactory(projectId) { return new data_layer_1.DataTable(new data_layer_1.SimpleExecutorAdapter(GoodData, projectId)); } var Execute = /** @class */ (function (_super) { __extends(Execute, _super); function Execute(props) { var _this = _super.call(this, props) || this; _this.state = { result: null }; var onError = props.onError, onLoadingChanged = props.onLoadingChanged; _this.dataTable = props.dataTableFactory(props.projectId); _this.dataTable.onData(function (result) { if (result && result.isEmpty) { onError({ status: errorStates_1.ErrorStates.NO_DATA }); } else { _this.setState({ result: result }); } onLoadingChanged({ isLoading: false }); }); _this.dataTable.onError(function (error) { var status = get(error, 'response.status'); if (status === 413) { onLoadingChanged({ isLoading: false }); return onError({ status: errorStates_1.ErrorStates.DATA_TOO_LARGE_TO_COMPUTE, error: error }); } if (status === 400) { onLoadingChanged({ isLoading: false }); return onError({ status: errorStates_1.ErrorStates.BAD_REQUEST, error: error }); } onLoadingChanged({ isLoading: false }); onError({ status: errorStates_1.ErrorStates.UNKNOWN_ERROR, error: error }); }); return _this; } Execute.prototype.componentDidMount = function () { this.runExecution(this.props); }; Execute.prototype.componentWillReceiveProps = function (nextProps) { if (this.hasPropsChanged(nextProps, ['afm', 'transformation'])) { this.runExecution(nextProps); } }; Execute.prototype.shouldComponentUpdate = function (nextProps, nextState) { return !isEqual(this.state.result, nextState.result) || this.hasPropsChanged(nextProps, ['afm', 'transformation', 'children']); }; Execute.prototype.render = function () { var result = this.state.result; if (!result) { return null; } return this.props.children({ result: result }); }; Execute.prototype.isPropChanged = function (nextProps, propName) { if (propName === 'children') { return nextProps.children !== this.props.children; } return !isEqual(nextProps[propName], this.props[propName]); }; Execute.prototype.hasPropsChanged = function (nextProps, propNames) { var _this = this; return propNames.some(function (propName) { return _this.isPropChanged(nextProps, propName); }); }; Execute.prototype.runExecution = function (props) { var afm = props.afm, transformation = props.transformation, onLoadingChanged = props.onLoadingChanged; onLoadingChanged({ isLoading: true }); this.dataTable.getData(afm, transformation); }; Execute.defaultProps = { dataTableFactory: dataTableFactory }; return Execute; }(React.Component)); exports.Execute = Execute; //# sourceMappingURL=Execute.js.map