/* * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. * Copyright © 2017-2018 Amdocs * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ import React, {Component} from 'react'; import { PropTypes } from 'prop-types'; import {Button} from 'react-bootstrap'; import AutoSuggest from 'react-autosuggest'; import Highlighter from 'react-highlight-words'; import debounce from 'lodash.debounce'; import {ButtonGroup} from 'react-bootstrap'; import Modal from 'react-bootstrap/lib/Modal'; import {Link} from 'react-router-dom'; import {genericRequest} from 'app/networking/NetworkCalls.js'; import {changeUrlAddress} from 'utils/Routes.js'; import { ICON_CLASS_SEARCH, ICON_CLASS_CLEAR, ICON_CLASS_HELP, SEARCH_DEBOUNCE_TIME, NO_MATCHES_FOUND, SEARCH_PLACEHOLDER_TEXT } from './AutoCompleteSearchBarConstants.js'; export default class AutoCompleteSearchBar extends Component { static propTypes = { value: PropTypes.string, suggestions: PropTypes.array, cachedSuggestions: PropTypes.array, suggestionName: PropTypes.string }; constructor(props) { console.log(props); super(props); this.state = { helpModalShow: false, searchable: [] }; }; componentWillMount() { this.debouncedLoadSuggestions = debounce(this.props.onSuggestionsFetchRequested, SEARCH_DEBOUNCE_TIME); } onSuggestionsFetchRequested = ({value}) => { this.debouncedLoadSuggestions({value}); }; isValidSearch(value) { return (value && value !== NO_MATCHES_FOUND); } isValidSuggestionObject(suggestionObj) { return (suggestionObj && Object.keys(suggestionObj).length > 0 && this.isValidSearch(suggestionObj.text)); } getSelectedSuggestionObj(value, cachedSuggestions) { let matchesSuggestion = {}; if (this.isValidSearch(value)) { for (let suggestionIndex in cachedSuggestions) { if (cachedSuggestions[suggestionIndex].text === value) { matchesSuggestion = cachedSuggestions[suggestionIndex]; break; } } } return matchesSuggestion; } newSearchSelected( cachedSuggestion, invalidSearchCallback, dispatchAnalytics, value) { if (this.isValidSuggestionObject(cachedSuggestion)) { changeUrlAddress(cachedSuggestion, this.props.history); //Call analytics if defined if (dispatchAnalytics) { dispatchAnalytics(); } } else { invalidSearchCallback(value); } } render() { const { value, suggestions, suggestionName, cachedSuggestions, onInputChange, onInvalidSearch, onClearSuggestionsTextFieldRequested, onSuggestionsClearRequested, dispatchAnalytics } = this.props; const inputProps = { placeholder: SEARCH_PLACEHOLDER_TEXT, value, onChange: onInputChange }; let closeHelpModal = () => { this.setState({helpModalShow: false}); }; let showHelpModal = () => { genericRequest('/schema/searchable', true, 'GET').then(res=>{ let searchDOM = res.sort(function(a, b) { var compareA = (a['node-type']).toLowerCase(); var compareB = (b['node-type']).toLowerCase(); if(compareA < compareB){ return -1; }; if(compareA > compareB){ return 1; }; return 0; }).map((prop) => { return (

{prop['node-type']}:

{prop['searchable-attributes']}

); }); this.setState({searchable: searchDOM, helpModalShow: true}); }, error => { console.log(error); this.setState({searchable: 'An error occurred, please try again later.', helpModalShow: true}); }).catch(error => { console.log(error); this.setState({searchable: 'An error occurred, please try again later.', helpModalShow: true}); }); }; let clearButtonClass = (value.length > 0) ? 'auto-complete-clear-button' : 'auto-complete-clear-button hidden'; return (
suggestion[suggestionName]} onSuggestionsFetchRequested={this.onSuggestionsFetchRequested} onSuggestionsClearRequested={onSuggestionsClearRequested} onSuggestionSelected={(event, {suggestion}) => { this.newSearchSelected(suggestion, onInvalidSearch, dispatchAnalytics, value); this.props.onClearSuggestionsTextFieldRequested(); }} renderSuggestion={this.renderSuggestion} inputProps={inputProps} focusFirstSuggestion={false} renderSuggestionsContainer={this.renderSuggestionsContainer}/>