UNPKG

@gooddata/react-components

Version:

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

199 lines • 8.38 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-2019 GoodData Corporation var React = require("react"); var PropTypes = require("prop-types"); var gooddata_js_1 = require("@gooddata/gooddata-js"); var get = require("lodash/get"); var isEqual = require("lodash/isEqual"); var utils_1 = require("../../../helpers/utils"); var errorStates_1 = require("../../../constants/errorStates"); var defaultChildren = function (_a) { var validElements = _a.validElements, loadMore = _a.loadMore, isLoading = _a.isLoading, error = _a.error; var paging = validElements ? validElements.paging : {}; var _b = paging.offset, offset = _b === void 0 ? 0 : _b, _c = paging.count, count = _c === void 0 ? null : _c, _d = paging.total, total = _d === void 0 ? null : _d; var nextOffset = count + offset; if (error) { return React.createElement("div", null, error); } return (React.createElement("div", null, React.createElement("p", null, "Use children function to map ", "{", " validElements, loadMore, isLoading ", "} ", "to your React components."), React.createElement("button", { className: "gd-button gd-button-secondary", onClick: loadMore, disabled: isLoading || offset + count === total }, "More"), React.createElement("h2", null, "validElements"), React.createElement("pre", null, "isLoading: ", isLoading.toString(), "offset: ", offset, "count: ", count, "total: ", total, "nextOffset: ", nextOffset, "validElements:", JSON.stringify(validElements, null, "\t")))); }; /** * AttributeElements * is a component that lists attribute values using a children function */ var AttributeElements = /** @class */ (function (_super) { __extends(AttributeElements, _super); function AttributeElements(props) { var _this = _super.call(this, props) || this; _this.uri = null; _this.getValidElementsPromise = null; _this.state = { validElements: null, isLoading: true, error: null, }; var sdk = props.sdk || gooddata_js_1.factory(); _this.sdk = sdk.clone(); utils_1.setTelemetryHeaders(_this.sdk, "AttributeElements", props); _this.loadMore = _this.loadMore.bind(_this); return _this; } AttributeElements.prototype.componentDidMount = function () { this.getValidElements(this.props, get(this.props, "options.offset", 0)); }; AttributeElements.prototype.componentWillReceiveProps = function (nextProps) { if (nextProps.sdk && this.sdk !== nextProps.sdk) { this.sdk = nextProps.sdk.clone(); utils_1.setTelemetryHeaders(this.sdk, "AttributeElements", nextProps); } if (this.props.uri !== nextProps.uri || this.props.identifier !== nextProps.identifier || this.props.projectId !== nextProps.projectId || !isEqual(this.props.options, nextProps.options)) { this.uri = null; // invalidate this.setState({ isLoading: true, validElements: null, }); this.getValidElements(nextProps, get(nextProps, "options.offset", 0)); } }; AttributeElements.prototype.loadMore = function (event) { if (event) { event.preventDefault(); } // do not execute while still loading if (this.state.isLoading) { return; } this.setState({ isLoading: true, }); var offset = get(this.state, "validElements.paging.offset", 0); var count = get(this.state, "validElements.paging.count", 0); var nextOffset = offset + count; this.getValidElements(this.props, nextOffset); }; AttributeElements.prototype.getValidElements = function (props, offset) { var _this = this; var projectId = props.projectId, options = props.options, identifier = props.identifier; var optionsWithUpdatedPaging = __assign({}, options, { offset: offset }); var uriPromise = new Promise(function (resolve, reject) { return props.uri || _this.uri ? resolve(props.uri || _this.uri) : _this.sdk.md.getUrisFromIdentifiers(projectId, [identifier]).then(function (result) { _this.uri = result[0].uri; resolve(_this.uri); }, function (error) { reject(error); }); }); var currentGetValidElementsPromise = null; // The promise needs to reset here because we are not setting it synchroneously // and even this small delay is sometimes too late this.getValidElementsPromise = null; uriPromise .then(function (uri) { var objectId = utils_1.getObjectIdFromUri(uri); currentGetValidElementsPromise = _this.sdk.md.getValidElements(projectId, objectId, // This is misdocumented as identifier, but is in fact objectId optionsWithUpdatedPaging); _this.getValidElementsPromise = currentGetValidElementsPromise; return _this.getValidElementsPromise; }) .then(function (response) { if (_this.getValidElementsPromise !== currentGetValidElementsPromise) { return Promise.reject(errorStates_1.ErrorStates.CANCELLED); } var items = get(_this.state, "validElements.items", []).concat(response.validElements.items); var offset = get(_this.state, "validElements.paging.offset", parseInt(response.validElements.paging.offset, 10) || 0); var mergedResponse = __assign({}, response.validElements, { items: items, paging: { total: parseInt(response.validElements.paging.total, 10), offset: offset, count: items.length, } }); _this.setState({ validElements: mergedResponse, isLoading: false, }); }) .catch(function (error) { if (error !== errorStates_1.ErrorStates.CANCELLED) { _this.setState({ error: error, isLoading: false, }); } }); }; AttributeElements.prototype.render = function () { var _a = this.state, validElements = _a.validElements, isLoading = _a.isLoading, error = _a.error; return this.props.children({ validElements: validElements, loadMore: this.loadMore, isLoading: isLoading, error: error, }); }; AttributeElements.propTypes = { projectId: PropTypes.string.isRequired, uri: PropTypes.string, identifier: PropTypes.string, options: PropTypes.object, }; AttributeElements.defaultProps = { projectId: null, uri: null, identifier: null, options: null, children: defaultChildren, }; return AttributeElements; }(React.PureComponent)); exports.AttributeElements = AttributeElements; //# sourceMappingURL=AttributeElements.js.map