Skip to content

Instantly share code, notes, and snippets.

@agoldis
Created April 27, 2018 05:51
Show Gist options
  • Save agoldis/5e3d602c7ac6d60ab82685b43d9d1632 to your computer and use it in GitHub Desktop.
Save agoldis/5e3d602c7ac6d60ab82685b43d9d1632 to your computer and use it in GitHub Desktop.
redux - actual derive state
import { compose } from "redux";
const usersSummary = function() {
return this.users.map(user => ({
...user,
booksCount: user.books.length,
commentsCount: this.comments.filter(c => c.user === user.id).length
}));
};
const booksSummary = function() {
return this.books.map(book => ({
...book,
readersCount: this.users.filter(u => u.books.indexOf(book.isbn) > -1)
.length,
commentsCount: this.comments.filter(c => c.book === book.isbn).length
}));
};
const addUsersSummary = state =>
Object.defineProperty(state, "usersSummary", {
get: usersSummary
});
const addBooksSummary = state =>
Object.defineProperty(state, "booksSummary", {
get: booksSummary
});
export const deriveState = compose(addBooksSummary, addUsersSummary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment