@@ -37,7 +37,63 @@ type TProps = {
37
37
code : accountApi . AccountCode ;
38
38
} ;
39
39
40
- type AddressDialog = { 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 [ addressDialog , setAddressDialog ] = useState < AddressDialog > ( ) ;
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
@@ -76,9 +132,8 @@ export const Receive = ({
76
132
const availableScriptTypes = useRef < accountApi . ScriptType [ ] > ( ) ;
77
133
78
134
const hasManyScriptTypes = availableScriptTypes . current && availableScriptTypes . current . length > 1 ;
79
- const scriptTypeDialogOpened = ! ! ( addressDialog && ( hasManyScriptTypes || insured ) ) ;
80
135
81
- useEsc ( ( ) => ! scriptTypeDialogOpened && ! verifying && route ( `/account/${ code } ` ) ) ;
136
+ useEsc ( ( ) => ! addressTypeDialog && ! verifying && route ( `/account/${ code } ` ) ) ;
82
137
83
138
useEffect ( ( ) => {
84
139
if ( receiveAddresses ) {
@@ -98,13 +153,10 @@ export const Receive = ({
98
153
}
99
154
} , [ addressType , availableScriptTypes , receiveAddresses ] ) ;
100
155
101
- const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
102
- if ( addressDialog ) {
103
- e . preventDefault ( ) ;
104
- setActiveIndex ( 0 ) ;
105
- setAddressType ( addressDialog . addressType ) ;
106
- setAddressDialog ( undefined ) ;
107
- }
156
+ const handleAddressTypeChosen = ( addressType : number ) => {
157
+ setActiveIndex ( 0 ) ;
158
+ setAddressType ( addressType ) ;
159
+ setAddressTypeDialog ( false ) ;
108
160
} ;
109
161
110
162
const verifyAddress = async ( addressesIndex : number ) => {
@@ -206,45 +258,20 @@ export const Receive = ({
206
258
{ ( hasManyScriptTypes || insured ) && (
207
259
< button
208
260
className = { style . changeType }
209
- onClick = { ( ) => setAddressDialog ( ! addressDialog ? { addressType } : undefined ) } >
261
+ onClick = { ( ) => setAddressTypeDialog ( true ) } >
210
262
{ t ( 'receive.changeScriptType' ) }
211
263
</ button >
212
264
) }
213
- < form onSubmit = { handleSubmit } >
214
- < Dialog open = { scriptTypeDialogOpened } onClose = { ( ) => setAddressDialog ( undefined ) } medium title = { t ( 'receive.changeScriptType' ) } >
215
- { availableScriptTypes . current && availableScriptTypes . current . map ( ( scriptType , i ) => (
216
- < div key = { scriptType } >
217
- { addressDialog && (
218
- < >
219
- < Radio
220
- checked = { addressDialog . addressType === i }
221
- id = { scriptType }
222
- name = "scriptType"
223
- onChange = { ( ) => setAddressDialog ( { addressType : i } ) }
224
- title = { getScriptName ( scriptType ) } >
225
- { t ( `receive.scriptType.${ scriptType } ` ) }
226
- </ Radio >
227
- { scriptType === 'p2tr' && addressDialog . addressType === i && (
228
- < Message type = "warning" >
229
- { t ( 'receive.taprootWarning' ) }
230
- </ Message >
231
- ) }
232
- </ >
233
- ) }
234
- </ div >
235
- ) ) }
236
- { insured && (
237
- < Message type = "warning" >
238
- { t ( 'receive.bitsuranceWarning' ) }
239
- </ Message >
240
- ) }
241
- < DialogButtons >
242
- < Button primary type = "submit" >
243
- { t ( 'button.done' ) }
244
- </ Button >
245
- </ DialogButtons >
246
- </ Dialog >
247
- </ 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
+
248
275
< div className = "buttons" >
249
276
< Button
250
277
disabled = { verifying !== false }
0 commit comments