@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
141 lines • 5.88 kB
JavaScript
;
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-2018 GoodData Corporation
var React = require("react");
var gooddata_js_1 = require("@gooddata/gooddata-js");
var isEqual = require("lodash/isEqual");
var noop = require("lodash/noop");
var Execute_1 = require("../proptypes/Execute");
var utils_1 = require("../helpers/utils");
var errorHandlers_1 = require("../helpers/errorHandlers");
var dimensions_1 = require("../helpers/dimensions");
var DataTable = gooddata_js_1.DataLayer.DataTable, ExecuteAfmAdapter = gooddata_js_1.DataLayer.ExecuteAfmAdapter;
function dataTableFactory(sdk, projectId) {
return new DataTable(new ExecuteAfmAdapter(sdk, projectId));
}
/**
* [Execute](http://sdk.gooddata.com/gooddata-ui/docs/execute_component.html)
* is a component that does execution based on afm and resultSpec props and passes the result to it's children function
*/
var Execute = /** @class */ (function (_super) {
__extends(Execute, _super);
function Execute(props) {
var _this = _super.call(this, props) || this;
_this.state = {
result: null,
isLoading: true,
error: null,
};
var sdk = props.sdk || gooddata_js_1.factory();
_this.sdk = sdk.clone();
utils_1.setTelemetryHeaders(_this.sdk, _this.props.telemetryComponentName, props);
_this.initDataTable(props);
return _this;
}
Execute.prototype.componentWillMount = function () {
this.runExecution(this.props);
};
Execute.prototype.componentWillReceiveProps = function (nextProps) {
if (nextProps.sdk && this.sdk !== nextProps.sdk) {
this.sdk = nextProps.sdk.clone();
utils_1.setTelemetryHeaders(this.sdk, this.props.telemetryComponentName, nextProps);
this.initDataTable(nextProps);
}
if (this.hasPropsChanged(nextProps, ["sdk", "projectId", "afm", "resultSpec"])) {
this.initDataTable(nextProps);
this.runExecution(nextProps);
}
};
Execute.prototype.shouldComponentUpdate = function (nextProps, nextState) {
return (!isEqual(this.state, nextState) ||
this.hasPropsChanged(nextProps, ["sdk", "projectId", "afm", "resultSpec", "children"]));
};
Execute.prototype.render = function () {
var _a = this.state, result = _a.result, isLoading = _a.isLoading, error = _a.error;
return this.props.children({ result: result, isLoading: isLoading, error: error });
};
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, resultSpec = props.resultSpec, onLoadingChanged = props.onLoadingChanged;
this.setState({
isLoading: true,
result: null,
error: null,
});
onLoadingChanged({
isLoading: true,
});
var finalResultSpec = __assign({}, (resultSpec || {}));
if (!finalResultSpec.dimensions) {
finalResultSpec.dimensions = dimensions_1.getGeneralDimensionsFromAFM(afm);
}
this.dataTable.getData(afm, finalResultSpec);
};
Execute.prototype.initDataTable = function (props) {
var _this = this;
var onError = props.onError, onLoadingChanged = props.onLoadingChanged, onLoadingFinish = props.onLoadingFinish, projectId = props.projectId;
this.dataTable = props.dataTableFactory(this.sdk, projectId);
this.dataTable.onData(function (result) {
_this.setState({
result: result,
isLoading: false,
});
onLoadingChanged({ isLoading: false });
onLoadingFinish({ result: result });
});
this.dataTable.onError(function (error) {
var newError = errorHandlers_1.convertErrors(error);
onError(newError);
onLoadingChanged({ isLoading: false });
_this.setState({
result: null,
isLoading: false,
error: newError,
});
});
};
Execute.propTypes = Execute_1.ExecutePropType;
Execute.defaultProps = {
dataTableFactory: dataTableFactory,
onError: noop,
onLoadingChanged: noop,
onLoadingFinish: noop,
telemetryComponentName: "Execute",
};
return Execute;
}(React.Component));
exports.Execute = Execute;
//# sourceMappingURL=Execute.js.map