0

So I have a custom entity, which I will add to in the UI:

meeting.ts:

export interface Meeting {
  summary: MeetingSummary;
  agenda: any;
  audience?: Audience[];
  venue?: {
    lat?: number;
    long?: number;
  };
  invitationKey?: string;
  key: string;
  activeProposalId?: string;
  createdAt?: string;
  updatedAt?: string;
  createdBy?: string;
  updatedBy?: string;
  owner?: string;
}

I scaffolded @ngrx/entity using the new creator syntax:

meeting.actions.ts:

export const beginLoadMeetings = createAction(
  '[ Meeting ] Begin Load Meetings',
  props<{ uid: string }>()
);

export const loadMeetings = createAction(
  '[Meeting/API] Load Meetings',
  props<{ meetings: Meeting[] }>()
);

export const beginAddMeeting = createAction(
  '[ Meeting ] Begin Add  New Meeting',
  props<{ meeting: Meeting }>()
);

export const addMeeting = createAction(
  '[ Meeting ] Add Meeting to Store',
  props<{ meeting: Meeting }>()
);

export const upsertMeeting = createAction(
  '[Meeting/API] Upsert Meeting',
  props<{ meeting: Meeting }>()
);

export const addMeetings = createAction(
  '[Meeting/API] Add Meetings',
  props<{ meetings: Meeting[] }>()
);
// ...

And the corresponding reducer

meeting.reducer.ts

export interface MeetingState extends EntityState<Meeting> {
  loading: boolean;
}

export interface State extends fromRoot.State {
  meetings: MeetingState;
}

export const adapter: EntityAdapter<Meeting> = createEntityAdapter<Meeting>(
  {
    selectId: (meeting: Meeting) => (meeting.key)
  }
);

export const initialState: MeetingState = adapter.getInitialState({
  loading: false
});

const meetingReducer = createReducer(
  initialState,
  on(MeetingActions.beginLoadMeetings,
    (state) => ({...state, loading: true})
  ),
  on(MeetingActions.beginAddMeeting,
    (state) => state
  ),
  on(MeetingActions.addMeeting,
    (state, action) => adapter.addOne(action.meeting, state)
  ),
  on(MeetingActions.upsertMeeting,
    (state, action) => adapter.upsertOne(action.meeting, state)
  ),
  //...
);

I will dispatch a beginAddMeeting action, which triggers an effect which stores to the Backend. This happens correctly and the effect also correctly dispatches the addMeeting action with the correct payload, which I can verify in the DevTools. However, the state does not change, and the entity is not getting added to the store, so all my containers fail to note the change.

Curiously enough, when I dispatch beginLoadMeetings, which triggers a similar effect, only gets all entities from the Backend, The store gets correctly updated. In short, addMany works, addOne and upsertOne do nothing to the state. The reducer does not even react when I do the update without the adapter, which leads me to suspect there is something wrong with my config. Spent half a week on fixing this, with no success so I hope I find some help here. Thanks!

thomi
  • 1,603
  • 1
  • 24
  • 31
  • If you can create a reproduction, I'm happy to take a look – timdeschryver Jul 20 '19 at 11:38
  • Sorry, but that's a little difficult. The problem seems to occur in conjunction with firebase: Actions get dispatched correctly, but the new entity will not get added to the store (see above code...). On reload everything is find (but then, I also reload the DB...). – thomi Jul 31 '19 at 16:48
  • Was debugging on this today. I think it may have to do with some error related to form initialisation with `@ViewChild` forms. The dev tools told me that the store somehow hung up because of this error (The form of course was meant to put new stuff in...). Still a bit vague about what caused this, but it seems to work now. – thomi Aug 02 '19 at 17:11
  • @thomi, could you please describe the solution in more detail? I have a similar problem now, also with forms. Single-update methods don't work for me, only when the whole state is cleared and re-populated after that. It looks like a problem with some consumers, but I cannot figure out what exactly. – Fyodor Feb 06 '20 at 08:05

0 Answers0