@@ -37,7 +37,63 @@ type TProps = {
37
37
code : accountApi . AccountCode ;
38
38
} ;
39
39
40
- type AddressTypeDialog = { addressType : number } | undefined ;
40
+ type TAddressTypeDialogProps = {
41
+ open : boolean ;
42
+ setOpen : ( open : boolean ) => void ;
43
+ preselectedAddressType : number ;
44
+ availableScriptTypes ?: accountApi . ScriptType [ ] ;
45
+ insured : boolean ;
46
+ handleAddressTypeChosen : ( addressType : number ) => void ;
47
+ } ;
48
+
49
+ const AddressTypeDialog = ( {
50
+ open,
51
+ setOpen,
52
+ preselectedAddressType,
53
+ availableScriptTypes,
54
+ insured,
55
+ handleAddressTypeChosen,
56
+ } : TAddressTypeDialogProps ) => {
57
+ const { t } = useTranslation ( ) ;
58
+ const [ addressType , setAddressType ] = useState < number > ( preselectedAddressType ) ;
59
+
60
+ return (
61
+ < Dialog open = { open } onClose = { ( ) => setOpen ( false ) } medium title = { t ( 'receive.changeScriptType' ) } >
62
+ < form onSubmit = { ( e : React . FormEvent < HTMLFormElement > ) => {
63
+ e . preventDefault ( ) ;
64
+ handleAddressTypeChosen ( addressType ) ;
65
+ } } >
66
+ { availableScriptTypes && availableScriptTypes . map ( ( scriptType , i ) => (
67
+ < div key = { scriptType } >
68
+ < Radio
69
+ checked = { addressType === i }
70
+ id = { scriptType }
71
+ name = "scriptType"
72
+ onChange = { ( ) => setAddressType ( i ) }
73
+ title = { getScriptName ( scriptType ) } >
74
+ { t ( `receive.scriptType.${ scriptType } ` ) }
75
+ </ Radio >
76
+ { scriptType === 'p2tr' && addressType === i && (
77
+ < Message type = "warning" >
78
+ { t ( 'receive.taprootWarning' ) }
79
+ </ Message >
80
+ ) }
81
+ </ div >
82
+ ) ) }
83
+ { insured && (
84
+ < Message type = "warning" >
85
+ { t ( 'receive.bitsuranceWarning' ) }
86
+ </ Message >
87
+ ) }
88
+ < DialogButtons >
89
+ < Button primary type = "submit" >
90
+ { t ( 'button.done' ) }
91
+ </ Button >
92
+ </ DialogButtons >
93
+ </ form >
94
+ </ Dialog >
95
+ ) ;
96
+ } ;
41
97
42
98
// For BTC/LTC: all possible address types we want to offer to the user, ordered by priority (first one is default).
43
99
// Types that are not available in the addresses delivered by the backend should be ignored.
@@ -63,7 +119,7 @@ export const Receive = ({
63
119
const [ activeIndex , setActiveIndex ] = useState < number > ( 0 ) ;
64
120
// index into `availableScriptTypes`, or 0 if none are available.
65
121
const [ addressType , setAddressType ] = useState < number > ( 0 ) ;
66
- const [ addressTypeDialog , setAddressTypeDialog ] = useState < AddressTypeDialog > ( ) ;
122
+ const [ addressTypeDialog , setAddressTypeDialog ] = useState < boolean > ( false ) ;
67
123
const [ currentAddresses , setCurrentAddresses ] = useState < accountApi . IReceiveAddress [ ] > ( ) ;
68
124
const [ currentAddressIndex , setCurrentAddressIndex ] = useState < number > ( 0 ) ;
69
125
@@ -77,7 +133,7 @@ export const Receive = ({
77
133
78
134
const hasManyScriptTypes = availableScriptTypes . current && availableScriptTypes . current . length > 1 ;
79
135
80
- useEsc ( ( ) => addressTypeDialog === undefined && ! verifying && route ( `/account/${ code } ` ) ) ;
136
+ useEsc ( ( ) => ! addressTypeDialog && ! verifying && route ( `/account/${ code } ` ) ) ;
81
137
82
138
useEffect ( ( ) => {
83
139
if ( receiveAddresses ) {
@@ -97,13 +153,10 @@ export const Receive = ({
97
153
}
98
154
} , [ addressType , availableScriptTypes , receiveAddresses ] ) ;
99
155
100
- const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
101
- if ( addressTypeDialog ) {
102
- e . preventDefault ( ) ;
103
- setActiveIndex ( 0 ) ;
104
- setAddressType ( addressTypeDialog . addressType ) ;
105
- setAddressTypeDialog ( undefined ) ;
106
- }
156
+ const handleAddressTypeChosen = ( addressType : number ) => {
157
+ setActiveIndex ( 0 ) ;
158
+ setAddressType ( addressType ) ;
159
+ setAddressTypeDialog ( false ) ;
107
160
} ;
108
161
109
162
const verifyAddress = async ( addressesIndex : number ) => {
@@ -205,45 +258,20 @@ export const Receive = ({
205
258
{ ( hasManyScriptTypes || insured ) && (
206
259
< button
207
260
className = { style . changeType }
208
- onClick = { ( ) => setAddressTypeDialog ( ! addressTypeDialog ? { addressType } : undefined ) } >
261
+ onClick = { ( ) => setAddressTypeDialog ( true ) } >
209
262
{ t ( 'receive.changeScriptType' ) }
210
263
</ button >
211
264
) }
212
- < form onSubmit = { handleSubmit } >
213
- < Dialog open = { addressTypeDialog !== undefined } onClose = { ( ) => setAddressTypeDialog ( undefined ) } medium title = { t ( 'receive.changeScriptType' ) } >
214
- { availableScriptTypes . current && availableScriptTypes . current . map ( ( scriptType , i ) => (
215
- < div key = { scriptType } >
216
- { addressTypeDialog && (
217
- < >
218
- < Radio
219
- checked = { addressTypeDialog . addressType === i }
220
- id = { scriptType }
221
- name = "scriptType"
222
- onChange = { ( ) => setAddressTypeDialog ( { addressType : i } ) }
223
- title = { getScriptName ( scriptType ) } >
224
- { t ( `receive.scriptType.${ scriptType } ` ) }
225
- </ Radio >
226
- { scriptType === 'p2tr' && addressTypeDialog . addressType === i && (
227
- < Message type = "warning" >
228
- { t ( 'receive.taprootWarning' ) }
229
- </ Message >
230
- ) }
231
- </ >
232
- ) }
233
- </ div >
234
- ) ) }
235
- { insured && (
236
- < Message type = "warning" >
237
- { t ( 'receive.bitsuranceWarning' ) }
238
- </ Message >
239
- ) }
240
- < DialogButtons >
241
- < Button primary type = "submit" >
242
- { t ( 'button.done' ) }
243
- </ Button >
244
- </ DialogButtons >
245
- </ Dialog >
246
- </ form >
265
+
266
+ < AddressTypeDialog
267
+ open = { addressTypeDialog }
268
+ setOpen = { setAddressTypeDialog }
269
+ preselectedAddressType = { addressType }
270
+ availableScriptTypes = { availableScriptTypes . current }
271
+ insured = { insured }
272
+ handleAddressTypeChosen = { handleAddressTypeChosen }
273
+ />
274
+
247
275
< div className = "buttons" >
248
276
< Button
249
277
disabled = { verifying !== false }
0 commit comments