-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.js
More file actions
100 lines (82 loc) · 2.68 KB
/
Copy pathOrder.js
File metadata and controls
100 lines (82 loc) · 2.68 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
import { useState, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux'
import { makeBuyOrder, makeSellOrder } from '../store/interactions'
const Order = () => {
const [isBuy, setIsBuy] = useState(true)
const [amount, setAmount] = useState(0)
const [price, setPrice] = useState(0)
const provider = useSelector(state => state.provider.connection)
const tokens = useSelector(state => state.tokens.contracts)
const exchange = useSelector(state => state.exchange.contract)
const dispatch = useDispatch()
const buyRef = useRef(null)
const sellRef = useRef(null)
const tabHandler = (e) => {
if(e.target.className !== buyRef.current.className) {
e.target.className = 'tab tab--active'
buyRef.current.className = 'tab'
setIsBuy(false)
} else {
e.target.className = 'tab tab--active'
sellRef.current.className = 'tab'
setIsBuy(true)
}
}
const buyHandler = (e) => {
e.preventDefault()
makeBuyOrder(provider, exchange, tokens, { amount, price }, dispatch)
setAmount(0)
setPrice(0)
}
const sellHandler = (e) => {
e.preventDefault()
makeSellOrder(provider, exchange, tokens, { amount, price }, dispatch)
setAmount(0)
setPrice(0)
}
return (
<div className="component exchange__orders">
<div className='component__header flex-between'>
<h2>New Order</h2>
<div className='tabs'>
<button onClick={tabHandler} ref={buyRef} className='tab tab--active'>Buy</button>
<button onClick={tabHandler} ref={sellRef} className='tab'>Sell</button>
</div>
</div>
<form onSubmit={ isBuy ? buyHandler : sellHandler }>
{isBuy ? (
<label htmlFor="amount">Buy Amount</label>
) : (
<label htmlFor="amount">Sell Amount</label>
)}
<input
type="text"
id='amount'
placeholder='0.0000'
value={amount === 0 ? '' : amount}
onChange={(e) => setAmount(e.target.value)}
/>
{isBuy ? (
<label htmlFor="price">Buy Price</label>
) : (
<label htmlFor="price">Sell Price</label>
)}
<input
type="text"
id='price'
placeholder='0.0000'
value={price === 0 ? '' : price}
onChange={(e) => setPrice(e.target.value)}
/>
<button className='button button--filled' type='submit'>
{isBuy ? (
<span>Buy Order</span>
) : (
<span>Sell Order</span>
)}
</button>
</form>
</div>
);
}
export default Order;