UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

97 lines (80 loc) 3.36 kB
var _class, _temp; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React from 'react'; import PropTypes from 'prop-types'; import { withRouter } from 'react-router-dom'; import QueryString from 'query-string'; import Card from './Card'; /** * A suggested alternate query if one is available. */ var SpellCheckMessage = (_temp = _class = function (_React$Component) { _inherits(SpellCheckMessage, _React$Component); function SpellCheckMessage(props) { _classCallCheck(this, SpellCheckMessage); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.handleClick = _this.handleClick.bind(_this); return _this; } SpellCheckMessage.prototype.getMessage = function getMessage() { var searcher = this.context.searcher; if (searcher) { var response = searcher.state.response; if (response && response.totalHits === 0) { if (response.feedback) { var spellCheckMessages = response.feedback.filter(function (feedback) { return feedback.messageName === 'spellcheck.suggested'; }); return spellCheckMessages[0] ? spellCheckMessages[0].message : ''; } } } return ''; }; SpellCheckMessage.prototype.getLink = function getLink() { var message = this.getMessage(); if (message) { return React.createElement( 'a', { onClick: this.handleClick, role: 'button', tabIndex: 0 }, message ); } return null; }; SpellCheckMessage.prototype.handleClick = function handleClick() { var message = this.getMessage(); var path = '/results'; var searchString = QueryString.parse(this.props.location.search); searchString.query = message; this.props.history.push({ pathname: path, search: QueryString.stringify(searchString) }); }; SpellCheckMessage.prototype.render = function render() { var link = this.getLink(); if (link) { return React.createElement( Card, { borderless: true, style: { paddingLeft: '20%', fontSize: '2rem' } }, 'Your search returned no results.', React.createElement('br', null), 'Did you mean \u201C', link, '\u201D?' ); } return null; }; return SpellCheckMessage; }(React.Component), _class.contextTypes = { searcher: PropTypes.any }, _class.displayName = 'SpellCheckMessage', _temp); export default withRouter(SpellCheckMessage);