-
Notifications
You must be signed in to change notification settings - Fork 932
/
Copy pathindex.tsx
182 lines (167 loc) · 4.69 KB
/
index.tsx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React, { FunctionComponent, useState } from 'react'
import { AmountInput, Button, Checkbox } from '../../atoms'
import { TransactionModal } from '../../molecules/transaction-modal'
import { Utils } from '../../../shared/utils'
import styles from './style.module.css'
import { Spacer } from '../../atoms/spacer'
import { deposit } from 'crowdfund-contract'
import * as abundance from 'abundance-token'
export interface IFormPledgeProps {
account: string
decimals: number
symbol?: string
onPledge: () => void
updatedAt: number
}
export interface IResultSubmit {
status: string
value?: string
symbol?: string
error?: string
}
/**
* Mint 100.0000000 tokens to the user's wallet for testing
*/
function MintButton({ account, symbol, onComplete, decimals }: { decimals: number, account: string; symbol: string, onComplete: () => void }) {
const [isSubmitting, setSubmitting] = useState(false)
const displayAmount = 100
const amount = BigInt(displayAmount * 10 ** decimals)
return (
<Button
title={`Mint ${displayAmount} ${symbol}`}
onClick={async () => {
setSubmitting(true)
await abundance.mint({ to: account, amount })
setSubmitting(false)
onComplete()
}}
disabled={isSubmitting}
isLoading={isSubmitting}
/>
)
}
const FormPledge: FunctionComponent<IFormPledgeProps> = props => {
const [balance, setBalance] = React.useState<BigInt>(BigInt(0))
const [decimals, setDecimals] = React.useState<number>(0)
const [symbol, setSymbol] = React.useState<string>()
const [amount, setAmount] = useState<number>()
const [resultSubmit, setResultSubmit] = useState<IResultSubmit | undefined>()
const [input, setInput] = useState('')
const [isSubmitting, setSubmitting] = useState(false)
React.useEffect(() => {
Promise.all([
abundance.balance({ id: props.account }),
abundance.decimals(),
abundance.symbol(),
]).then(fetched => {
setBalance(fetched[0])
setDecimals(fetched[1])
setSymbol(fetched[2].toString())
})
}, [props.account, props.updatedAt])
const clearInput = (): void => {
setInput('')
}
const handleSubmit = async (): Promise<void> => {
if (!amount) return
setSubmitting(true)
try {
await deposit({
user: props.account,
amount: BigInt(amount * 10 ** decimals),
})
setResultSubmit({
status: 'success',
value: String(amount),
symbol,
})
props.onPledge()
setInput('')
setAmount(undefined)
} catch (e) {
if (e instanceof Error) {
setResultSubmit({
status: 'error',
error: e?.message || 'An error has occurred',
})
} else {
throw e;
}
} finally {
setSubmitting(false)
}
}
return (
<div>
<h6>Choose Amount</h6>
<div className={styles.wrapper}>
<Checkbox
title={`100 ${props.symbol}`}
value={100}
isChecked={amount == 100}
setAmount={setAmount}
clearInput={clearInput}
/>
<Checkbox
title={`250 ${props.symbol}`}
value={250}
isChecked={amount == 250}
setAmount={setAmount}
clearInput={clearInput}
/>
<Checkbox
title={`500 ${props.symbol}`}
value={500}
isChecked={amount == 500}
setAmount={setAmount}
clearInput={clearInput}
/>
<Checkbox
title={`1000 ${props.symbol}`}
value={1000}
isChecked={amount == 1000}
setAmount={setAmount}
clearInput={clearInput}
/>
</div>
<div className={styles.centerContent}>
<h6>OR</h6>
</div>
<AmountInput
placeHolder="Custom amount"
setAmount={setAmount}
input={input}
setInput={setInput}
/>
<Button
title={'Back this project'}
onClick={handleSubmit}
disabled={!amount || isSubmitting}
isLoading={isSubmitting}
/>
{props.account && props.decimals && props.symbol ? (
<div>
<Spacer rem={1} />
<MintButton
account={props.account}
symbol={props.symbol}
decimals={decimals}
onComplete={() => props.onPledge()}
/>
<div className={styles.wrapper}>
<div>
<h6>Your balance: {Utils.formatAmount(balance, decimals)} {symbol}</h6>
</div>
</div>
</div>
) : null}
{resultSubmit && (
<TransactionModal
result={resultSubmit}
closeModal={() => setResultSubmit(undefined)}
/>
)}
</div>
)
}
export { FormPledge }