1
1
import { isInRange , hasLength , matches , useForm } from '@mantine/form'
2
2
import { useCallback , useMemo , useState , FormEvent } from 'react'
3
- import { Button , Group , TextInput , Box , NumberInput , Stack } from '@mantine/core'
3
+ import {
4
+ Button ,
5
+ Group ,
6
+ TextInput ,
7
+ Box ,
8
+ NumberInput ,
9
+ Stack ,
10
+ SegmentedControl ,
11
+ Center ,
12
+ ThemeIcon ,
13
+ MantineColor
14
+ } from '@mantine/core'
4
15
import { defaultConfirmModalProps } from 'components/common/Modals/CustomConfirmModal'
5
16
import { modals } from '@mantine/modals'
6
17
import useAdmin from 'hooks/useAdmin'
7
18
import throttle from 'lodash.throttle'
8
- import { Account } from 'types'
19
+ import { Account , AdminTransfer , AdminTransferType } from 'types'
9
20
import useCustomNotifications from 'hooks/useCustomNotifications'
21
+ import DepositIcon from 'resources/icons/Deposit'
22
+ import WithdrawIcon from 'resources/icons/Withdraw'
23
+ import { parseBigNumTokenAmountToDecimal } from 'helpers/balances'
10
24
11
- type Deposit = {
12
- accountId : string
13
- amount : number
14
- token : {
15
- name : string
16
- chainId : number
17
- address : string
18
- decimals : number
19
- }
20
- txHash : string
25
+ const transferTypeLabels : { [ key in AdminTransferType ] : { label : string ; color : MantineColor } } = {
26
+ deposit : { label : 'deposit' , color : 'success' } ,
27
+ withdraw : { label : 'refund' , color : 'warning' }
21
28
}
22
29
23
30
function AdminDeposit ( { accountData } : { accountData : Account } ) {
24
31
const { showNotification } = useCustomNotifications ( )
25
- const { makeDeposit , getAllAccounts } = useAdmin ( )
32
+ const { makeTransfer , getAllAccounts } = useAdmin ( )
26
33
const [ loading , setLoading ] = useState ( false )
34
+ const [ transferType , setTransferType ] = useState < AdminTransferType > ( 'deposit' )
35
+ const balance = useMemo (
36
+ ( ) =>
37
+ parseBigNumTokenAmountToDecimal (
38
+ accountData . availableBalance ,
39
+ accountData . balanceToken . decimals
40
+ ) ,
41
+ [ accountData . availableBalance , accountData . balanceToken . decimals ]
42
+ )
27
43
28
- const form = useForm < Deposit > ( {
44
+ const form = useForm < AdminTransfer > ( {
29
45
initialValues : {
30
46
accountId : accountData . id ,
31
47
amount : 0 ,
@@ -40,7 +56,16 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
40
56
41
57
validate : {
42
58
accountId : matches ( / ^ ( 0 x ) ? [ 0 - 9 a - f A - F ] { 40 } $ / , 'invalid account address' ) ,
43
- amount : isInRange ( { min : 10 , max : 1000000000000 } , 'min 10' ) ,
59
+ amount :
60
+ transferType === 'deposit'
61
+ ? isInRange ( { min : 10 , max : 100_000_000 } , 'min: 10, max: 100k' )
62
+ : isInRange (
63
+ {
64
+ min : 1 ,
65
+ max : balance
66
+ } ,
67
+ `min: 1, max: balance ${ balance } `
68
+ ) ,
44
69
token : {
45
70
name : hasLength ( { min : 1 } ) ,
46
71
chainId : isInRange ( { min : 0 , max : 999999 } ) ,
@@ -52,10 +77,11 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
52
77
} )
53
78
54
79
const handleSubmit = useCallback (
55
- async ( values : Deposit ) => {
80
+ async ( values : AdminTransfer ) => {
56
81
setLoading ( true )
57
- await makeDeposit (
82
+ await makeTransfer (
58
83
values ,
84
+ transferType ,
59
85
( ) => {
60
86
getAllAccounts ( )
61
87
form . reset ( )
@@ -68,7 +94,7 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
68
94
69
95
setLoading ( false )
70
96
} ,
71
- [ form , makeDeposit , showNotification , getAllAccounts ]
97
+ [ makeTransfer , transferType , getAllAccounts , form , showNotification ]
72
98
)
73
99
74
100
const throttledSbm = useMemo (
@@ -82,19 +108,48 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
82
108
! form . validate ( ) . hasErrors &&
83
109
modals . openConfirmModal (
84
110
defaultConfirmModalProps ( {
85
- text : `Are you sure you want to deposit ${ form . values . amount } ${ form . values . token . name } to ${ form . values . accountId } ?` ,
111
+ text : `Are you sure you want to ${ transferTypeLabels [ transferType ] . label } ${ form . values . amount } ${ form . values . token . name } to ${ form . values . accountId } ?` ,
86
112
color : 'attention' ,
87
113
labels : { confirm : 'Yes Sir' , cancel : 'No' } ,
88
114
onConfirm : ( ) => form . onSubmit ( throttledSbm ) ( )
89
115
} )
90
116
)
91
117
} ,
92
- [ form , throttledSbm ]
118
+ [ form , throttledSbm , transferType ]
93
119
)
94
120
95
121
return (
96
122
< Box component = "form" >
97
123
< Stack gap = "xs" >
124
+ < SegmentedControl
125
+ color = { transferTypeLabels [ transferType ] . color }
126
+ value = { transferType }
127
+ onChange = { ( val ) => setTransferType ( val as AdminTransferType ) }
128
+ data = { [
129
+ {
130
+ label : (
131
+ < Center style = { { gap : 10 } } >
132
+ < ThemeIcon size = "sm" variant = "transparent" color = "mainText" >
133
+ < DepositIcon />
134
+ </ ThemeIcon >
135
+ < span > Deposit</ span >
136
+ </ Center >
137
+ ) ,
138
+ value : 'deposit'
139
+ } ,
140
+ {
141
+ label : (
142
+ < Center style = { { gap : 10 } } >
143
+ < ThemeIcon size = "sm" variant = "transparent" color = "mainText" >
144
+ < WithdrawIcon />
145
+ </ ThemeIcon >
146
+ < span > Refund</ span >
147
+ </ Center >
148
+ ) ,
149
+ value : 'withdraw'
150
+ }
151
+ ] }
152
+ />
98
153
< TextInput
99
154
label = "Account address"
100
155
placeholder = "0x000"
@@ -105,6 +160,7 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
105
160
< Group grow align = "baseline" >
106
161
< NumberInput
107
162
label = "Amount"
163
+ description = { transferType === 'withdraw' ? `available balance: ${ balance } ` : undefined }
108
164
// type="number"
109
165
placeholder = "Amount"
110
166
hideControls
@@ -153,12 +209,13 @@ function AdminDeposit({ accountData }: { accountData: Account }) {
153
209
154
210
< Group justify = "left" mt = "md" >
155
211
< Button
212
+ color = { transferTypeLabels [ transferType ] . color }
156
213
type = "submit"
157
214
loading = { loading }
158
215
disabled = { loading || ! form . isDirty ( ) }
159
216
onClick = { onSubmit }
160
217
>
161
- Make deposit
218
+ Make { transferTypeLabels [ transferType ] . label }
162
219
</ Button >
163
220
</ Group >
164
221
</ Stack >
0 commit comments