@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
126 lines • 4.98 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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2007-2020 GoodData Corporation
var React = require("react");
var PropTypes = require("prop-types");
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;
});
}
function getAttribute(metadata, uri) {
return metadata.getObjectDetails(uri).then(function (result) {
if (!result || !result.attribute) {
throw new Error('Invalid data uri. Required data uri must be of type "attribute"');
}
return result.attribute;
});
}
var AttributeLoader = /** @class */ (function (_super) {
__extends(AttributeLoader, _super);
function AttributeLoader(props) {
var _this = _super.call(this, props) || this;
_this.state = {
attribute: null,
attributeDisplayForm: null,
isLoading: true,
isUsingIdentifier: false,
error: null,
};
return _this;
}
AttributeLoader.prototype.componentDidMount = function () {
this.getAttributeDetails(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,
});
this.getAttributeDetails(nextProps);
}
};
AttributeLoader.prototype.render = function () {
var _a = this.state, attributeDisplayForm = _a.attributeDisplayForm, isLoading = _a.isLoading, isUsingIdentifier = _a.isUsingIdentifier, error = _a.error, attribute = _a.attribute;
return this.props.children({
attribute: attribute,
attributeDisplayForm: attributeDisplayForm,
isLoading: isLoading,
isUsingIdentifier: isUsingIdentifier,
error: error,
});
};
AttributeLoader.prototype.getAttributeDetails = 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) {
getAttribute(metadata, attributeDisplayForm.content.formOf).then(function (attribute) {
_this.setState({
attribute: attribute,
attributeDisplayForm: attributeDisplayForm,
isLoading: false,
isUsingIdentifier: !!identifier,
error: null,
});
});
}, function (error) {
_this.setState({
attribute: null,
attributeDisplayForm: null,
isLoading: false,
error: error,
});
});
};
AttributeLoader.propTypes = {
projectId: PropTypes.string,
uri: PropTypes.string,
identifier: PropTypes.string,
metadata: PropTypes.shape({
getObjectDetails: PropTypes.func.isRequired,
getObjectUri: PropTypes.func.isRequired,
}).isRequired,
};
AttributeLoader.defaultProps = {
uri: null,
identifier: null,
projectId: null,
};
return AttributeLoader;
}(React.PureComponent));
exports.AttributeLoader = AttributeLoader;
//# sourceMappingURL=AttributeLoader.js.map