Created
April 27, 2018 06:03
-
-
Save agoldis/1d95268e8de82e65f5458badfb7fcd5f to your computer and use it in GitHub Desktop.
redux - full example
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