@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
328 lines (326 loc) • 12.1 kB
JavaScript
import _typeof from "@babel/runtime/helpers/typeof";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
//
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
import React, { Component } from 'react';
import bg403 from './assets/bg403.svg';
import bg404 from './assets/bg404.svg';
import bgCustom from './assets/bgCustom.svg';
import { Link } from 'carbon-components-react';
import { arrayOf, number, oneOfType, shape, string } from 'prop-types';
import { settings } from 'carbon-components';
import { idePrefix } from '../../globals/js/settings';
var IdeHTTPErrors = /*#__PURE__*/function (_Component) {
function IdeHTTPErrors(_props) {
var _this;
_classCallCheck(this, IdeHTTPErrors);
_this = _callSuper(this, IdeHTTPErrors, [_props]);
// Section rendering
_defineProperty(_this, "renderTitle", function () {
return _this.renderSection('title');
});
_defineProperty(_this, "renderDescription", function () {
return _this.renderSection('description');
});
_defineProperty(_this, "renderDetails", function () {
return _this.renderSection('details');
});
_defineProperty(_this, "renderLinks", function () {
var _this2 = _this,
linksPropIsObject = _this2.linksPropIsObject,
linksProp = _this2.props.links,
renderSection = _this2.renderSection;
if (!linksProp) {
return;
}
var links = linksPropIsObject ? [linksProp] : linksProp;
return renderSection('links', /*#__PURE__*/React.createElement("ul", null, links.map(function (_ref) {
var text = _ref.text,
url = _ref.url;
return /*#__PURE__*/React.createElement("li", {
className: "".concat(_this.className, "-links--link"),
key: "".concat(text, ":").concat(url)
}, /*#__PURE__*/React.createElement(Link, {
href: url
}, text));
})));
});
// Background rendering
_defineProperty(_this, "renderBackground", function (isInline) {
var _this3 = _this,
getImage = _this3.getImage,
isLargeViewport = _this3.isLargeViewport,
renderSection = _this3.renderSection,
state = _this3.state;
if (!state || isInline && isLargeViewport || !isInline && !isLargeViewport) {
return null;
}
var className = 'image-container';
var sectionClassName = isInline ? "inline-".concat(className) : className;
var background = getImage(isInline);
return renderSection(sectionClassName, background);
});
_defineProperty(_this, "getImage", function (isInline) {
var _this4 = _this,
src = _this4.background,
componentClassName = _this4.className;
var className = isInline ? "".concat(componentClassName, "-inline-image") : "".concat(componentClassName, "-image");
return /*#__PURE__*/React.createElement("img", {
alt: "",
className: className,
src: src
});
});
_defineProperty(_this, "handleResize", function () {
_this.setState({
width: _this.pageWidth.current.offsetWidth
});
});
// Layout
_defineProperty(_this, "renderSection", function (sectionClassName, children) {
if (!sectionClassName) {
return null;
}
var _this5 = _this,
className = _this5.className,
gridClasses = _this5.gridClasses,
props = _this5.props,
renderRow = _this5.renderRow;
return renderRow(/*#__PURE__*/React.createElement("div", {
className: "".concat(className, "-").concat(sectionClassName, " ").concat(gridClasses)
}, children ? children : props[sectionClassName]));
});
_defineProperty(_this, "renderRow", function (children) {
return /*#__PURE__*/React.createElement("div", {
className: "".concat(_this.gridPrefix, "--row")
}, children);
});
// Validation
_defineProperty(_this, "validateProps", function () {
var _this6 = _this,
assertBackgroundIsValid = _this6.assertBackgroundIsValid,
assertIsType = _this6.assertIsType,
assertLinksIsValid = _this6.assertLinksIsValid,
_this6$props = _this6.props,
background = _this6$props.background,
description = _this6$props.description,
details = _this6$props.details,
links = _this6$props.links,
title = _this6$props.title;
background && assertBackgroundIsValid();
description && assertIsType('string', 'description', description);
details && assertIsType('string', 'details', details);
links && assertLinksIsValid();
title && assertIsType('string', 'title', title);
});
_defineProperty(_this, "assertBackgroundIsValid", function () {
var _this7 = _this,
background = _this7.props.background,
throwError = _this7.throwError;
if (background !== 403 && background !== 404) {
throwError("background prop must be 403 or 404, got ".concat(background));
}
});
_defineProperty(_this, "assertLinksIsValid", function () {
var _this8 = _this,
assertIsValidLinkObject = _this8.assertIsValidLinkObject,
linksPropIsArray = _this8.linksPropIsArray,
linksPropIsObject = _this8.linksPropIsObject,
links = _this8.props.links,
throwError = _this8.throwError;
if (linksPropIsObject) {
assertIsValidLinkObject(links);
} else if (linksPropIsArray) {
if (!links.length) {
return throwError('links prop defined as an array must contain at least 1 item');
}
links.map(function (link) {
return assertIsValidLinkObject(link);
});
} else {
throwError("links prop must be an object or array of objects with text+url props, got ".concat(JSON.stringify(links)));
}
});
_defineProperty(_this, "assertIsType", function (type, propName, prop) {
var propType = _typeof(prop);
if (propType !== type) {
_this.throwError("".concat(propName, " prop must be type ").concat(type, ", got ").concat(propType));
}
});
_defineProperty(_this, "assertIsValidLinkObject", function (link) {
var _this9 = _this,
assertIsType = _this9.assertIsType;
var text = link.text,
url = link.url;
assertIsType('string', 'links.text', text);
assertIsType('string', 'links.url', url);
});
_defineProperty(_this, "throwError", function (msg) {
throw new Error("".concat(_this.constructor.name, ": ").concat(msg));
});
_this.pageWidth = /*#__PURE__*/React.createRef();
return _this;
}
_inherits(IdeHTTPErrors, _Component);
return _createClass(IdeHTTPErrors, [{
key: "componentDidMount",
value: function componentDidMount() {
var handleResize = this.handleResize;
handleResize();
window.addEventListener('resize', handleResize);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
}, {
key: "render",
value: function render() {
var className = this.className,
gridPrefix = this.gridPrefix,
pageWidth = this.pageWidth,
renderBackground = this.renderBackground,
renderDescription = this.renderDescription,
renderDetails = this.renderDetails,
renderLinks = this.renderLinks,
renderTitle = this.renderTitle,
validateProps = this.validateProps;
validateProps();
return /*#__PURE__*/React.createElement("div", {
className: className
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(gridPrefix, "--grid ").concat(gridPrefix, "--grid--full-width"),
ref: pageWidth
}, renderBackground(), renderTitle(), renderDescription(), renderDetails(), renderLinks(), renderBackground(true)));
}
}, {
key: "background",
get: function get() {
switch (this.props.background) {
case 403:
return bg403;
case 404:
return bg404;
default:
return bgCustom;
}
}
}, {
key: "isLargeViewport",
get: function get() {
var state = this.state;
if (!state) {
return false;
}
var width = state.width;
// NOTE @simon Hardcoded value 672 is per Carbon guidelines, see
// https://www.carbondesignsystem.com/guidelines/2x-grid/basics#breakpoints
var mediumViewportWidth = 672;
return width >= mediumViewportWidth;
}
}, {
key: "gridClasses",
get: function get() {
var gridPrefix = this.gridPrefix;
var column = function column(viewportSize, columnsToSpan) {
return "".concat(gridPrefix, "--col-").concat(viewportSize, "-").concat(columnsToSpan);
};
var offset = function offset(viewportSize, columnsToSpan) {
return "".concat(gridPrefix, "--offset-").concat(viewportSize, "-").concat(columnsToSpan);
};
return [offset('md', 3), column('md', 4), offset('md', 1), offset('max', 6), column('max', 6), offset('max', 4)].join(' ');
}
}, {
key: "gridPrefix",
get: function get() {
return settings.prefix;
}
}, {
key: "linksPropIsArray",
get: function get() {
return Array.isArray(this.props.links);
}
}, {
key: "linksPropIsObject",
get: function get() {
var links = this.props.links;
return links && Object.prototype.hasOwnProperty.call(links, 'text') && Object.prototype.hasOwnProperty.call(links, 'url');
}
// Class scope helpers
}, {
key: "className",
get: function get() {
return "".concat(idePrefix, "-http-errors");
}
}]);
}(Component);
_defineProperty(IdeHTTPErrors, "propTypes", {
/**
* Alternative background image to render for 403 and 404 pages. Must equal
* 403 or 404 if defined. A generic image is rendered if not defined.
*/
background: number,
/**
* Second row of the text section used to display a pre-translated
* description for the HTTP status code message
*/
description: string,
/**
* Third row of the text section used to display a pre-translated details
* related to the HTTP status code message and description
*/
details: string,
/**
* Fourth row of the text section used to display a list of links to
* relevant sections or documentation
*/
links: oneOfType([
/**
* A single link object
*/
shape({
/**
* The pre-translated text to display for the link
*/
text: string.isRequired,
/**
* The link's destination
*/
url: string.isRequired
}),
/**
* An array of links objects
*/
arrayOf(shape({
/**
* The pre-translated text to display for the link
*/
text: string.isRequired,
/**
* The link's destination
*/
url: string.isRequired
}))]),
/**
* First row of the text section used to display a pre-translated HTTP
* status code message
*/
title: string
});
_defineProperty(IdeHTTPErrors, "defaultProps", {
title: 'CHANGE ME',
description: 'CHANGE ME'
});
export { IdeHTTPErrors as default };