I am trying to organize the content of an array (that is also a state) and then, take this organized data to set this value into another state.
The problem is that even inserting the process into a promise and making the set state into then() function the state isnt getting updated correctly.
I wrote a console.log to show the content and it just show up if i save my code again making a CTRL + S in my code editor.
You can take a look in the code… I don’t know whats wrong.
async function organizaBairros(){
const pedBairro = {};
const processPedidos = (pedidos, status) => {
pedidos.forEach(pedido => {
const bairro = pedido.nbl_bairro;
if (!pedBairro[bairro]) {
pedBairro[bairro] = {
totalNotasA: 0,
totalNotasP: 0,
totalNotasS: 0,
totalValueA: 0,
totalValueP: 0,
totalValueS: 0
};
}
if (status === 'A') {
pedBairro[bairro].totalNotasA += 1;
pedBairro[bairro].totalValueA += pedido.nbl_total_nota;
} else if(status === 'P') {
pedBairro[bairro].totalNotasP += 1;
pedBairro[bairro].totalValueP += pedido.nbl_total_nota;
} else if(status === 'S') {
pedBairro[bairro].totalNotasS += 1;
pedBairro[bairro].totalValueS += pedido.nbl_total_nota;
}
});
};
Promise.all([
processPedidos(jsonPedidosA, 'A'),
processPedidos(jsonPedidosP, 'P'),
processPedidos(jsonPedidosS, 'S')
]).then(() => {
console.log(pedBairro)
setPedidosBairro(pedBairro);
}).catch(error => {
console.error('An error occurred:', error);
});
}