refactor: use fetch rather than custom implementation

This commit is contained in:
Dexter 2022-05-25 09:37:49 +01:00
parent 2f1d0cf224
commit 82d62ce426

View File

@ -1,3 +1,4 @@
import { useFetch } from '@vegaprotocol/react-helpers';
import type { Networks, Tranche } from '@vegaprotocol/smart-contracts-sdk'; import type { Networks, Tranche } from '@vegaprotocol/smart-contracts-sdk';
import React from 'react'; import React from 'react';
import { APP_ENV } from '../config'; import { APP_ENV } from '../config';
@ -15,20 +16,12 @@ const TRANCHES_URLS: { [N in Networks]: string } = {
export function useTranches() { export function useTranches() {
const [tranches, setTranches] = React.useState<Tranche[] | null>(null); const [tranches, setTranches] = React.useState<Tranche[] | null>(null);
const [error, setError] = React.useState<Error | null>(null);
const [loading, setLoading] = React.useState<boolean>(false);
const url = React.useMemo(() => TRANCHES_URLS[APP_ENV], []); const url = React.useMemo(() => TRANCHES_URLS[APP_ENV], []);
React.useEffect(() => { const {
const run = async () => { state: { data, loading, error },
try { } = useFetch<Tranche[] | null>(url);
setLoading(true); const processedTrances = data
const res = await fetch(url); ?.map((t) => ({
if (!res.ok) {
throw new Error(res.statusText);
}
const tranchesJson = await res.json();
const processedTrances = tranchesJson
.map((t: Tranche) => ({
...t, ...t,
tranche_start: new Date(t.tranche_start), tranche_start: new Date(t.tranche_start),
tranche_end: new Date(t.tranche_end), tranche_end: new Date(t.tranche_end),
@ -61,15 +54,7 @@ export function useTranches() {
})), })),
})) }))
.sort((a: Tranche, b: Tranche) => a.tranche_id - b.tranche_id); .sort((a: Tranche, b: Tranche) => a.tranche_id - b.tranche_id);
setTranches(processedTrances); setTranches(processedTrances ? processedTrances : null);
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
};
run();
}, [setTranches, url]);
return { return {
tranches, tranches,