This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
/
CurrencySelector.js
90 lines (79 loc) · 2.13 KB
/
CurrencySelector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { FormattedMessage } from 'react-intl';
import { changeCurrency } from '../../state/fundraiser/actions';
import type { AppState } from '../../state/reducers';
type Props = {};
type State = {
currencyDropdVisible: boolean,
};
class CurrencySelector extends Component<Props> {
constructor(props: Props) {
super(props);
this.state = {
currencyDropdVisible: false,
};
}
toggleCurrencyDropd() {
this.setState({
currencyDropdVisible: !this.state.currencyDropdVisible,
});
}
onSelectCurrency(currency: string): void {
this.props.changeCurrency(currency);
}
selectElement() {
return (
this.state.currencyDropdVisible && (
<select
value={this.props.currency}
className="AmountSelection__currency-selector"
onChange={(e: SyntheticEvent<HTMLSelectElement>) =>
this.onSelectCurrency(e.currentTarget.value)
}
>
{Object.keys(this.props.donationBands).map(currency => {
return (
<option key={currency} value={currency}>
{currency}
</option>
);
})}
</select>
)
);
}
render() {
return (
<p>
<FormattedMessage
id="fundraiser.currency_in"
defaultMessage="Values shown in {currency}."
values={{ currency: this.props.currency }}
/>
.
<a
onClick={this.toggleCurrencyDropd.bind(this)}
className="AmountSelection__currency-toggle"
>
<FormattedMessage
id="fundraiser.switch_currency"
defaultMessage="Switch currency"
/>
</a>
{this.selectElement()}
</p>
);
}
}
const mapState = (state: AppState) => ({
currency: state.fundraiser.currency,
donationBands: state.fundraiser.donationBands,
});
const mapDispatch = (dispatch: Dispatch<*>) => ({
changeCurrency: (currency: string) => dispatch(changeCurrency(currency)),
});
export default connect(
mapState,
mapDispatch
)(CurrencySelector);