Skip to content

Instantly share code, notes, and snippets.

@Swiip
Created March 19, 2019 16:01
Show Gist options
  • Save Swiip/993f0e561b8464b8db295d9f9044d876 to your computer and use it in GitHub Desktop.
Save Swiip/993f0e561b8464b8db295d9f9044d876 to your computer and use it in GitHub Desktop.
logic/hooks/useAgencies.js
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