import isEqual from 'lodash/isEqual'; import { useCallback, useEffect, useRef, useState } from 'react'; import { ErrorResult, MatchPathPattern, OperationParams, PathFor, ResultFor, Serialized } from '../../definitions/rest/helpers'; import sdk from '../services/sdk'; export const useEndpointData = >( endpoint: TPath, params: void extends OperationParams<'GET', MatchPathPattern> ? void : Serialized>> = undefined as void extends OperationParams< 'GET', MatchPathPattern > ? void : Serialized>> ): { result: Serialized>> | undefined; loading: boolean; reload: Function; error: ErrorResult | undefined; } => { const [loading, setLoading] = useState(true); const [result, setResult] = useState>> | undefined>(); const [error, setError] = useState(); const paramsRef = useRef(params); if (!isEqual(paramsRef.current, params)) { paramsRef.current = params; } const fetchData = useCallback(() => { if (!endpoint) return; setLoading(true); sdk .get(endpoint, params) .then(e => { setLoading(false); if (e.success) { setResult(e); } else { setError(e as ErrorResult); } }) .catch((e: ErrorResult) => { setLoading(false); setError(e); }); }, [paramsRef.current]); useEffect(() => { fetchData(); }, [fetchData]); return { result, loading, reload: fetchData, error }; };