MrWolfZ / ngrx-forms

Enhance your forms in Angular applications with the power of ngrx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array actions not working from state root

mme-private opened this issue · comments

Describe the bug

Using MoveArrayControlAction, SwapArrayControlAction actions from form root state with formStateReducer doesn't work.

To Reproduce

const initialState = { aaa: 'AAA', bbb: [0, 1, 2] };
let formState = createFormGroupState<{ aaa: string, bbb: number[] }>('testForm', initialState);
console.log(formState.value.bbb) // OK got: 0, 1, 2

const addAction = new AddArrayControlAction<number>(formState.controls.bbb.id, 3);
formState = formStateReducer(formState, addAction);
console.log(formState.value.bbb) // OK got: 0, 1, 2, 3

const moveAction = new MoveArrayControlAction(formState.controls.bbb.id, 1, 2);
formState = formStateReducer(formState, moveAction);
console.log(formState.value.bbb) // NOK expected:  0, 2, 1, 3 got: 0, 1, 2, 3
// works using form array reducer inside got: 0, 2, 1, 3
console.log(formArrayReducer(formState.controls.bbb, moveAction).value) // got: 0, 2, 1, 3

const swapAction = new SwapArrayControlAction(formState.controls.bbb.id, 0, 3);
formState = formStateReducer(formState, swapAction); // NOK expected 3, 2, 1, 0, still: 0, 1, 2, 3
console.log(formState.value.bbb)
// works using form array reducer inside got 3, 1, 2, 0
console.log(formArrayReducer(formState.controls.bbb, swapAction).value)

Expected behavior
Using array actions with formStateReducer works

Library version:
6.3.4

Additional context
Probably bug is located in: ngrx-forms/src/group/reducer.ts

Where it checks for two array actions

...
switch (action.type) {
    case FocusAction.TYPE:
    case UnfocusAction.TYPE:
    case AddArrayControlAction.TYPE:
    case RemoveArrayControlAction.TYPE:
      return childReducer(state, action);
...

If we add here MoveArrayControlAction and SwapArrayControlAction too, everything works.

Maybe there is some deeper abstraction problem here, why does group reducer need to know which ones are array actions.

Thank you for the bug report and apologies for the late response. I just published version 6.3.5 which contains the fix for this bug.