Last active
September 24, 2015 20:02
-
-
Save gregziegan/0e5308c2ef154fa9b258 to your computer and use it in GitHub Desktop.
The problem with REST endpoints and state -- reading a simple hierarchical novel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Reader extends React.Component { | |
| componentWillReceiveProps (nextProps) { | |
| const { | |
| entities: { novels, chapters, words }, | |
| pagination: { | |
| novelsByContributor, chaptersByNovel, wordsByChapter | |
| } | |
| } = nextProps; | |
| const isFetchingChapters = chaptersByNovel[NOVEL_ID] | |
| && chaptersByNovel[NOVEL_ID].isFetching; | |
| const requestChapters = !isFetchingChapters | |
| && isEmpty(chapters) | |
| && !isEmpty(novels); | |
| const isFetchingText = wordsByChapter[CHAPTER_ID] | |
| && wordsByChapter[CHAPTER_ID].isFetching; | |
| const requestChapterText = !isFetchingText | |
| && isEmpty(words) | |
| && !isEmpty(chapters); | |
| if (requestChapters) { | |
| this.props.loadChapters(novels[NOVEL_ID]); | |
| } | |
| if (requestChapterText) { | |
| this.props.loadChapterText(chapters[CHAPTER_ID]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment