I would like to initialize my EntityAdapter from an action an use it in the same action
I have an Order object with lines in my state. I want to use an entityAdapter for the lines but if the user don't start an order, Order is null
The order is not existing while the user don't press the add button
This is my code
Reducer action `
export const addProductToCurrentOrderAction = (
state: appState,
action: PayloadAction<{ quantity: number; product: Product }>,
) => {
const { quantity, product } = action.payload;
// if order doen't exist, I create a new one with lines = orderLinesAdapter.getInitialState()
if (state.currentOrder === null) {
state.currentOrder = createOrderDoc(state.userState.user);
}
// check if the line already exist
let orderLineTest = state.currentOrder.lines.entities[product.ean];
if (!orderLineTest) {
// if not I create a new orderLine
orderLineTest = createOrderLineFromProduct(product, state.configState.config);
}
// set line quantity and line total
orderLineTest.quantity = quantity;
// add or update line
orderLinesAdapter.addOne(state.currentOrder.lines, orderLineTest);
};
`
This only work if the order is already created (on first run the line is not added to entityAdapter)
I'm supposed there is a tricky problem with the entityAdapter update outside the action
Maybe the way to do this is not really correct for redux-toolkit?