File size: 1,642 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import React from "react";
import "./SearchBar.scss";
import { withRouter } from "react-router";
import { selectSearchItems } from "../../Redux/Search/search-selectors";
import { getSearchData } from "../../Redux/Search/search-actions";
import { connect } from "react-redux";
import { compose } from "redux";
class SearchBar extends React.Component {
constructor() {
super();
this.state = {
currentPath: ""
};
}
handleChange = event => {
const { value } = event.target;
if (
Number(value.length) === 1 &&
this.props.currentRoute !== "/searchresults"
) {
this.setState({ currentPath: this.props.currentRoute }, () =>
this.props.history.push("/searchresults")
);
} else if (Number(value.length) === 0) {
this.props.history.push(`${this.state.currentPath}`);
} else if (Number(value.length) > 1) {
this.props.history.push("/searchresults");
}
return value ? this.props.dispatch(getSearchData(value)) : null;
};
render() {
return (
<div className="container-1">
<div className="container-2">
<span className="search-icon">
<i className="fa fa-search"></i>
</span>
<input
onChange={this.handleChange}
type="search"
id="search"
placeholder="Movies, TV Shows..."
/>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
searchItems: selectSearchItems(state)
});
export default compose(
withRouter,
connect(mapStateToProps)
)(SearchBar);
|