/
BuySellDialog.js
170 lines (153 loc) · 5.07 KB
/
BuySellDialog.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// @flow
import type { Node } from 'react';
import { Component } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import type { $npm$ReactIntl$IntlFormat } from 'react-intl';
import { truncateAddress } from '../../utils/formatters';
import Dialog from '../widgets/Dialog';
import DialogCloseButton from '../widgets/DialogCloseButton';
import ChangellyFetcher from './ChangellyFetcher'
import styles from './BuySellDialog.scss';
import { ReactComponent as VerifyIcon } from '../../assets/images/verify-icon.inline.svg'
import VerticalFlexContainer from '../layout/VerticalFlexContainer'
import LoadingSpinner from '../widgets/LoadingSpinner'
import globalMessages from '../../i18n/global-messages';
import { trackBuySellDialog } from '../../api/analytics';
const messages = defineMessages({
dialogTitle: {
id: 'buy.dialog.title',
defaultMessage: '!!!Buy ADA',
},
dialogSelectAddress: {
id: 'buysell.dialog.selectAddress',
defaultMessage: '!!!Please select the receiving address. This will be shared with the third party provider called Changelly for the buy / sell of ADA. ',
},
dialogDescription: {
id: 'buysell.dialog.instructions',
defaultMessage: '!!!Please select your preferences. On the next screen, confirm your selection by pressing the green arrow on the top right',
},
dialogManual: {
id: 'buysell.dialog.manual',
defaultMessage: '!!!I will add my address manually',
},
});
export type WalletInfo = {|
walletName: string,
currencyName: string,
anAddressFormatted: string,
|}
type Props = {|
+onCancel: void => void,
+genWalletList: () => Promise<Array<WalletInfo>>
|};
const WIDGET_URL = 'https://widget.changelly.com?from=*&to=*&amount=200&fromDefault=usd&toDefault=ada&theme=default&merchant_id=g9qheu8vschp16jj&payment_id=&v=3'
type State = {|
addressSelected: ?string,
walletList: ?Array<WalletInfo>,
|};
@observer
export default class BuySellDialog extends Component<Props, State> {
static contextTypes: {|intl: $npm$ReactIntl$IntlFormat|} = {
intl: intlShape.isRequired,
};
state: State = {
addressSelected: null,
walletList: null,
};
async componentDidMount () {
const { intl } = this.context;
const resp = await this.props.genWalletList()
const wallets = [
...resp,
{
walletName: intl.formatMessage(messages.dialogManual),
currencyName: '',
anAddressFormatted: '',
}
]
this.setState({ walletList: wallets })
trackBuySellDialog();
}
createRows: ($npm$ReactIntl$IntlFormat, Array<WalletInfo>) => Node = (intl, wallets) => (
wallets.map((wallet, i) => {
return (
// eslint-disable-next-line react/no-array-index-key
<div key={i} className={styles.row}>
<div className={styles.left}>
<div className={styles.nameAndCurrency}>
{ wallet.currencyName ? `(${wallet.currencyName}) ` : ''}{wallet.walletName}
</div>
<div className={styles.address}>
{truncateAddress(wallet.anAddressFormatted)}
</div>
</div>
<div className={styles.right}>
{/* Verify Address action */}
<button
type="button"
onClick={() =>
this.setState({ addressSelected: wallet.anAddressFormatted })
}
>
<div>
<span className={styles.verifyIcon}>
<VerifyIcon />
</span>
</div>
</button>
{/* Action block end */}
</div>
</div>
)
})
)
render(): Node {
const { intl } = this.context;
if (this.state.walletList == null) {
return (
<Dialog
title={intl.formatMessage(globalMessages.processingLabel)}
closeOnOverlayClick={false}
>
<VerticalFlexContainer>
<LoadingSpinner />
</VerticalFlexContainer>
</Dialog>
);
}
if (this.state.addressSelected == null) {
const addressNodes = this.createRows(intl, this.state.walletList);
return (
<Dialog
title={intl.formatMessage(messages.dialogTitle)}
closeOnOverlayClick={false}
onClose={this.props.onCancel}
closeButton={<DialogCloseButton />}
className=""
>
<div className={styles.content}>
{intl.formatMessage(messages.dialogSelectAddress)}
{addressNodes}
</div>
</Dialog>
)
}
return (
<Dialog
title={intl.formatMessage(messages.dialogTitle)}
closeOnOverlayClick={false}
onClose={this.props.onCancel}
closeButton={<DialogCloseButton />}
className=""
>
<div className={styles.component}>
<div className={styles.description}>
{intl.formatMessage(messages.dialogDescription)}
</div>
<ChangellyFetcher widgetURL={WIDGET_URL} address={this.state.addressSelected} />
</div>
</Dialog>
);
}
}