Skip to content

Instantly share code, notes, and snippets.

@gregziegan
Last active September 24, 2015 20:02
Show Gist options
  • Select an option

  • Save gregziegan/0e5308c2ef154fa9b258 to your computer and use it in GitHub Desktop.

Select an option

Save gregziegan/0e5308c2ef154fa9b258 to your computer and use it in GitHub Desktop.
The problem with REST endpoints and state -- reading a simple hierarchical novel
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