Created
April 27, 2018 05:51
-
-
Save agoldis/5e3d602c7ac6d60ab82685b43d9d1632 to your computer and use it in GitHub Desktop.
redux - actual derive state
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
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