All files / src/hooks useFetchMarketCode.ts

90.9% Statements 20/22
33.33% Branches 1/3
80% Functions 4/5
90.47% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  1x                   5x   5x 5x   5x 2x 2x   2x 1x     1x 1x 1x 1x       1x   2x       5x 2x         5x     1x  
import {ImarketCodes} from '../interfaces';
import {useState, useEffect} from 'react';
 
/**
 * useFetchMarketCode hook is used to fetch market codes from upbit api
 * @returns Object with the market codes and a loading state.
 */
function useFetchMarketCode(option = {debug: false}): {
  isLoading: boolean;
  marketCodes: ImarketCodes[];
} {
  const REST_API_URL = 'https://api.upbit.com/v1/market/all?isDetails=false';
 
  const [isLoading, setIsLoading] = useState<boolean>(true);
  const [marketCodes, setMarketCodes] = useState<ImarketCodes[]>([]);
 
  const fetchMarketCodes = async () => {
    try {
      const response = await fetch(REST_API_URL);
 
      if (!response.ok) {
        throw new Error('Failed to fetch market codes');
      }
 
      const json = await response.text();
      const result = JSON.parse(json) as ImarketCodes[];
      setMarketCodes(result);
      Iif (option.debug) {
        console.log('Market codes fetched:', result);
      }
    } catch (error) {
      console.error('Error fetching market codes:', error);
    } finally {
      setIsLoading(false);
    }
  };
 
  useEffect(() => {
    fetchMarketCodes().catch(error => {
      console.error('Error fetching market codes:', error);
    });
  }, []);
 
  return {isLoading, marketCodes};
}
 
export default useFetchMarketCode;