14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
-
17
+ import { useContext } from 'react' ;
18
18
import { 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' ;
24
20
import { Amount } from '../../components/amount/amount' ;
25
- import { equal } from '../../utils/equal' ;
26
- import { apiGet } from '../../utils/request' ;
27
21
import style from './rates.module.css' ;
28
22
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 = {
38
24
currency : Fiat ,
39
25
displayName : string
40
26
}
@@ -60,68 +46,6 @@ export const currenciesWithDisplayName: FiatWithDisplayName[] = [
60
46
{ currency : 'USD' , displayName : 'United States Dollar' } ,
61
47
{ currency : 'BTC' , displayName : 'Bitcoin' }
62
48
] ;
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
- }
125
49
126
50
export function formatNumber ( amount : number , maxDigits : number ) : string {
127
51
let formatted = amount . toFixed ( maxDigits ) ;
@@ -141,38 +65,37 @@ type TProvidedProps = {
141
65
noAction ?: boolean ;
142
66
sign ?: string ;
143
67
noBtcZeroes ?: boolean ;
68
+ alwaysShowAmounts ?: boolean ;
144
69
}
145
70
146
- type TProps = TProvidedProps & SharedProps ;
147
71
148
72
function Conversion ( {
149
73
amount,
150
74
tableRow,
151
75
unstyled,
152
76
skipUnit,
153
- active,
154
77
noAction,
155
78
sign,
156
79
noBtcZeroes,
157
- btcUnit,
158
80
alwaysShowAmounts = false
159
- } : TProps ) {
81
+ } : TProvidedProps ) {
82
+
83
+ const { rotateFiat, defaultCurrency, btcUnit } = useContext ( RatesContext ) ;
160
84
161
85
let formattedAmount = < > { '---' } </ > ;
162
86
let isAvailable = false ;
163
87
164
- var activeUnit : ConversionUnit = active ;
165
- if ( active === 'BTC' && btcUnit === 'sat' ) {
88
+ let activeUnit : ConversionUnit = defaultCurrency ;
89
+ if ( defaultCurrency === 'BTC' && btcUnit === 'sat' ) {
166
90
activeUnit = 'sat' ;
167
91
}
168
92
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 ] !== '' ) {
171
95
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 } /> ;
173
97
}
174
98
175
-
176
99
if ( tableRow ) {
177
100
return (
178
101
< tr className = { unstyled ? '' : style . fiatRow } >
@@ -211,4 +134,4 @@ function Conversion({
211
134
212
135
export const formattedCurrencies = currenciesWithDisplayName . map ( ( fiat ) => ( { label : `${ fiat . displayName } (${ fiat . currency } )` , value : fiat . currency } ) ) ;
213
136
214
- export const FiatConversion = share < SharedProps , TProvidedProps > ( store ) ( Conversion ) ;
137
+ export const FiatConversion = Conversion ;
0 commit comments