This commit is contained in:
Kongzibapt 2021-02-08 18:07:00 +01:00
commit c82e2cf251
2 changed files with 44 additions and 8 deletions

View file

@ -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 (
<div id="sortBox">
@ -24,7 +60,7 @@ class Sort extends Component {
<div id="selectAndOrder">
<div id="selectBlock">
<div id={this.state.unwound ? 'unwoundMenu' : 'selectBox'} >
<select id='sortSelect' onClick={this.changeArrowDirection} onChange={this.props.handleChangeSortType}>
<select id='sortSelect' onClick={this.changeArrowDirection} onChange={this.handleChangeSortType}>
<option className="optionSort" value="name">Ordre Alphabétique</option>
<option className="optionSort" value="category_id">Catégorie</option>
<option className="optionSort" value="quantity">Quantité</option>
@ -33,7 +69,7 @@ class Sort extends Component {
</div>
</div>
<p id="sortOrder" onClick={this.props.handleChangeSortOrder} hidden={this.props.orderIsHidden}>
{this.props.lowToHigh ? "A-Z" : "Z-A"}
{this.props.lowToHigh ? this.state.increasingText : this.state.decreasingText}
</p>
</div>
</div>

View file

@ -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);
});