I'm rewriting a Ionic project to work with ngrx because it is growing a lot and I need to maintain a centralized state.
I have implemented it with ngrx and using normalized states with the help of Normalizr.
Now I have a doubt on how to pass to dumb component a populated object:
Suppose I have two interfaces:
interface Conversation {
date: Date,
lastMessage: string //this is the id of a message entity coming from normalizr
}
and
interface Message {
content: string
}
Now when I'm trying to get all conversations to pass to the dumb component I'm using a selector like this:
getConversations() {
//select all the conversations to have a shape of Conversation[]
let conversations$ = this.store.select(state => Object.keys(state.entities.conversations).map( (id:string) => state.entities.conversations[id] ));
//select all the messages in a shape { [id:string] : Message }
let messages$ = this.store.select(state => state.entities.messages);
return Observable.combineLatest(
conversations$,
messages$,
(conversations, messages) => {
return conversations.map(conversation => {
return { ...conversation, lastMessage: messages[conversation.lastMessage] }
});
}
)
}
But the Observable I'm returning is not an array of Conversation[] because
return { ...conversation, lastMessage: messages[conversation.lastMessage] }
is putting inside 'lastMessage' an object of type Message instead of a String.
I tried to use interfaces with the object type instead of strings
interface Conversation {
date: Date,
lastMessage: Message
}
But then I cannot use selector like
this.store.select(state => state.entities.messages[conversation.lastMessage]
because it is not a string anymore.
How can I achieve this?
Thank you