Search code examples
angularngrximmer.js

Modify state objects on selector


How can I return the tree list from a selector, derived from the flat list in the store?

In my nrgx store I have a flat list of objects, using a ngrx/selector I'm converting that flat list into a tree list. I've just implemented immer which is now freezing the store (which is good), however, the selector is now throwing ERROR TypeError: Cannot assign to read-only property 'children' of object '[object Object]' because the store is frozen. Maybe I'm approaching this the wrong way.

I've tried cloning the array but the read-only property seems to be cloned as well. I've also tried cloning the child item.

    // ngrx/selector
    export const getAllActionScriptItems = createSelector(featureSelector, state => {
      if (state.items && state.items.length > 0) {
        // Tired cloning the head item.
        const headItem = state.items.find(i => i.isHeader) ;
        const sortedActionScripts = [SortActions(state.items, headItem.id)];

        return sortedActionScripts;
      }
    });


    function SortActions(data: IActionScript[], startingId: string, noActions: boolean = false): IActionScript {


      // Clone the array
      const clonedData = [...data];

      // Tired cloning the top level item
      const topLevelItem = { ...clonedData.find(item => item.id === startingId) };


      const children = GetChildren(clonedData, topLevelItem.id);

      GetChildrensChildren(clonedData, children);

      topLevelItem.children = children;



      return topLevelItem;
    }


    function GetChildren(data: IActionScript[], parentId: string) {

      // return data.filter(item => item.parentId === parentId).sort((a, b) => a.eventPosition - b.eventPosition);
      // Tried cloning the child items
      return [...data.filter(item => item.parentId === parentId).sort((a, b) => a.eventPosition - b.eventPosition)];
    }

    function GetChildrensChildren(data: IActionScript[], children: IActionScript[]) {
      children.forEach(child => {

        // ERROR TypeError: Cannot assign to read only property 'children' of object '[object Object]
        child.children = GetChildren(data, child.id);
        if (child.children && child.children.length > 0) {
          GetChildrensChildren(data, child.children);
        }
      });
    }

Solution

  • I've solved it, by using immer again, I've produced a new state at the createSelector function, after reading the documentation, produce removes the read-only properties.

    The TypeScript typings automatically remove readonly modifiers from your draft types and return a value that matches your original type. - Here

    export const getAllActionScriptItems = createSelector(featureSelector, state => {
    
      if (state.items && state.items.length > 0) {
        const newState = produce(state, draft => {
    
          const headItem = draft.items.find(i => i.isHeader);
          const sortedActionScripts = [SortActions(draft.items, headItem.id)];
          draft.items = sortedActionScripts;
        });
    
        return newState.items.find(i => i.isHeader);
      }
    });