You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/frontend/src/Search/AddNewItemConnector.js

105 lines
2.2 KiB

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { clearSearchResults, getSearchResults } from 'Store/Actions/searchActions';
import { fetchRootFolders } from 'Store/Actions/settingsActions';
import parseUrl from 'Utilities/String/parseUrl';
import AddNewItem from './AddNewItem';
function createMapStateToProps() {
return createSelector(
(state) => state.search,
(state) => state.artist.items.length,
(state) => state.router.location,
(search, existingArtistsCount, location) => {
const { params } = parseUrl(location.search);
return {
...search,
term: params.term,
hasExistingArtists: existingArtistsCount > 0
};
}
);
}
const mapDispatchToProps = {
getSearchResults,
clearSearchResults,
fetchRootFolders
};
class AddNewItemConnector extends Component {
//
// Lifecycle
constructor(props, context) {
super(props, context);
this._searchTimeout = null;
}
componentDidMount() {
this.props.fetchRootFolders();
}
componentWillUnmount() {
if (this._searchTimeout) {
clearTimeout(this._searchTimeout);
}
this.props.clearSearchResults();
}
//
// Listeners
onSearchChange = (term) => {
if (this._searchTimeout) {
clearTimeout(this._searchTimeout);
}
if (term.trim() === '') {
this.props.clearSearchResults();
} else {
this._searchTimeout = setTimeout(() => {
this.props.getSearchResults({ term });
}, 300);
}
};
onClearSearch = () => {
this.props.clearSearchResults();
};
//
// Render
render() {
const {
term,
...otherProps
} = this.props;
return (
<AddNewItem
term={term}
{...otherProps}
onSearchChange={this.onSearchChange}
onClearSearch={this.onClearSearch}
/>
);
}
}
AddNewItemConnector.propTypes = {
term: PropTypes.string,
getSearchResults: PropTypes.func.isRequired,
clearSearchResults: PropTypes.func.isRequired,
fetchRootFolders: PropTypes.func.isRequired
};
export default connect(createMapStateToProps, mapDispatchToProps)(AddNewItemConnector);