-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWalletForm.tsx
More file actions
142 lines (132 loc) · 5.46 KB
/
WalletForm.tsx
File metadata and controls
142 lines (132 loc) · 5.46 KB
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
import React, { ReactElement, useState, useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { WalletPOSTParameters } from 'utils/validators'
import { AddressWithPaybuttons } from 'services/addressService'
import Image from 'next/image'
import style from '../Wallet/wallet.module.css'
import style_pb from 'components/Paybutton/paybutton.module.css'
import Plus from 'assets/plus.png'
import axios from 'axios'
import { UserNetworksInfo } from 'services/networkService'
interface IProps {
userAddresses: AddressWithPaybuttons[]
refreshWalletList: Function
userId: string
usedNetworks: UserNetworksInfo[]
}
export default function WalletForm ({ userAddresses, refreshWalletList, userId, usedNetworks }: IProps): ReactElement {
const { register, handleSubmit, reset } = useForm<WalletPOSTParameters>()
const [modal, setModal] = useState(false)
const [error, setError] = useState('')
const [selectedAddressIdList, setSelectedAddressIdList] = useState([] as string[])
async function onSubmit (params: WalletPOSTParameters): Promise<void> {
params.userId = userId
params.addressIdList = selectedAddressIdList
try {
void await axios.post(`/api/wallet`, params)
void refreshWalletList()
setError('')
} catch (err: any) {
setError(err.response.data.message)
}
}
function handleSelectedAddressesChange(checked: boolean, addressId: string): void {
const addressIsSelected = selectedAddressIdList.includes(addressId)
if (addressIsSelected && checked === false) {
setSelectedAddressIdList(
selectedAddressIdList.filter(id => id !== addressId)
)
}
if (!addressIsSelected && checked === true) {
setSelectedAddressIdList(
[...selectedAddressIdList, addressId]
)
}
}
useEffect(() => {
}, [selectedAddressIdList])
useEffect(() => {
setModal(false)
reset()
}, [userAddresses])
useEffect(() => {
setSelectedAddressIdList([])
}, [modal])
return (
<>
<div className={style_pb.create_button_ctn}>
<div className={style_pb.create_button} onClick={() => setModal(true)}>
<Image src={Plus} alt='arrow' width={30} height={30} />
<div className={style_pb.tooltiptext}>New Wallet</div>
</div>
</div>
{modal
? (
<div className={style_pb.form_ctn_outer}>
<div className={style_pb.form_ctn_inner}>
<h4>Create New Wallet</h4>
<div className={style_pb.form_ctn}>
<p>Wallets must have a unique name and contain at least one address. Each address can only be linked to one wallet at a time.</p>
<form onSubmit={handleSubmit(onSubmit)} method='post'>
<label htmlFor='name'>Wallet Name</label>
<input
{...register('name')}
type='text'
id='name'
name='name'
autoFocus
/>
<h4>Select Buttons</h4>
<div className={style.buttonlist_ctn}>
{userAddresses.map((addr, index) => (
<div className={style.input_field} key={`create-addr-${addr.id}`}>
<input
type='checkbox'
value={addr.id}
id={`addressIdList.${index}`}
onChange={ (e) => handleSelectedAddressesChange(e.target.checked, addr.id) }
/>
<label htmlFor={`addressIdList.${index}`}>
{addr.paybuttons.map((conn) => (
<div className={style.buttonpill}>{conn.paybutton.name}</div>
))}
<div className={style.addresslabel}>{addr.address}</div>
</label>
</div>
))}
</div>
<div className={style.makedefault_ctn} key={'wallet-create'}>
{usedNetworks.some(network => network.ticker === 'xec') &&
<div className={style.input_field}>
<input
{...register('isXECDefault')}
type="checkbox"
id='isXECDefault'
/>
<label htmlFor='xec-default' className={style.makedefault_margin}>Default XEC Wallet</label>
</div>
}
{usedNetworks.some(network => network.ticker === 'bch') &&
<div className={style.input_field}>
<input
{...register('isBCHDefault')}
type="checkbox"
id='isBCHDefault'
/>
<label htmlFor='bch-default' className={style.makedefault_margin}>Default BCH Wallet</label>
</div>
}
</div>
<div className={style_pb.btn_row}>
{error !== '' && <div className={style_pb.error_message}>{error}</div>}
<button type='submit' className='button_main'>Submit</button>
<button onClick={() => { setModal(false); reset() }} className='button_outline'>Cancel</button>
</div>
</form>
</div>
</div>
</div>)
: null}
</>
)
}