diff --git a/src/js/Components/Sort.js b/src/js/Components/Sort.js index 11c469b..91a2d1c 100644 --- a/src/js/Components/Sort.js +++ b/src/js/Components/Sort.js @@ -8,6 +8,7 @@ class Sort extends Component { this.state = { unwound:false } + this.handleChangeSortType = this.handleChangeSortType.bind(this) } changeArrowDirection = () => { @@ -15,6 +16,41 @@ class Sort extends Component { this.setState({unwound:!this.state.unwound}) } + componentDidMount() { + console.log("SORT MOUNTED") + this.setState({sortType: "name"}, () => { + this.setOrderText() + }) + } + + setOrderText() { + switch(this.state.sortType) { + case "name": + this.setState({ + increasingText: "a->z", + decreasingText: "z->a" + }) + break; + case "price": + this.setState({ + increasingText: "0->9", + decreasingText: "9->0" + }) + break; + default: + break; + } + } + + handleChangeSortType(e) { + let type = e.target.value + this.setState({sortType: type}, () => { + this.setOrderText() + }) + + this.props.handleChangeSortType(type) + } + render() { return (
@@ -24,7 +60,7 @@ class Sort extends Component {
- @@ -33,7 +69,7 @@ class Sort extends Component {
diff --git a/src/js/Views/Stock.js b/src/js/Views/Stock.js index fc19386..6a6666c 100644 --- a/src/js/Views/Stock.js +++ b/src/js/Views/Stock.js @@ -107,7 +107,6 @@ export default class Stock extends React.Component { //ordre décroissant sorted = [...this.state.articles].sort((a, b) => b.price - a.price) } - } else { console.log('autre 1') this.setState({orderIsHidden: true}) @@ -134,7 +133,7 @@ export default class Stock extends React.Component { .then(res => { this.setState({articles : res.data}, () => { console.log(this.state.articles) - this.sortArticles("name") + this.setState({sortType: "name"}) }) }) .catch(error => { @@ -204,16 +203,17 @@ export default class Stock extends React.Component { // }) // } - handleChangeSortType = (e) => { - console.log(e.target.value) - this.setState({sortType: e.target.value}, () => { + //ATTENTION ça a changé, on passe directement le string en argument (j'avais besoin d'utiliser cette fonction autrement, voir dans Sort.js) + handleChangeSortType = (type) => { + console.log(type) + this.setState({sortType: type}, () => { this.sortArticles(this.state.sortType) }) } //Gère le choix de tri par ordre croissant ou décroissant quand c'est possible avec le tri actuel handleChangeSortOrder = e => { - this.setState({lowToHigh: (this.state.lowToHigh + 1) % 2},() => { + this.setState({lowToHigh: !this.state.lowToHigh},() => { console.log(this.state.sortType) this.sortArticles(this.state.sortType); });