front/src/Routes/Search.js

121 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-03-16 22:10:41 +01:00
import React from 'react';
2019-04-13 13:59:58 +02:00
import PropTypes from 'prop-types';
2019-03-16 22:10:41 +01:00
import {
Container,
Row,
Col,
} from 'reactstrap';
import {
NotificationContainer,
NotificationManager,
} from 'react-notifications';
import Api from '../Components/Api';
import MapItem from '../Components/Map';
import Vegetables from '../Components/Vegetables';
2019-04-13 13:59:58 +02:00
class Search extends React.Component {
2019-03-16 22:10:41 +01:00
constructor(props) {
super(props);
2019-04-13 13:59:58 +02:00
const {
match,
changeBackground,
} = props;
2019-03-16 22:10:41 +01:00
this.state = {
2019-04-13 13:59:58 +02:00
query: match.params.query || null,
2019-03-16 22:10:41 +01:00
selectedType: {},
selectedVegetable: {},
};
this.getVegetables = this.getVegetables.bind(this);
this.selectVegetable = this.selectVegetable.bind(this);
this.getVegetables();
2019-04-13 13:59:58 +02:00
changeBackground('map');
2019-03-16 22:10:41 +01:00
}
getVegetables() {
const {
query,
} = this.state;
Api.get(`search/vegetables/?q=${query}`)
.then((res) => {
this.setState({
selectedType: {
Vegetables: res.data.rows,
2019-03-16 22:20:25 +01:00
id: res.data.rows.length > 0 ? res.data.rows[0].Type.id : null,
name: res.data.rows.length > 0 ? res.data.rows[0].Type.name : null,
2019-03-16 22:10:41 +01:00
},
});
})
.catch(() => {
NotificationManager.error('Erreur lors de la récupération des catégories');
});
}
selectVegetable(vegetable) {
this.setState({
selectedVegetable: vegetable,
});
}
render() {
2019-04-13 13:59:58 +02:00
const {
history,
} = this.props;
const {
selectedType,
selectedVegetable,
} = this.state;
2019-03-16 22:10:41 +01:00
return (
<Container>
<NotificationContainer />
<Row>
<Col xs="12" sm="8">
<MapItem
2019-04-13 13:59:58 +02:00
selectedType={selectedType}
selectedVegetable={selectedVegetable}
2019-03-16 22:10:41 +01:00
selectVegetable={this.selectVegetable}
/>
</Col>
<Col xs="12" sm="4">
<Row>
2019-04-09 20:00:47 +02:00
<Col xs="12">
2019-03-16 22:10:41 +01:00
<Vegetables
2019-04-13 13:59:58 +02:00
selectedType={selectedType}
selectedVegetable={selectedVegetable}
2019-03-16 22:10:41 +01:00
selectVegetable={this.selectVegetable}
2019-04-13 13:59:58 +02:00
simple
history={history}
2019-03-16 22:10:41 +01:00
/>
</Col>
</Row>
</Col>
</Row>
</Container>
);
}
}
2019-04-13 13:59:58 +02:00
Search.defaultProps = {
match: {
params: {
query: null,
},
},
};
Search.propTypes = {
changeBackground: PropTypes.func.isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
query: PropTypes.string,
}),
}),
history: PropTypes.shape().isRequired,
};
export default Search;