Skip to content

Commit ec2001a

Browse files
committed
frontend/receive: split address type dialog into own component
Untangle the big receive component.
1 parent e660d17 commit ec2001a

File tree

1 file changed

+74
-46
lines changed

1 file changed

+74
-46
lines changed

frontends/web/src/routes/account/receive/receive.tsx

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,63 @@ type TProps = {
3737
code: accountApi.AccountCode;
3838
};
3939

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+
};
4197

4298
// For BTC/LTC: all possible address types we want to offer to the user, ordered by priority (first one is default).
4399
// Types that are not available in the addresses delivered by the backend should be ignored.
@@ -63,7 +119,7 @@ export const Receive = ({
63119
const [activeIndex, setActiveIndex] = useState<number>(0);
64120
// index into `availableScriptTypes`, or 0 if none are available.
65121
const [addressType, setAddressType] = useState<number>(0);
66-
const [addressTypeDialog, setAddressTypeDialog] = useState<AddressTypeDialog>();
122+
const [addressTypeDialog, setAddressTypeDialog] = useState<boolean>(false);
67123
const [currentAddresses, setCurrentAddresses] = useState<accountApi.IReceiveAddress[]>();
68124
const [currentAddressIndex, setCurrentAddressIndex] = useState<number>(0);
69125

@@ -77,7 +133,7 @@ export const Receive = ({
77133

78134
const hasManyScriptTypes = availableScriptTypes.current && availableScriptTypes.current.length > 1;
79135

80-
useEsc(() => addressTypeDialog === undefined && !verifying && route(`/account/${code}`));
136+
useEsc(() => !addressTypeDialog && !verifying && route(`/account/${code}`));
81137

82138
useEffect(() => {
83139
if (receiveAddresses) {
@@ -97,13 +153,10 @@ export const Receive = ({
97153
}
98154
}, [addressType, availableScriptTypes, receiveAddresses]);
99155

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);
107160
};
108161

109162
const verifyAddress = async (addressesIndex: number) => {
@@ -205,45 +258,20 @@ export const Receive = ({
205258
{ (hasManyScriptTypes || insured) && (
206259
<button
207260
className={style.changeType}
208-
onClick={() => setAddressTypeDialog(!addressTypeDialog ? { addressType } : undefined)}>
261+
onClick={() => setAddressTypeDialog(true)}>
209262
{t('receive.changeScriptType')}
210263
</button>
211264
)}
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+
247275
<div className="buttons">
248276
<Button
249277
disabled={verifying !== false}

0 commit comments

Comments
 (0)