I wonder where is a good place to transform a response entity(ies) before it is set in my Redux state.
Example
- I have a
chat_messageentity - It has a
readboolean property sent by the server - I need to compute a new
unreadboolean property =!message.read && message.user_id !== currentUser.id
Questions
- Do I compute this new attribute in a
getChatMessagesselector (reselect)? - Do I compute this new attribute before I set the normalized response in my state? – Hence my question "Transform"
- Do I just calculate it in my
Component, but this (simple) logic is not shared and duplicated all over the place... - Do I send the
unreadattribute from the server...
Notes
unreadattribute example is a simplified example.- I don't like solution 1., cause you need to share this logic between selectors anyway. So you might have a
isMessageUnreadhelper function shared bygetLastChatMessage,getChatMessagesselectors. I also don't like selectors doing too much logic. - I would lean towards solution 2. New
unreadattribute is computed only when receiving response. - Solution 3. is my current lazy solution (with experiments here and there but nothing conclusive really)
- I don't like solution 4., these attributes are more UI-related than backend-related.
- If using solution 2., I feel like
reducersis not a good place to do this tranformations. Might be easy for simple entities, but what about "heavy" transformation (iterating over collections, checking relationships, etc) ? I would prefer to put that new logic outside of: selectors, reducers, and entities. Kinda like a normalizr "plugin" / interceptor / transformer / post-processor...
Normalizr provides the
processStrategyoption: