Created
April 7, 2017 03:50
-
-
Save rhysforyou/95d5e48a6fb73ef088385c7902a109b0 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
/* @flow */ | |
import { connect } from 'react-redux' | |
import { createSelector } from 'reselect' | |
import { searchPackagesRequested } from '../actions/packages' | |
import PackageList from '../components/PackageList' | |
import type { Dispatch } from 'redux' | |
import type { Selector as BaseSelector } from 'reselect' | |
import type { Action, Package } from '../actions/types' | |
import type { State, PackagesState, Search } from '../reducers/types' | |
import type { Props as ChildProps } from '../components/PackageList' | |
type StateProps = { packages: Array<Package> } | |
type DispatchProps = { onRefresh: () => Action } | |
type OwnProps = { query: string } | |
type Props = ChildProps & OwnProps | |
type Selector<Result> = BaseSelector<State, OwnProps, Result>; | |
const reactSearchSelector: Selector<Search> = (state, { query }) => { | |
return state.searches[query] || { query: query, status: 'loading' } | |
} | |
const packagesSelector: Selector<PackagesState> = state => state.entities.packages | |
const reactSearchPackageIdsSelector: Selector<Array<string>> = createSelector( | |
reactSearchSelector, | |
search => (search.status && search.status === 'loaded') ? search.packages : [] | |
) | |
const reactSearchPackagesSelector: Selector<Array<Package>> = createSelector( | |
reactSearchPackageIdsSelector, | |
packagesSelector, | |
(ids, packages) => ids.map(id => packages[id]) | |
) | |
const mapStateToProps = (state: State, props: OwnProps): StateProps => ({ | |
packages: reactSearchPackagesSelector(state, props) | |
}) | |
const mapDispatchToProps = (dispatch: Dispatch<Action>, props: OwnProps): DispatchProps => ({ | |
onRefresh: () => dispatch(searchPackagesRequested(props.query)) | |
}) | |
const SearchPackagesList: ReactClass<Props> = connect(mapStateToProps, mapDispatchToProps)(PackageList) | |
export default SearchPackagesList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment