UNPKG

@gooddata/react-components

Version:
108 lines 4.22 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 PropTypes = React.PropTypes; function getAttributeUri(metadata, projectId, uri, identifier) { return new Promise(function (resolve, reject) { if (uri) { return resolve(uri); } if (!identifier || !projectId) { return reject(new Error('Missing either uri, or identifier and projectId in AttributeFilter props')); } return metadata.getObjectUri(projectId, identifier).then(function (uri) { resolve(uri); }, reject); }); } function getAttributeDisplayForm(metadata, uri) { return metadata.getObjectDetails(uri).then(function (result) { if (!result || !result.attributeDisplayForm) { throw new Error('Invalid data uri. Required data uri must be of type "attributeDisplayForm"'); } return result.attributeDisplayForm; }); } var AttributeLoader = /** @class */ (function (_super) { __extends(AttributeLoader, _super); function AttributeLoader(props) { var _this = _super.call(this, props) || this; _this.state = { attributeDisplayForm: null, isLoading: true, isUsingIdentifier: false, error: null }; return _this; } AttributeLoader.prototype.componentDidMount = function () { this.getAttributeDisplayForm(this.props); }; AttributeLoader.prototype.componentWillReceiveProps = function (nextProps) { if (this.props.uri !== nextProps.uri || this.props.identifier !== nextProps.identifier || this.props.projectId !== nextProps.projectId) { this.setState({ isLoading: true, attributeDisplayForm: null // invalidate }); this.getAttributeDisplayForm(nextProps); } }; AttributeLoader.prototype.render = function () { var _a = this.state, attributeDisplayForm = _a.attributeDisplayForm, isLoading = _a.isLoading, isUsingIdentifier = _a.isUsingIdentifier; return this.props.children({ attributeDisplayForm: attributeDisplayForm, isLoading: isLoading, isUsingIdentifier: isUsingIdentifier }); }; AttributeLoader.prototype.getAttributeDisplayForm = function (props) { var _this = this; var metadata = props.metadata, projectId = props.projectId, uri = props.uri, identifier = props.identifier; getAttributeUri(metadata, projectId, uri, identifier) .then(function (dfUri) { return getAttributeDisplayForm(metadata, dfUri); }) .then(function (attributeDisplayForm) { _this.setState({ attributeDisplayForm: attributeDisplayForm, isLoading: false, isUsingIdentifier: !!identifier, error: null }); }, function (error) { _this.setState({ attributeDisplayForm: null, isLoading: false, error: error }); }); }; AttributeLoader.propTypes = { projectId: PropTypes.string.isRequired, uri: PropTypes.string, identifier: PropTypes.string, metadata: PropTypes.shape({ getObjectDetails: PropTypes.func.isRequired, getObjectUri: PropTypes.func.isRequired }) }; AttributeLoader.defaultProps = { uri: null, identifier: null, metadata: GoodData.md }; return AttributeLoader; }(React.PureComponent)); exports.AttributeLoader = AttributeLoader; //# sourceMappingURL=AttributeLoader.js.map