I have an array data, using react-redux:
data = [{
class: "A",
seat: [
{ id: "A1", price: 7.5, status: false },
{ id: "A2", price: 7.5, status: false },
{ id: "A3", price: 7.5, status: false },
{ id: "A4", price: 7.5, status: false },
],
},
{
class: "B",
seat: [
{ id: "B1", price: 7.5, status: false },
{ id: "B2", price: 7.5, status: false },
{ id: "B3", price: 7.5, status: false },
{ id: "B4", price: 7.5, status: false },
],
}
}
movieReducer:
const stateMovie = {
cart: [],
data: data,
};
export const movieReducer = (state = stateMovie, action) => {
case "ADD_TO_CART": {
let cloneCart = [...state.cart];
let index1 = cloneCart.findIndex((sp) => {
return sp.id == action.seatItem.id;
});
let cloneData = [...state.data];
if (index1 == -1) {
cloneCart.push(action.seatItem);
action.seatItem.status = "selecting";
}
state.data = cloneData;
state.cart = cloneCart;
return { ...state };
}
case "REMOVE_FROM_CART": {
let cloneCart = [...state.cart];
cloneCart.splice(action.index, 1);
let cloneData = [...state.data];
state.data = cloneData;
state.cart = cloneCart;
return { ...state };
}
case "CONFIRM_BOOKING": {
let cloneData = [...state.data]
state.data = cloneData;
return { ...state };
}
after i use button "ADD_TO_CART" to select a seat my state.data now become:
data = [{
class: "A",
seat: [
{ id: "A1", price: 7.5, status: false },
{ id: "A2", price: 7.5, status: false },
{ id: "A3", price: 7.5, status: false },
{ id: "A4", price: 7.5, status: false },
],
},
{
class: "B",
seat: [
{ id: "B1", price: 7.5, status: false },
{ id: "B2", price: 7.5, status: 'selecting' },
{ id: "B3", price: 7.5, status: false },
{ id: "B4", price: 7.5, status: false },
],
}
}
in case "CONFIRM_BOOKING" my question is how can i change status in state.data with the seat i add to cart from "selecting" to "true"; i just have idea with 2 findIndex/map loop.