|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { FormGroup, FormControl, Button } from 'react-bootstrap'; |
| 4 | +import { API_BASE_URL } from '../config'; |
| 5 | +import history from '../history'; |
| 6 | + |
| 7 | +function ConductTransaction() { |
| 8 | + const [amount, setAmount] = useState(0); |
| 9 | + const [recipient, setRecipient] = useState(''); |
| 10 | + const [knownAddresses, setKnownAddresses] = useState([]); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + fetch(`${API_BASE_URL}/known-addresses`) |
| 14 | + .then(response => response.json()) |
| 15 | + .then(json => setKnownAddresses(json)); |
| 16 | + }, []); |
| 17 | + |
| 18 | + const updateRecipient = event => { |
| 19 | + setRecipient(event.target.value); |
| 20 | + } |
| 21 | + |
| 22 | + const updateAmount = event => { |
| 23 | + setAmount(Number(event.target.value)); |
| 24 | + } |
| 25 | + |
| 26 | + const submitTransaction = () => { |
| 27 | + fetch(`${API_BASE_URL}/wallet/transact`, { |
| 28 | + method: 'POST', |
| 29 | + headers: { 'Content-Type': 'application/json' }, |
| 30 | + body: JSON.stringify({ recipient, amount }) |
| 31 | + }).then(response => response.json()) |
| 32 | + .then(json => { |
| 33 | + console.log('submitTransaction json', json); |
| 34 | + |
| 35 | + alert('Success!'); |
| 36 | + |
| 37 | + history.push('/transaction-pool'); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="ConductTransaction"> |
| 43 | + <Link to="/">Home</Link> |
| 44 | + <hr /> |
| 45 | + <h3>Conduct a Transaction</h3> |
| 46 | + <br /> |
| 47 | + <FormGroup> |
| 48 | + <FormControl |
| 49 | + input="text" |
| 50 | + placeholder="recipient" |
| 51 | + value={recipient} |
| 52 | + onChange={updateRecipient} |
| 53 | + /> |
| 54 | + </FormGroup> |
| 55 | + <FormGroup> |
| 56 | + <FormControl |
| 57 | + input="number" |
| 58 | + placeholder="amount" |
| 59 | + value={amount} |
| 60 | + onChange={updateAmount} |
| 61 | + /> |
| 62 | + </FormGroup> |
| 63 | + <div> |
| 64 | + <Button |
| 65 | + variant="danger" |
| 66 | + onClick={submitTransaction} |
| 67 | + > |
| 68 | + Submit |
| 69 | + </Button> |
| 70 | + </div> |
| 71 | + <br /> |
| 72 | + <h4>Known Addresses</h4> |
| 73 | + <div> |
| 74 | + { |
| 75 | + knownAddresses.map((knownAddress, i) => ( |
| 76 | + <span key={knownAddress}> |
| 77 | + <u>{knownAddress}</u>{i !== knownAddresses.length - 1 ? ', ' : ''} |
| 78 | + </span> |
| 79 | + )) |
| 80 | + } |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +export default ConductTransaction; |
0 commit comments