Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

useCurrenciesByMarketcapWithStatus #1963

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/currencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useMarketcapTickers,
currenciesByMarketcap,
useCurrenciesByMarketcap,
useCurrenciesByMarketcapWithStatus,
} from "./sortByMarketcap";
import {
listFiatCurrencies,
Expand Down Expand Up @@ -54,6 +55,7 @@ export {
useMarketcapTickers,
currenciesByMarketcap,
useCurrenciesByMarketcap,
useCurrenciesByMarketcapWithStatus,
listFiatCurrencies,
listCryptoCurrencies,
getFiatCurrencyByTicker,
Expand Down
33 changes: 33 additions & 0 deletions src/currencies/sortByMarketcap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,36 @@ export const useCurrenciesByMarketcap = <C extends Currency>(
const tickers = useMarketcapTickers();
return tickers ? sortByMarketcap(currencies, tickers) : currencies;
};

type CurrenciesByMarketcapResult = {
loading: boolean;
error?: Error;
currencies?: any[];
};

export const useCurrenciesByMarketcapWithStatus = <C extends Currency>(
ThomasLaforge marked this conversation as resolved.
Show resolved Hide resolved
currencies: C[]
): CurrenciesByMarketcapResult => {
const [status, setStatus] = useState<CurrenciesByMarketcapResult>({
loading: false,
});

useEffect(() => {
function getCurrenciesByMarketcap() {
setStatus({ loading: true });
getMarketcapTickers()
.then((tickers) => {
setStatus({
loading: false,
currencies: sortByMarketcap(currencies, tickers),
});
})
.catch((error: Error) => {
setStatus({ loading: false, error, currencies });
});
}
getCurrenciesByMarketcap();
}, []);

return status;
};