import PropTypes from 'prop-types'; import React, { Component } from 'react'; import TextInput from 'Components/Form/TextInput'; import Icon from 'Components/Icon'; import Button from 'Components/Link/Button'; import Link from 'Components/Link/Link'; import LoadingIndicator from 'Components/Loading/LoadingIndicator'; import PageContent from 'Components/Page/PageContent'; import PageContentBody from 'Components/Page/PageContentBody'; import { icons, kinds } from 'Helpers/Props'; import getErrorMessage from 'Utilities/Object/getErrorMessage'; import translate from 'Utilities/String/translate'; import AddNewAlbumSearchResultConnector from './Album/AddNewAlbumSearchResultConnector'; import AddNewArtistSearchResultConnector from './Artist/AddNewArtistSearchResultConnector'; import styles from './AddNewItem.css'; class AddNewItem extends Component { // // Lifecycle constructor(props, context) { super(props, context); this.state = { term: props.term || '', isFetching: false }; } componentDidMount() { const term = this.state.term; if (term) { this.props.onSearchChange(term); } } componentDidUpdate(prevProps) { const { term, isFetching } = this.props; if (term && term !== prevProps.term) { this.setState({ term, isFetching: true }); this.props.onSearchChange(term); } else if (isFetching !== prevProps.isFetching) { this.setState({ isFetching }); } } // // Listeners onSearchInputChange = ({ value }) => { const hasValue = !!value.trim(); this.setState({ term: value, isFetching: hasValue }, () => { if (hasValue) { this.props.onSearchChange(value); } else { this.props.onClearSearch(); } }); }; onClearSearchPress = () => { this.setState({ term: '' }); this.props.onClearSearch(); }; // // Render render() { const { error, items, hasExistingArtists } = this.props; const term = this.state.term; const isFetching = this.state.isFetching; return (
{ isFetching && } { !isFetching && !!error ?
{translate('FailedLoadingSearchResults')}
{getErrorMessage(error)}
: null } { !isFetching && !error && !!items.length &&
{ items.map((item) => { if (item.artist) { const artist = item.artist; return ( ); } else if (item.album) { const album = item.album; return ( ); } return null; }) }
} { !isFetching && !error && !items.length && !!term &&
{translate('CouldntFindAnyResultsForTerm', [term])}
You can also search using the MusicBrainz ID of an artist or release group e.g. lidarr:cc197bad-dc9c-440d-a5b5-d52ba2e14234
} { term ? null :
{translate('ItsEasyToAddANewArtistJustStartTypingTheNameOfTheArtistYouWantToAdd')}
You can also search using the MusicBrainz ID of an artist e.g. lidarr:cc197bad-dc9c-440d-a5b5-d52ba2e14234
} { !term && !hasExistingArtists ?
You haven't added any artists yet, do you want to add an existing library location (Root Folder) and update?
: null }
); } } AddNewItem.propTypes = { term: PropTypes.string, isFetching: PropTypes.bool.isRequired, error: PropTypes.object, isAdding: PropTypes.bool.isRequired, addError: PropTypes.object, items: PropTypes.arrayOf(PropTypes.object).isRequired, hasExistingArtists: PropTypes.bool.isRequired, onSearchChange: PropTypes.func.isRequired, onClearSearch: PropTypes.func.isRequired }; export default AddNewItem;