The Setup
In my backend, I have a Musician model which can have many Instruments or Songs. The rest API has separate routes to get all Musicians, get all the Instruments for a musician, get all the Songs for a musician, etc.
My root state looks like this:
musicians: [],
instruments: [],
songs: []
I then have 3 Redux Toolkit "slices" that manage those respective parts of state. For example, instrumentsSlice has an async thunk called fetchInstruments that calls the rest API to fetch instruments for a specific musician, then is fulfilled in extraReducers:
extraReducers: {
[fetchInstruments.fulfilled]: (state, action) => {
return action.payload.map(instrument => {
instrument.id = nextInstrumentId++;
return instrument;
});
},
....
}
On the page for a specific musician: fetchInstruments and fetchSongs are dispatched, which call their respective APIs that update the state. The Selectors I use then filter only the Instruments or Songs that match the particular Musician ID for that page.
The Problem
Up to this point, I've been able to get away with using these 3 separate slices. Now, I want to create a dashboard where I can view summary information for all Musicians. I realized that it wouldn't make sense to call 3 XHR requests per every Musician listed, so I created a single new rest route that returns the information nested for all musicians, fetchAllMusicianData, i.e.:
musicians: [
{
name:,
instruments: [],
songs: []
},
...
]
The Question
Somehow, I need to get this nested Musician/Instrument/Song data into my state tree. However, ideally, I don't want to duplicate logic that exists in the fetchInstruments or fetchSongs reducers (such as creating local ids in my extraReducers code above).
Is there some way that when the fetchAllMusicianData call is fulfilled, I can call each of those other slices to run their methods like fetchInstruments.fulfilled? Is there some way I need to abstract updating this information? Or, should I be organizing my state in a different way?
Thanks for the help in advance!