Les tris ordre croissant/decroissant fonctionnent indépendamment, il faut rendre ça generique

This commit is contained in:
Florian Ehr 2021-02-06 00:40:59 +01:00
rodzic 4f47f96b4c
commit 1de84726e0
2 zmienionych plików z 16 dodań i 7 usunięć

Wyświetl plik

@ -23,7 +23,7 @@ class Sort extends Component {
<option className="optionSort" value="Ordre Alphabétique">Ordre Alphabétique</option>
<option className="optionSort" value="Catégorie">Catégorie</option>
<option className="optionSort" value="Quantité">Quantité</option>
<option className="optionSort" value="Quantité">Prix</option>
<option className="optionSort" value="Prix">Prix</option>
</select>
</div>
</div>

Wyświetl plik

@ -66,7 +66,7 @@ export default class Stock extends React.Component {
sortArticles = (sortType) => {
let sorted;
//TRI PAR ORDRE ALPHABETIQUE
//TRI PAR ORDRE ALPHABETIQUE OU PRIX
if (sortType === "name"){
this.setState({orderIsHidden: false})
if (this.state.lowToHigh) {
@ -76,7 +76,19 @@ export default class Stock extends React.Component {
//ordre alphabétique décroissant
sorted = [...this.state.articles].sort((a, b) => b.name.localeCompare(a.name))
}
} else if (sortType === "price") {
console.log('tri pix')
this.setState({orderIsHidden: false})
if (this.state.lowToHigh) {
//ordre croissant
sorted = [...this.state.articles].sort((a, b) => a.price - b.price)
} else {
//ordre décroissant
sorted = [...this.state.articles].sort((a, b) => b.price - a.price)
}
} else {
console.log('autre 1')
this.setState({orderIsHidden: true})
this.setState({lowToHigh: false})
sorted = [...this.state.articles].sort((a, b) => b[sortType] - a[sortType]);
@ -88,16 +100,12 @@ export default class Stock extends React.Component {
this.setState({onSortedCategories:true})
})
}else{
console.log('autre 2')
this.setState({articles:sorted},() => {
console.log(this.state.articles)
this.setState({onSortedCategories:false})
})
}
//TRI PAR PRIX
if (sortType === 'price') {
}
}
getArticles = () => {
@ -160,6 +168,7 @@ export default class Stock extends React.Component {
}
handleChangeSortType = (e) => {
console.log(e.target.value)
this.setState({sortType:e.target.value},()=>{
if (this.state.sortType === "Ordre Alphabétique"){
this.sortArticles("name")