@@ -3,13 +3,15 @@ import * as React from 'react'
33import { View } from 'react-native'
44import FastImage from 'react-native-fast-image'
55
6+ import { useAsyncEffect } from '../../hooks/useAsyncEffect'
67import { formatFiatString } from '../../hooks/useFiatText'
78import { toLocaleDate , toPercentString } from '../../locales/intl'
89import { lstrings } from '../../locales/strings'
910import { getDefaultFiat } from '../../selectors/SettingsSelectors'
10- import { CoinRankingData , CoinRankingDataPercentChange } from '../../types/coinrankTypes'
11+ import { asCoinRankingData , CoinRankingData , CoinRankingDataPercentChange } from '../../types/coinrankTypes'
1112import { useSelector } from '../../types/reactRedux'
1213import { EdgeAppSceneProps } from '../../types/routerTypes'
14+ import { fetchRates } from '../../util/network'
1315import { formatLargeNumberString as formatLargeNumber } from '../../util/utils'
1416import { SwipeChart } from '../charts/SwipeChart'
1517import { EdgeAnim , fadeInLeft } from '../common/EdgeAnim'
@@ -21,7 +23,8 @@ import { COINGECKO_SUPPORTED_FIATS } from './CoinRankingScene'
2123type CoinRankingDataValueType = string | number | CoinRankingDataPercentChange | undefined
2224
2325export interface CoinRankingDetailsParams {
24- coinRankingData : CoinRankingData
26+ assetId ?: string
27+ coinRankingData ?: CoinRankingData
2528 fiatCurrencyCode : string
2629}
2730
@@ -79,9 +82,10 @@ const CoinRankingDetailsSceneComponent = (props: Props) => {
7982 const theme = useTheme ( )
8083 const styles = getStyles ( theme )
8184 const { route, navigation } = props
82- const { coinRankingData, fiatCurrencyCode } = route . params
83- const { currencyCode, currencyName } = coinRankingData
84- const currencyCodeUppercase = currencyCode . toUpperCase ( )
85+ const { assetId, coinRankingData : initCoinRankingData , fiatCurrencyCode } = route . params
86+ const [ coinRankingData , setCoinRankingData ] = React . useState < CoinRankingData | undefined > ( initCoinRankingData )
87+ const { currencyCode, currencyName } = coinRankingData ?? { }
88+ const currencyCodeUppercase = currencyCode ?. toUpperCase ( ) ?? ''
8589
8690 // In case the user changes their default fiat while viewing this scene, we
8791 // want to go back since the parent scene handles fetching data.
@@ -98,9 +102,30 @@ const CoinRankingDetailsSceneComponent = (props: Props) => {
98102 }
99103 } , [ supportedFiat , initFiat , isFocused , navigation ] )
100104
105+ useAsyncEffect (
106+ async ( ) => {
107+ if ( coinRankingData == null ) {
108+ if ( assetId == null ) {
109+ throw new Error ( 'No currencyCode or coinRankingData provided' )
110+ }
111+ const response = await fetchRates ( `v2/coinrankAsset/${ assetId } ?fiatCode=${ supportedFiat } ` )
112+ if ( ! response . ok ) {
113+ const text = await response . text ( )
114+ throw new Error ( `Unable to fetch coin ranking data. ${ text } ` )
115+ }
116+
117+ const json = await response . json ( )
118+ const crData = asCoinRankingData ( json . data )
119+ setCoinRankingData ( crData )
120+ }
121+ } ,
122+ [ currencyCode , supportedFiat ] ,
123+ 'CoinRankingDetailsScene'
124+ )
125+
101126 const imageUrlObject = React . useMemo (
102127 ( ) => ( {
103- uri : coinRankingData . imageUrl ?? ''
128+ uri : coinRankingData ? .imageUrl ?? ''
104129 } ) ,
105130 [ coinRankingData ]
106131 )
@@ -140,19 +165,19 @@ const CoinRankingDetailsSceneComponent = (props: Props) => {
140165 case 'rank' :
141166 return `#${ baseString } `
142167 case 'marketCapChange24h' :
143- extendedString = coinRankingData . marketCapChangePercent24h != null ? ` (${ toPercentString ( coinRankingData . marketCapChangePercent24h / 100 ) } )` : ''
168+ extendedString = coinRankingData ? .marketCapChangePercent24h != null ? ` (${ toPercentString ( coinRankingData . marketCapChangePercent24h / 100 ) } )` : ''
144169 break
145170 case 'allTimeHigh' : {
146171 const fiatString = `${ formatFiatString ( {
147172 fiatAmount : baseString
148173 } ) } ${ supportedFiat } `
149- return coinRankingData . allTimeHighDate != null ? `${ fiatString } - ${ toLocaleDate ( new Date ( coinRankingData . allTimeHighDate ) ) } ` : fiatString
174+ return coinRankingData ? .allTimeHighDate != null ? `${ fiatString } - ${ toLocaleDate ( new Date ( coinRankingData . allTimeHighDate ) ) } ` : fiatString
150175 }
151176 case 'allTimeLow' : {
152177 const fiatString = `${ formatFiatString ( {
153178 fiatAmount : baseString
154179 } ) } ${ supportedFiat } `
155- return coinRankingData . allTimeLowDate != null ? `${ fiatString } - ${ toLocaleDate ( new Date ( coinRankingData . allTimeLowDate ) ) } ` : fiatString
180+ return coinRankingData ? .allTimeLowDate != null ? `${ fiatString } - ${ toLocaleDate ( new Date ( coinRankingData . allTimeLowDate ) ) } ` : fiatString
156181 }
157182 default :
158183 // If no special modifications, just return simple data formatting
@@ -194,17 +219,21 @@ const CoinRankingDetailsSceneComponent = (props: Props) => {
194219
195220 return (
196221 < SceneWrapper hasTabs hasNotifications scroll >
197- < View style = { styles . container } >
198- < EdgeAnim style = { styles . titleContainer } enter = { fadeInLeft } >
199- < FastImage style = { styles . icon } source = { imageUrlObject } />
200- < EdgeText style = { styles . title } > { `${ currencyName } (${ currencyCodeUppercase } )` } </ EdgeText >
201- </ EdgeAnim >
202- < SwipeChart assetId = { coinRankingData . assetId } currencyCode = { currencyCodeUppercase } fiatCurrencyCode = { initFiat } />
203- < View style = { styles . columns } >
204- < View style = { styles . column } > { renderRows ( coinRankingData , COLUMN_LEFT_DATA_KEYS ) } </ View >
205- < View style = { styles . column } > { renderRows ( coinRankingData , COLUMN_RIGHT_DATA_KEYS ) } </ View >
222+ { coinRankingData != null ? (
223+ < View style = { styles . container } >
224+ < EdgeAnim style = { styles . titleContainer } enter = { fadeInLeft } >
225+ < FastImage style = { styles . icon } source = { imageUrlObject } />
226+ < EdgeText style = { styles . title } > { `${ currencyName } (${ currencyCodeUppercase } )` } </ EdgeText >
227+ </ EdgeAnim >
228+ < SwipeChart assetId = { coinRankingData . assetId } currencyCode = { currencyCodeUppercase } fiatCurrencyCode = { initFiat } />
229+ < View style = { styles . columns } >
230+ < View style = { styles . column } > { renderRows ( coinRankingData , COLUMN_LEFT_DATA_KEYS ) } </ View >
231+ < View style = { styles . column } > { renderRows ( coinRankingData , COLUMN_RIGHT_DATA_KEYS ) } </ View >
232+ </ View >
206233 </ View >
207- </ View >
234+ ) : (
235+ < EdgeText > Loading...</ EdgeText >
236+ ) }
208237 </ SceneWrapper >
209238 )
210239}
0 commit comments