I have somewhat the following Application State tree:
AppState
{
MyPage1 - MyList - MyItem[]
MyPage2 - MySubPage - MyList - MyItem[]
MyPage3 - MyItem //can be stand-alone as well
}
So, I have a list of items on a page. I load this list by dispatching an action to the MyList reducer. Once items are loaded, they are passed as an input to the MyListComponent, which accordingly creates MyItemComponents with the corresponding inputs. MyItemComponent can be stand-alone as well. So, it's not always dependent on the MyList.
The thing is that each of the MyItems has its own actions (e.g. can be liked or edited). And I'm currently stuck where and what action should I dispatch in that case. How do I identify which item was updated and which list should I update.
What would be the action flow when user updates one of the items in a list?
And also - would the state change of MyList be detected if I change one of the MyItem directly?
Scenario I might think of is (while having all appropriate actions on MyItem itself) to add extra actions to the MyList - e.g.:
updateItem(payload:
{
updateType, //e.g. like, edit, delete
data, //data needed to make an update
itemId //index in the MyItem array
})
Then, somehow I fire another action from MyList to the specified MyItem from the array, item fires appropriate action to update its own state (updated via @Effect) If an update was successful, a new action is fired to update the list.
To be honest, it all seems pretty verbose and vague. Not even sure how it would be implemented in practice. Really need suggestion from someone with experience.
p.s. I should mention that all the data is fetched and sent back from/to the REST api.
Edit: If following the approach from Comprehensive Introduction to @ngrx/store, the app would have tones of duplicate actions (each container - MyList - would have all the actions from MyItem repeated). Would there be a more suitable approach for my case?
The way to do it is to have a sub reducer which handles a sub part of the state tree.
In the case where you need to send actions to a single item in the array you pass along all of those actions to the sub reducer that handles a single item.
Here's an example of a news item, that has comments. I hope it clears things up a bit.
And to see how to call these here is the news.actions.