@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
110 lines • 5.87 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) 2020 GoodData Corporation
var React = require("react");
var isEqual = require("lodash/isEqual");
var react_intl_1 = require("react-intl");
var IntlWrapper_1 = require("./IntlWrapper");
var ErrorComponent_1 = require("../../simple/ErrorComponent");
var errorStates_1 = require("../../../constants/errorStates");
var RuntimeError_1 = require("../../../errors/RuntimeError");
var errorHandlers_1 = require("../../../helpers/errorHandlers");
var common_1 = require("../../../helpers/geoChart/common");
function geoValidatorHOC(InnerComponent) {
var ValidatorHOCWrapped = /** @class */ (function (_super) {
__extends(ValidatorHOCWrapped, _super);
function ValidatorHOCWrapped(props) {
var _this = _super.call(this, props) || this;
_this.errorMap = errorHandlers_1.generateErrorMap(props.intl);
return _this;
}
ValidatorHOCWrapped.prototype.render = function () {
this.initError();
if (this.isMapboxTokenMissing) {
return this.renderErrorComponent(errorStates_1.ErrorStates.GEO_MAPBOX_TOKEN_MISSING);
}
if (this.isLocationMissing) {
return this.renderErrorComponent(errorStates_1.ErrorStates.GEO_LOCATION_MISSING);
}
return React.createElement(InnerComponent, __assign({ key: "InnerComponent" }, this.props));
};
ValidatorHOCWrapped.prototype.shouldComponentUpdate = function (nextProps) {
var _a = this.props, config = _a.config, dataSource = _a.dataSource, drillableItems = _a.drillableItems;
var nextConfig = nextProps.config, nextDataSource = nextProps.dataSource, nextDrillableItems = nextProps.drillableItems;
// check if buckets, filters and config are changed
var isSameConfig = this.isSameConfig(config, nextConfig);
var isSameDataSource = isEqual(dataSource.getAfm(), nextDataSource.getAfm());
var isSameDrillableItems = isEqual(drillableItems, nextDrillableItems);
return !isSameConfig || !isSameDataSource || !isSameDrillableItems;
};
ValidatorHOCWrapped.prototype.componentDidUpdate = function () {
this.handleError();
};
ValidatorHOCWrapped.prototype.componentDidMount = function () {
this.handleError();
};
ValidatorHOCWrapped.prototype.initError = function () {
var _a = this.props.config, _b = _a.mdObject, _c = (_b === void 0 ? {} : _b).buckets, buckets = _c === void 0 ? [] : _c, mapboxToken = _a.mapboxToken;
this.isLocationMissing = common_1.isLocationMissing(buckets);
this.isMapboxTokenMissing = !Boolean(mapboxToken);
};
ValidatorHOCWrapped.prototype.handleError = function () {
var onError = this.props.onError;
if (onError && this.isLocationMissing) {
onError(new RuntimeError_1.RuntimeError(errorStates_1.ErrorStates.GEO_LOCATION_MISSING));
}
};
ValidatorHOCWrapped.prototype.renderErrorComponent = function (errorState) {
var ErrorComponent = this.props.ErrorComponent || ErrorComponent_1.ErrorComponent;
// in this case, we show "Sorry, we can't display this insight"
var errorProps = this.errorMap[errorState] || this.errorMap[errorStates_1.ErrorStates.UNKNOWN_ERROR];
return React.createElement(ErrorComponent, __assign({ code: errorState }, errorProps));
};
ValidatorHOCWrapped.prototype.isSameConfig = function (config, nextConfig) {
var colorMapping = (config.colorMapping || []).map(function (currentColor) { return currentColor.color; });
var nextColorMapping = (nextConfig.colorMapping || []).map(function (newColor) { return newColor.color; });
var configProps = __assign({}, config, { colorMapping: colorMapping });
var nextConfigProps = __assign({}, nextConfig, { colorMapping: nextColorMapping });
return isEqual(configProps, nextConfigProps);
};
return ValidatorHOCWrapped;
}(React.Component));
var IntlValidatorHOC = react_intl_1.injectIntl(ValidatorHOCWrapped);
return /** @class */ (function (_super) {
__extends(ValidatorHOC, _super);
function ValidatorHOC() {
return _super !== null && _super.apply(this, arguments) || this;
}
ValidatorHOC.prototype.render = function () {
return (React.createElement(IntlWrapper_1.IntlWrapper, { locale: this.props.locale },
React.createElement(IntlValidatorHOC, __assign({}, this.props))));
};
return ValidatorHOC;
}(React.Component));
}
exports.geoValidatorHOC = geoValidatorHOC;
//# sourceMappingURL=GeoValidatorHOC.js.map