Created
March 19, 2019 16:01
-
-
Save Swiip/993f0e561b8464b8db295d9f9044d876 to your computer and use it in GitHub Desktop.
logic/hooks/useAgencies.js
This file contains 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 { useQuery } from "react-apollo-hooks"; | |
import { gql } from "apollo-boost"; | |
import { initAgencies, selectAgency } from "../agencies"; | |
import useRedux from "./useRedux"; | |
import useLoaded from "./useLoaded"; | |
const agencyQuery = gql` | |
query Agencies { ... } | |
`; | |
const useAgencies = () => { | |
const query = useQuery(agencyQuery); | |
const { | |
agencies, | |
selectedAgency, | |
dispatchInitAgencies, | |
dispatchSelectAgency | |
} = useRedux( | |
state => ({ | |
agencies: state.agencies.agencies, | |
selectedAgency: state.agencies.selectedAgency | |
}), | |
dispatch => ({ | |
dispatchInitAgencies: () => dispatch(initAgencies(query.data.agencies)), | |
dispatchSelectAgency: agency => dispatch(selectAgency(agency)) | |
}), | |
[query.data.agencies] | |
); | |
useLoaded(query, () => dispatchInitAgencies()); | |
return { agencies, selectedAgency, selectAgency: dispatchSelectAgency }; | |
}; | |
export default useAgencies; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment