import { useCallback , useMemo } from "react" ; import { useDispatch } from "react-redux" ; import { API } from "../plugins" ; import { useSelector } from "../store" ; import { pushLoader , popLoader , setData , setCache } from "../store/actions" ; export function useApi ( id ) { const dispatch = useDispatch ( ) ; const cache = useSelector ( ( state ) => state . cache [ id ] ) ; const boundSetCache = useCallback ( ( cache ) => dispatch ( setCache ( id , cache ) ) , [ dispatch , id ] ) ; const data = useSelector ( ( state ) => state . data . data [ id ] ) ; const boundSetData = useCallback ( ( data ) => dispatch ( setData ( id , data ) ) , [ dispatch , id ] ) ; const loader = useMemo ( ( ) => ( { push : ( ) => dispatch ( pushLoader ( ) ) , pop : ( ) => dispatch ( popLoader ( ) ) , } ) , [ dispatch ] ) ; return { cache , data , loader , setCache : boundSetCache , setData : boundSetData , } ; }