1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17-
17+ import { useContext } from 'react' ;
1818import { Fiat , ConversionUnit , IAmount } from '../../api/account' ;
19- import { BtcUnit } from '../../api/coins' ;
20- import { reinitializeAccounts } from '../../api/backend' ;
21- import { share } from '../../decorators/share' ;
22- import { Store } from '../../decorators/store' ;
23- import { setConfig } from '../../utils/config' ;
19+ import { RatesContext } from '../../contexts/RatesContext' ;
2420import { Amount } from '../../components/amount/amount' ;
25- import { equal } from '../../utils/equal' ;
26- import { apiGet } from '../../utils/request' ;
2721import style from './rates.module.css' ;
2822
29- export interface SharedProps {
30- active : Fiat ;
31- // eslint-disable-next-line react/no-unused-prop-types
32- selected : Fiat [ ] ;
33- btcUnit ?: BtcUnit ;
34- alwaysShowAmounts ?: boolean ;
35- }
36-
37- export type FiatWithDisplayName = {
23+ type FiatWithDisplayName = {
3824 currency : Fiat ,
3925 displayName : string
4026}
@@ -60,68 +46,6 @@ export const currenciesWithDisplayName: FiatWithDisplayName[] = [
6046 { currency : 'USD' , displayName : 'United States Dollar' } ,
6147 { currency : 'BTC' , displayName : 'Bitcoin' }
6248] ;
63- export const currencies : Fiat [ ] = [ 'AUD' , 'BRL' , 'CAD' , 'CHF' , 'CNY' , 'CZK' , 'EUR' , 'GBP' , 'HKD' , 'ILS' , 'JPY' , 'KRW' , 'NOK' , 'PLN' , 'RUB' , 'SEK' , 'SGD' , 'USD' , 'BTC' ] ;
64-
65- export const store = new Store < SharedProps > ( {
66- active : 'USD' ,
67- selected : [ 'USD' , 'EUR' , 'CHF' ] ,
68- btcUnit : 'default' ,
69- } ) ;
70-
71- // TODO: should not invoking apiGet imediatelly, see the apiGet() function for more details
72- updateRatesConfig ( ) ;
73-
74- export function updateRatesConfig ( ) : void {
75- apiGet ( 'config' ) . then ( ( appconf ) => {
76- if ( appconf . frontend && appconf . backend . mainFiat ) {
77- store . setState ( { active : appconf . backend . mainFiat } ) ;
78- }
79- if ( appconf . backend && appconf . backend . fiatList && appconf . backend . btcUnit ) {
80- store . setState ( {
81- selected : appconf . backend . fiatList ,
82- btcUnit : appconf . backend . btcUnit ,
83- } ) ;
84- }
85- } ) ;
86- }
87-
88- //this is setting default currency
89- export function setActiveFiat ( fiat : Fiat ) : void {
90- if ( ! store . state . selected . includes ( fiat ) ) {
91- selectFiat ( fiat ) ;
92- }
93- store . setState ( { active : fiat } ) ;
94- setConfig ( { backend : { mainFiat : fiat } } ) ;
95- }
96-
97- function rotateFiat ( ) : void {
98- const index = store . state . selected . indexOf ( store . state . active ) ;
99- const fiat = store . state . selected [ ( index + 1 ) % store . state . selected . length ] ;
100- setActiveFiat ( fiat ) ;
101- }
102-
103- //this is selecting active currency
104- export function selectFiat ( fiat : Fiat ) : void {
105- const selected = [ ...store . state . selected , fiat ] ;
106- setConfig ( { backend : { fiatList : selected } } )
107- . then ( ( ) => {
108- store . setState ( { selected } ) ;
109- // Need to reconfigure currency exchange rates updater
110- // which is done during accounts reset.
111- reinitializeAccounts ( ) ;
112- } ) ;
113- }
114-
115- export function unselectFiat ( fiat : Fiat ) : void {
116- const selected = store . state . selected . filter ( item => ! equal ( item , fiat ) ) ;
117- setConfig ( { backend : { fiatList : selected } } )
118- . then ( ( ) => {
119- store . setState ( { selected } ) ;
120- // Need to reconfigure currency exchange rates updater
121- // which is done during accounts reset.
122- reinitializeAccounts ( ) ;
123- } ) ;
124- }
12549
12650export function formatNumber ( amount : number , maxDigits : number ) : string {
12751 let formatted = amount . toFixed ( maxDigits ) ;
@@ -141,38 +65,37 @@ type TProvidedProps = {
14165 noAction ?: boolean ;
14266 sign ?: string ;
14367 noBtcZeroes ?: boolean ;
68+ alwaysShowAmounts ?: boolean ;
14469}
14570
146- type TProps = TProvidedProps & SharedProps ;
14771
14872function Conversion ( {
14973 amount,
15074 tableRow,
15175 unstyled,
15276 skipUnit,
153- active,
15477 noAction,
15578 sign,
15679 noBtcZeroes,
157- btcUnit,
15880 alwaysShowAmounts = false
159- } : TProps ) {
81+ } : TProvidedProps ) {
82+
83+ const { rotateFiat, defaultCurrency, btcUnit } = useContext ( RatesContext ) ;
16084
16185 let formattedAmount = < > { '---' } </ > ;
16286 let isAvailable = false ;
16387
164- var activeUnit : ConversionUnit = active ;
165- if ( active === 'BTC' && btcUnit === 'sat' ) {
88+ let activeUnit : ConversionUnit = defaultCurrency ;
89+ if ( defaultCurrency === 'BTC' && btcUnit === 'sat' ) {
16690 activeUnit = 'sat' ;
16791 }
16892
169- // amount.conversions[active ] can be empty in recent transactions.
170- if ( amount && amount . conversions && amount . conversions [ active ] && amount . conversions [ active ] !== '' ) {
93+ // amount.conversions[defaultCurrency ] can be empty in recent transactions.
94+ if ( amount && amount . conversions && amount . conversions [ defaultCurrency ] && amount . conversions [ defaultCurrency ] !== '' ) {
17195 isAvailable = true ;
172- formattedAmount = < Amount alwaysShowAmounts = { alwaysShowAmounts } amount = { amount . conversions [ active ] } unit = { activeUnit } removeBtcTrailingZeroes = { ! ! noBtcZeroes } /> ;
96+ formattedAmount = < Amount alwaysShowAmounts = { alwaysShowAmounts } amount = { amount . conversions [ defaultCurrency ] } unit = { activeUnit } removeBtcTrailingZeroes = { ! ! noBtcZeroes } /> ;
17397 }
17498
175-
17699 if ( tableRow ) {
177100 return (
178101 < tr className = { unstyled ? '' : style . fiatRow } >
@@ -211,4 +134,4 @@ function Conversion({
211134
212135export const formattedCurrencies = currenciesWithDisplayName . map ( ( fiat ) => ( { label : `${ fiat . displayName } (${ fiat . currency } )` , value : fiat . currency } ) ) ;
213136
214- export const FiatConversion = share < SharedProps , TProvidedProps > ( store ) ( Conversion ) ;
137+ export const FiatConversion = Conversion ;
0 commit comments