Created
March 1, 2016 15:29
-
-
Save mrtnbroder/6ae73cdbb3cb8b3ff255 to your computer and use it in GitHub Desktop.
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
| export const moviesSelector = ({ movies }) => movies.movies |
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 { createSelector } from 'reselect' | |
| import { moviesSelector } from './movies-selectors' | |
| import { subscriptionsOfUserSelector } from './subscription-selectors' | |
| export const getUserSubscriptions = createSelector( | |
| moviesSelector, | |
| subscriptionsOfUserSelector, | |
| (movies, subscriptions) => | |
| subscriptions.map((s) => s.movieId) // create array of movie id's | |
| .filter((movieId) => !~subscriptions.indexOf(+movieId)) // get movies the user is not subscribed to | |
| .map((id) => movies[id]) // get the movies | |
| ) |
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 { createSelector } from 'reselect' | |
| export const subscriptionsSelector = ({ subscriptions }) => subscriptions.subscriptions | |
| export const subscriptionsOfUserSelector = createSelector( | |
| subscriptionsSelector, | |
| (_, { userId }) => +userId, // userId is passed as a prop to some component | |
| (subscriptions, userId) => ({ | |
| subscriptions: Object.keys(subscriptions).map(key => subscriptions[key]).filter(s => s.userId === userId) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment