import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { Manager, Popper, Reference } from 'react-popper'; import getUniqueElememtId from 'Utilities/getUniqueElementId'; import { icons, kinds } from 'Helpers/Props'; import Icon from 'Components/Icon'; import Portal from 'Components/Portal'; import FormInputButton from 'Components/Form/FormInputButton'; import Link from 'Components/Link/Link'; import LoadingIndicator from 'Components/Loading/LoadingIndicator'; import TextInput from 'Components/Form/TextInput'; import ImportArtistSearchResultConnector from './ImportArtistSearchResultConnector'; import ImportArtistName from './ImportArtistName'; import styles from './ImportArtistSelectArtist.css'; class ImportArtistSelectArtist extends Component { // // Lifecycle constructor(props, context) { super(props, context); this._artistLookupTimeout = null; this._scheduleUpdate = null; this._buttonId = getUniqueElememtId(); this._contentId = getUniqueElememtId(); this.state = { term: props.id, isOpen: false }; } componentDidUpdate() { if (this._scheduleUpdate) { this._scheduleUpdate(); } } // // Control _addListener() { window.addEventListener('click', this.onWindowClick); } _removeListener() { window.removeEventListener('click', this.onWindowClick); } // // Listeners onWindowClick = (event) => { const button = document.getElementById(this._buttonId); const content = document.getElementById(this._contentId); if (!button || !content) { return; } if ( !button.contains(event.target) && !content.contains(event.target) && this.state.isOpen ) { this.setState({ isOpen: false }); this._removeListener(); } } onPress = () => { if (this.state.isOpen) { this._removeListener(); } else { this._addListener(); } this.setState({ isOpen: !this.state.isOpen }); } onSearchInputChange = ({ value }) => { if (this._artistLookupTimeout) { clearTimeout(this._artistLookupTimeout); } this.setState({ term: value }, () => { this._artistLookupTimeout = setTimeout(() => { this.props.onSearchInputChange(value); }, 200); }); } onRefreshPress = () => { this.props.onSearchInputChange(this.state.term); } onArtistSelect = (foreignArtistId) => { this.setState({ isOpen: false }); this.props.onArtistSelect(foreignArtistId); } // // Render render() { const { selectedArtist, isExistingArtist, isFetching, isPopulated, error, items, isQueued, isLookingUpArtist } = this.props; const errorMessage = error && error.responseJSON && error.responseJSON.message; return ( {({ ref }) => (
{ isLookingUpArtist && isQueued && !isPopulated ? : null } { isPopulated && selectedArtist && isExistingArtist ? : null } { isPopulated && selectedArtist ? : null } { isPopulated && !selectedArtist ?
No match found!
: null } { !isFetching && !!error ?
Search failed, please try again later.
: null }
)}
{({ ref, style, scheduleUpdate }) => { this._scheduleUpdate = scheduleUpdate; return (
{ this.state.isOpen ?
{ items.map((item) => { return ( ); }) }
: null }
); }}
); } } ImportArtistSelectArtist.propTypes = { id: PropTypes.string.isRequired, selectedArtist: PropTypes.object, isExistingArtist: PropTypes.bool.isRequired, isFetching: PropTypes.bool.isRequired, isPopulated: PropTypes.bool.isRequired, error: PropTypes.object, items: PropTypes.arrayOf(PropTypes.object).isRequired, isQueued: PropTypes.bool.isRequired, isLookingUpArtist: PropTypes.bool.isRequired, onSearchInputChange: PropTypes.func.isRequired, onArtistSelect: PropTypes.func.isRequired }; ImportArtistSelectArtist.defaultProps = { isFetching: true, isPopulated: false, items: [], isQueued: true }; export default ImportArtistSelectArtist;