更新EntityAdapter内部的数组

时间:2019-06-01 07:25:38

标签: angular ngrx

我有以下EntityAdapter

export interface IDroneRoute {
  assignedDroneId?: number;
  routeId: string;
  rerouting?: boolean;
  waypoints: Waypoint[];
}

export const featureAdapter: EntityAdapter<IDroneRoute> = createEntityAdapter<IDroneRoute>({
  selectId: model => model.routeId,
});

我希望有一个添加,删除和删除数组中的航点的动作

const ADD_ROUTE_POINT = (state: State, action: any) => {
  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: {// add waypoint},
  }, state);
};

如何在更改内访问项目的当前数组,以便可以对其进行更新?

1 个答案:

答案 0 :(得分:0)

changes对应于您要更新的实体(IDroneRoute),因此在return featureAdapter.updateOne({之前,您可以构建更新的对象。

const REMOVE_ROUTE_POINT = (state: State, action: any) => {
  const droneRoute = {...state.entities[action.payload.routeId]}
  droneRoute.waypoints = droneRoute.waypoints.filter(
    wp => wp.id !== action.payload.waypoint.id); 

  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: droneRoute,
  }, state);
};

const ADD_ROUTE_POINT = (state: State, action: any) => {
  const droneRoute = {...state.entities[action.payload.routeId]};
  droneRoute.waypoints = [...droneRoute.waypoints, ...action.payload.waypoint];

  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: droneRoute,
  }, state);
};