I'm trying to setState of order to be the content of catalog only if it's true, I guess I need to make a function that always listen if the state of order changes.
This is my state
constructor(props) {
super(props)
this.state = {
catalog: [
{
photo: 'https://via.placeholder.com/164x168',
title: 'FirstItem',
description: 'Fugiat nulla incididunt laborum et esse adipisicing esse aliquip.',
size_m: { size: 'M', price: 5000, id: 'FTM', selectedIndicator: '', isSelected: false },
size_l: { size: 'L', price: 8000, id: 'FTL', selectedIndicator: '', isSelected: false },
size_xl: { size: 'XL', price: 10000, id: 'FTXL', selectedIndicator: '', isSelected: false },
},
{
photo: 'https://via.placeholder.com/164x168',
title: 'SecondItem',
description: 'Fugiat nulla incididunt laborum et esse adipisicing esse aliquip.',
size_m: { size: 'M', price: 5000, id: 'SCRM', selectedIndicator: '', isSelected: false },
size_l: { size: 'L', price: 8000, id: 'SCRL', selectedIndicator: '', isSelected: false },
size_xl: { size: 'XL', price: 10000, id: 'SCXL', selectedIndicator: '', isSelected: false },
},
],
order: []
}
}
Example: In case of size_m of first item and size_l of second item in catalog change isSelected to true, order state should update to
order: [
{
size_m: { size: 'M', price: 5000, id: 'FTM', selectedIndicator: '', isSelected: true },
size_l: { size: 'L', price: 8000, id: 'SCRL', selectedIndicator: '', isSelected: true }
}
]
This is my handler function
setOrder = () => {
const { catalog } = this.state
let catalogOrder = []
catalog.map(item => {
if(item.size_m.isSelected) {
catalogOrder = [item]
}
return this.setState({order: catalogOrder})
})
}