From 6f0655ded094e15e8413cdb6cd535a24cc121850 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Fri, 9 Apr 2021 14:38:41 +0200 Subject: [PATCH] fix: bee api hooks isLoading value now defaults to true (#61) * fix: bee api hooks isLoading value now defaults to true * refactor: removed ping hook --- src/constants.ts | 4 ++++ src/hooks/apiHooks.tsx | 54 ++++++++++-------------------------------- src/services/bee.tsx | 21 +++------------- 3 files changed, 19 insertions(+), 60 deletions(-) create mode 100644 src/constants.ts diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..5244117 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,4 @@ +// These values can for now be constants because their change in the app reloads the page +export const apiHost = sessionStorage.getItem('api_host') || process.env.REACT_APP_BEE_HOST || 'http://localhost:1633' +export const debugApiHost = + sessionStorage.getItem('debug_api_host') || process.env.REACT_APP_BEE_DEBUG_HOST || 'http://localhost:1635' diff --git a/src/hooks/apiHooks.tsx b/src/hooks/apiHooks.tsx index db67e6d..d6adb4e 100644 --- a/src/hooks/apiHooks.tsx +++ b/src/hooks/apiHooks.tsx @@ -11,7 +11,6 @@ import { Health, Peer, Topology, - PingResponse, LastChequesForPeerResponse, } from '@ethersphere/bee-js' @@ -24,7 +23,7 @@ export interface HealthHook { } export const useApiHealth = (): HealthHook => { const [health, setHealth] = useState(false) - const [isLoadingHealth, setLoading] = useState(false) + const [isLoadingHealth, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -53,7 +52,7 @@ export interface DebugHealthHook { export const useDebugApiHealth = (): DebugHealthHook => { const [nodeHealth, setNodeHealth] = useState(null) - const [isLoadingNodeHealth, setLoading] = useState(false) + const [isLoadingNodeHealth, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -82,7 +81,7 @@ export interface NodeAddressesHook { export const useApiNodeAddresses = (): NodeAddressesHook => { const [nodeAddresses, setNodeAddresses] = useState(null) - const [isLoadingNodeAddresses, setLoading] = useState(false) + const [isLoadingNodeAddresses, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -111,7 +110,7 @@ export interface NodeTopologyHook { export const useApiNodeTopology = (): NodeTopologyHook => { const [topology, setNodeTopology] = useState(null) - const [isLoading, setLoading] = useState(false) + const [isLoading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -139,7 +138,7 @@ export interface ChequebookAddressHook { export const useApiChequebookAddress = (): ChequebookAddressHook => { const [chequebookAddress, setChequebookAddress] = useState(null) - const [isLoadingChequebookAddress, setLoading] = useState(false) + const [isLoadingChequebookAddress, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -168,7 +167,7 @@ export interface NodePeersHook { export const useApiNodePeers = (): NodePeersHook => { const [peers, setPeers] = useState(null) - const [isLoading, setLoading] = useState(false) + const [isLoading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -197,7 +196,7 @@ export interface ChequebookBalanceHook { export const useApiChequebookBalance = (): ChequebookBalanceHook => { const [chequebookBalance, setChequebookBalance] = useState(null) - const [isLoadingChequebookBalance, setLoading] = useState(false) + const [isLoadingChequebookBalance, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -226,7 +225,7 @@ export interface PeerBalanceHook { export const useApiPeerBalances = (): PeerBalanceHook => { const [peerBalances, setPeerBalances] = useState(null) - const [isLoadingPeerBalances, setLoading] = useState(false) + const [isLoadingPeerBalances, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -255,7 +254,7 @@ export interface PeerChequesHook { export const useApiPeerCheques = (): PeerChequesHook => { const [peerCheques, setPeerCheques] = useState(null) - const [isLoadingPeerCheques, setLoading] = useState(false) + const [isLoadingPeerCheques, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -284,7 +283,7 @@ export interface PeerLastChequesHook { export const useApiPeerLastCheque = (peerId: string): PeerLastChequesHook => { const [peerCheque, setPeerCheque] = useState(null) - const [isLoadingPeerCheque, setLoading] = useState(false) + const [isLoadingPeerCheque, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -313,7 +312,7 @@ export interface SettlementsHook { export const useApiSettlements = (): SettlementsHook => { const [settlements, setSettlements] = useState(null) - const [isLoadingSettlements, setLoading] = useState(false) + const [isLoadingSettlements, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { @@ -334,35 +333,6 @@ export const useApiSettlements = (): SettlementsHook => { return { settlements, isLoadingSettlements, error } } -export interface PingPeerHook { - peerRTP: PingResponse | null - isPingingPeer: boolean - error: Error | null -} - -export const useApiPingPeer = (peerId: string): PingPeerHook => { - const [peerRTP, setPeerRTP] = useState(null) - const [isPingingPeer, setPingingPeer] = useState(false) - const [error, setError] = useState(null) - - useEffect(() => { - setPingingPeer(true) - beeDebugApi.connectivity - .ping(peerId) - .then(res => { - setPeerRTP(res) - }) - .catch(error => { - setError(error) - }) - .finally(() => { - setPingingPeer(false) - }) - }, [peerId]) - - return { peerRTP, isPingingPeer, error } -} - export interface PeerLastCashoutHook { peerCashout: LastCashoutActionResponse | null isLoadingPeerCashout: boolean @@ -371,7 +341,7 @@ export interface PeerLastCashoutHook { export const useApiPeerLastCashout = (peerId: string): PeerLastCashoutHook => { const [peerCashout, setPeerCashout] = useState(null) - const [isLoadingPeerCashout, setLoading] = useState(false) + const [isLoadingPeerCashout, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { diff --git a/src/services/bee.tsx b/src/services/bee.tsx index 63a3e10..af11e16 100644 --- a/src/services/bee.tsx +++ b/src/services/bee.tsx @@ -20,26 +20,11 @@ import { Topology, WithdrawTokensResponse, } from '@ethersphere/bee-js' +import { apiHost, debugApiHost } from '../constants' -const beeJSClient = () => { - let apiHost = process.env.REACT_APP_BEE_HOST || 'http://localhost:1633' +const beeJSClient = () => new Bee(apiHost) - if (sessionStorage.getItem('api_host')) { - apiHost = String(sessionStorage.getItem('api_host')) - } - - return new Bee(apiHost) -} - -const beeJSDebugClient = () => { - let debugApiHost = process.env.REACT_APP_BEE_DEBUG_HOST || 'http://localhost:1635' - - if (sessionStorage.getItem('debug_api_host')) { - debugApiHost = String(sessionStorage.getItem('debug_api_host')) - } - - return new BeeDebug(debugApiHost) -} +const beeJSDebugClient = () => new BeeDebug(debugApiHost) export const beeApi = { status: {