ngrx处理对象中的嵌套数组

fas*_*ise 12 javascript functional-programming redux ngrx

我正在学习redux模式并使用角度为2的ngrx.我正在创建一个具有以下形状的示例博客站点.

export interface BlogContent {
  id: string;
  header: string;
  tags: string[];
  title: string;
  actualContent: ActualContent[];
}
Run Code Online (Sandbox Code Playgroud)

我的减速机和动作如下:

import { ActionReducer, Action } from '@ngrx/store';
import * as _ from 'lodash';
export interface ActualContent {
  id: string;
  type: string;
  data: string;
}

export interface BlogContent {
  id: string;
  header: string;
  tags: string[];
  title: string;
  actualContent: ActualContent[];
}

export const initialState: BlogContent = {
  id: '',
  header: '',
  tags: [],
  title: '',
  actualContent: [],
};

export const ADD_OPERATION = 'ADD_OPERATION';
export const REMOVE_OPERATION = 'REMOVE_OPERATION';
export const RESET_OPERATION = 'RESET_OPERATION';
export const ADD_IMAGE_ID = 'ADD_IMAGE_ID';
export const ADD_FULL_BLOG = 'ADD_FULL_BLOG';
export const ADD_BLOG_CONTENT_OPERATION = 'ADD_BLOG_CONTENT_OPERATION';
export const ADD_BLOG_TAG_OPERATION = 'ADD_BLOG_TAG_OPERATION';

export const blogContent: ActionReducer<BlogContent> = (state: BlogContent= initialState, action: Action ) => {
    switch (action.type) {
      case  ADD_OPERATION :
        return Object.assign({}, state, action.payload );
      case  ADD_BLOG_CONTENT_OPERATION :
        return Object.assign({}, state, { actualContent: [...state.actualContent, action.payload]});
        case  ADD_BLOG_TAG_OPERATION :
        return Object.assign({}, state, { tags: [...state.tags, action.payload]});
      case REMOVE_OPERATION :
        return Object.assign({}, state, { actualContent: state.actualContent.filter((blog) => blog.id !== action.payload.id) });
      case ADD_IMAGE_ID : {
        let index = _.findIndex(state.actualContent, {id: action.payload.id});
        console.log(index);
        if ( index >= 0 ) {
          return  Object.assign({}, state, {
            actualContent :  [
              ...state.actualContent.slice(0, index),
              action.payload,
              ...state.actualContent.slice(index + 1)
            ]
          });
        }
        return state;
      }
      default :
        return state;
    }
};
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我不确定它是否正确的方法或我应该以某种方式将ActualContent分离到自己的reducer和动作,然后合并它们.对不起,如果这篇文章不属于这里,你可以指导我应该把这篇文章放在哪里,我会从这里删除它.提前致谢.

PS我做了一些研究,但找不到任何具有复杂嵌套对象的文章,以便我可以参考.请添加ngrx或相关主题的任何有用的博客链接,这可以帮助我.

max*_*992 13

而不是拥有嵌套结构

export interface BlogContent {
  id: string;
  header: string;
  tags: string[];
  title: string;
  actualContent: ActualContent[]; <------ NESTED
}
Run Code Online (Sandbox Code Playgroud)

你应该有一个标准化的状态.

例如,你应该有这样的东西:

// this should be into your store
export interface BlogContents {
  byId: { [key: string]: BlogContent };
  allIds: string[];
}

// this is made to type the objects you'll find in the byId
export interface BlogContent {
  id: string;
  // ...
  actualContentIds: string[];
}

// ----------------------------------------------------------

// this should be into your store
export interface ActualContents {
  byId: { [key: string]: ActualContent };
  allIds: string[];
}

export interface ActualContent {
  id: string;
  // ...
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您尝试填充您的商店,它看起来像这样:

const blogContentsState: BlogContents = {
  byId: {
    blogContentId0: {
      id: 'idBlogContent0',
      // ...
      actualContentIds: ['actualContentId0', 'actualContentId1', 'actualContentId2']
    }
  },
  allIds: ['blogContentId0']
};

const actualContentState: ActualContents = {
  byId: {
    actualContentId0: {
      id: 'actualContentId0',
      // ...
    },
    actualContentId1: {
      id: 'actualContentId1',
      // ...
    },
    actualContentId2: {
      id: 'actualContentId2',
      // ...
    }
  },
  allIds: ['actualContentId0', 'actualContentId1', 'actualContentId2']
};
Run Code Online (Sandbox Code Playgroud)

在您的逻辑或视图中(例如使用Angular),您需要嵌套结构,以便可以遍历数组,因此,您不希望迭代ID字符串数组.相反,你会喜欢actualContent: ActualContent[];.

为此,你创建了一个selector.每次商店更改时,您的选择器都会启动并生成原始数据的新"视图".

// assuming that you can blogContentsState and actualContentsState from your store
const getBlogContents = (blogContentsState, actualContentsState) =>
  blogContentsState
    .allIds
    .map(blogContentId => ({
      ...blogContentsState.byId[blogContentId],

      actualContent: blogContentsState
        .byId[blogContentId]
        .actualContentIds
        .map(actualContentId => actualContentsState.byId[actualContentId])
    }));
Run Code Online (Sandbox Code Playgroud)

我知道在开始时可以处理很多,我邀请您阅读有关选择器和规范化状态的官方文档:http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html

当你正在学习ngrx时,你可能想看看我制作的名为Pizza-Sync的小项目.代码源在Github上.这是一个项目,我已经做了类似的演示:).(您还应该安装ReduxDevTools应用程序以查看商店的情况).

如果你感兴趣,我只在Redux和Pizza-Sync上做了一个小视频:https://youtu.be/I28m9lwp15Y