2018-01-13 02:01:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import parseUrl from 'Utilities/String/parseUrl';
|
|
|
|
import { lookupSeries, clearAddSeries } from 'Store/Actions/addSeriesActions';
|
|
|
|
import { fetchRootFolders } from 'Store/Actions/rootFolderActions';
|
|
|
|
import AddNewSeries from './AddNewSeries';
|
|
|
|
|
|
|
|
function createMapStateToProps() {
|
|
|
|
return createSelector(
|
|
|
|
(state) => state.addSeries,
|
2019-08-04 01:55:31 +00:00
|
|
|
(state) => state.series.items.length,
|
2019-03-01 04:01:06 +00:00
|
|
|
(state) => state.router.location,
|
2019-08-04 01:55:31 +00:00
|
|
|
(addSeries, existingSeriesCount, location) => {
|
2018-01-13 02:01:27 +00:00
|
|
|
const { params } = parseUrl(location.search);
|
|
|
|
|
|
|
|
return {
|
2019-08-04 01:55:31 +00:00
|
|
|
...addSeries,
|
2018-01-13 02:01:27 +00:00
|
|
|
term: params.term,
|
2019-08-04 01:55:31 +00:00
|
|
|
hasExistingSeries: existingSeriesCount > 0
|
2018-01-13 02:01:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
|
|
|
lookupSeries,
|
|
|
|
clearAddSeries,
|
|
|
|
fetchRootFolders
|
|
|
|
};
|
|
|
|
|
|
|
|
class AddNewSeriesConnector extends Component {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lifecycle
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this._seriesLookupTimeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.fetchRootFolders();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this._seriesLookupTimeout) {
|
|
|
|
clearTimeout(this._seriesLookupTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.clearAddSeries();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Listeners
|
|
|
|
|
|
|
|
onSeriesLookupChange = (term) => {
|
|
|
|
if (this._seriesLookupTimeout) {
|
|
|
|
clearTimeout(this._seriesLookupTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (term.trim() === '') {
|
|
|
|
this.props.clearAddSeries();
|
|
|
|
} else {
|
|
|
|
this._seriesLookupTimeout = setTimeout(() => {
|
|
|
|
this.props.lookupSeries({ term });
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onClearSeriesLookup = () => {
|
|
|
|
this.props.clearAddSeries();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Render
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
term,
|
|
|
|
...otherProps
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AddNewSeries
|
|
|
|
term={term}
|
|
|
|
{...otherProps}
|
|
|
|
onSeriesLookupChange={this.onSeriesLookupChange}
|
|
|
|
onClearSeriesLookup={this.onClearSeriesLookup}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddNewSeriesConnector.propTypes = {
|
|
|
|
term: PropTypes.string,
|
|
|
|
lookupSeries: PropTypes.func.isRequired,
|
|
|
|
clearAddSeries: PropTypes.func.isRequired,
|
|
|
|
fetchRootFolders: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(createMapStateToProps, mapDispatchToProps)(AddNewSeriesConnector);
|