My goal is to retrieve an object by its Id from the entity state using a reducer. Here is the implementation:
export interface MessageState extends EntityState<Message> {
// additional entities state properties
loaded: boolean;
loading: boolean;
}
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>({
selectId: (msg: Message) => msg.messageId,
});
export const initialState: MessageState = adapter.getInitialState({
// additional entity state properties
loaded: false,
loading: false,
});
export function reducer(state = initialState, action: MessageActionsUnion): MessageState {
switch (action.type) {
case MessageActionTypes.UPSERT_Message: {
return { ...state, loading: true, loaded: false };
}
case MessageActionTypes.UPSERT_Message_SUCCESS: {
return adapter.upsertOne(action.payload.Message,
{
...state, loaded: true, loading: false,
});
}
default: {
return state;
}
}
}
Here is the content of my index.ts file:
export interface State extends fromRoot.AppState {
queueModule: QueueState;
}
export interface QueueState {
msgHeaders: fromMsgHeaders.MessageHeaderState
}
export const reducers: ActionReducerMap<QueueState> = {
msgHeaders: fromMsgHeaders.reducer
};
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
I am working on creating a selector that can fetch an object by passing its Id:
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const {
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
} = adapter.getSelectors(selectMessages);
I have gone through multiple resources, but I haven't found a clear explanation on how to achieve object selection based on Id.