I am creating an app with React/Redux and I am new to using both React and Redux. I'm stuck trying to figure out how to structure the redux store. Here is a brief description of what I am trying to achieve, generalized using the book example as in the redux createEntityAdapter docs
I have a page with a list of books. The user can click one or many books and for each book a list is displayed of the chapters. New or missing chapters can be added anytime as they become available on the server. The added chapters need to be added to the list in the correct position.
- Book 1
- Chapter 1
- Chapter 2
- Chapter 3
- Chapter 6
- Book 2
- Book 3
The book list is a component, and the chapters list is another component, but since there are many books, there will be many instances of the chapter list component. Each component has its own slice.
The store looks like:
bookList : [ Book1, Book2, Book3, ...]
ChapterList: {
Book1 : {
chapters: [
{
Chapter : 'Chapter1',
info: 'Chapter info',
other: 'more stuff'
},
{
Chapter : 'Chapter2',
info: 'Chapter info',
other: 'more stuff'
},
{...}
],
bookInfo: 'info about book'
},
Book2 : { ... },
Book3 : { ... }
}
The problem is that chapter info is being streamed and thus is updated continuously and missing chapters can arrive at any time. As a result I would need to sort the chapters array each time new data arrives, whereas the array is not very long, in the order of 50 elements, new data arrives a few times per second.
I intended to use createEntityAdapter to normalize the chapters array, but I don't see how this is possible given that it is nested in the Book# property and the Book# property is needed because I'm using multiple instances of the ChapterList component.
const ChapterListAdapter = createEntityAdapter({
// where bookNum needs to Book1, Book2, Book3 etc...
selectId: (chapterList) => chapterList[bookNum].chapters,
sortComparer: (a, b) => a.chapters.localeCompare(b.chapters
})
How do I solve this? Modify the store? Is there a way to flatten chapterList such that chapters is at the top level? Do I write my own normalizing/sorting logic into the the reducer?