|
1 | 1 | import React, { useState } from 'react' |
2 | | -import { Flex, Box, Type } from 'blockstack-ui' |
3 | | -import { address, networks } from 'bitcoinjs-lib' |
4 | | -import * as c32Check from 'c32check' |
5 | | -import Notification from '../notification' |
6 | | - |
| 2 | +import { validateBTC, validateSTX, savePaymentDetails } from './helpers' |
7 | 3 | import { |
8 | | - MakerCardHeader, |
9 | | - MakerCardText, |
10 | | - MakerButton, |
11 | | - MakerField |
12 | | -} from '../styled' |
13 | | - |
14 | | -const validateBTC = (addr) => { |
15 | | - try { |
16 | | - address.toOutputScript(addr, networks.bitcoin) |
17 | | - return true |
18 | | - } catch (error) { |
19 | | - return false |
20 | | - } |
21 | | -} |
22 | | - |
23 | | -const validateSTX = (addr) => { |
24 | | - try { |
25 | | - c32Check.c32addressDecode(addr) |
26 | | - return true |
27 | | - } catch (error) { |
28 | | - return false |
29 | | - } |
30 | | -} |
| 4 | + PaymentContainer, |
| 5 | + PaymentHeader, |
| 6 | + PaymentDescription, |
| 7 | + PaymentHelpText, |
| 8 | + PaymentBtcField, |
| 9 | + PaymentStxField, |
| 10 | + PaymentButton |
| 11 | +} from './content' |
31 | 12 |
|
32 | | -const PaymentDetails = ({ app, apiServer, accessToken, user }) => { |
| 13 | +const PaymentDetails = ({ app, apiServer, accessToken, user, onPaymentDetailsComplete }) => { |
33 | 14 | const [btcAddress, setBTCAddress] = useState(app.BTCAddress) |
34 | 15 | const [stxAddress, setSTXAddress] = useState(app.stacksAddress) |
35 | | - const [showNotification, setShowNotification] = useState(false) |
36 | | - const [btcValid, setBtcValid] = useState(true) |
37 | | - const [stxValid, setStxValid] = useState(true) |
38 | 16 | const [saving, setSaving] = useState(false) |
| 17 | + const [hasAttemptedSaved, setHasAttemptedSaved] = useState(false) |
| 18 | + const [savedValues, setSavedValue] = useState({ btcAddress, stxAddress }) |
39 | 19 |
|
40 | | - const notify = () => { |
41 | | - setShowNotification(true) |
42 | | - setTimeout(() => { |
43 | | - setShowNotification(false) |
44 | | - }, 10000) |
45 | | - } |
| 20 | + const isSaved = ( |
| 21 | + btcAddress === savedValues.btcAddress && |
| 22 | + btcAddress !== '' && |
| 23 | + stxAddress === savedValues.stxAddress && |
| 24 | + stxAddress !== '' |
| 25 | + ) |
46 | 26 |
|
47 | 27 | const save = async () => { |
48 | | - let isValid = true |
49 | | - if (!validateBTC(btcAddress)) { |
50 | | - isValid = false |
51 | | - setBtcValid(false) |
52 | | - } |
53 | | - if (!validateSTX(stxAddress)) { |
54 | | - isValid = false |
55 | | - setStxValid(false) |
56 | | - } |
57 | | - if (!isValid) { |
58 | | - return |
59 | | - } else { |
60 | | - setBtcValid(true) |
61 | | - setStxValid(true) |
62 | | - } |
| 28 | + setHasAttemptedSaved(true) |
| 29 | + if (!validateBTC(btcAddress) || !validateSTX(stxAddress)) return |
63 | 30 | setSaving(true) |
64 | | - console.log(stxAddress) |
65 | | - const response = await fetch(`${apiServer}/api/maker/apps?appId=${app.id}`, { |
66 | | - method: 'POST', |
67 | | - headers: new Headers({ |
68 | | - 'Content-Type': 'application/json', |
69 | | - authorization: `Bearer ${user.jwt}` |
70 | | - }), |
71 | | - body: JSON.stringify({ |
72 | | - BTCAddress: btcAddress, |
73 | | - stacksAddress: stxAddress |
74 | | - }) |
75 | | - }) |
76 | | - await response.json() |
77 | | - notify() |
| 31 | + await savePaymentDetails({ apiServer, appId: app.id, jwt: user.jwt, btcAddress, stxAddress }) |
78 | 32 | setSaving(false) |
| 33 | + setSavedValue({ btcAddress, stxAddress }) |
| 34 | + onPaymentDetailsComplete() |
| 35 | + } |
| 36 | + |
| 37 | + const buttonText = () => { |
| 38 | + if (saving) return 'Saving…' |
| 39 | + if (isSaved) return 'Saved' |
| 40 | + return 'Save' |
| 41 | + } |
| 42 | + |
| 43 | + const createInputError = ({ validateFn, currencySymbol }) => addressHash => { |
| 44 | + if (!hasAttemptedSaved) return null |
| 45 | + if (!validateFn(addressHash)) return `Please enter a valid ${currencySymbol} address` |
| 46 | + return null |
79 | 47 | } |
| 48 | + const getBtcError = createInputError({ validateFn: validateBTC, currencySymbol: 'BTC' }) |
| 49 | + const getStxError = createInputError({ validateFn: validateSTX, currencySymbol: 'STX' }) |
80 | 50 |
|
81 | 51 | return ( |
82 | | - <Flex> |
83 | | - <Box width={1} mt={0}> |
84 | | - <MakerCardHeader>Payment details</MakerCardHeader> |
85 | | - {showNotification && <Notification message="Thanks! Your payment details have been updated." />} |
86 | | - <MakerCardText mb={5} mt={0}> |
87 | | - This is where you will receive your App Mining payments. |
88 | | - Currently, payments are made in Bitcoin (BTC). Payments will be made |
89 | | - in Stacks (STX) in the future. |
90 | | - </MakerCardText> |
91 | | - <MakerField |
92 | | - name="btcAddress" |
93 | | - label="Bitcoin Address" |
94 | | - placeholder="Enter a Bitcoin address" |
95 | | - onChange={(e) => setBTCAddress(e.target.value)} |
96 | | - value={btcAddress || ''} |
97 | | - error={!btcValid ? 'Please enter a valid BTC address' : null} |
98 | | - /> |
99 | | - <MakerField |
100 | | - name="stacksAddress" |
101 | | - label="Stacks Address" |
102 | | - placeholder="Enter a Stacks address" |
103 | | - onChange={(e) => setSTXAddress(e.target.value)} |
104 | | - value={stxAddress || ''} |
105 | | - error={!stxValid ? 'Please enter a valid STX address' : null} |
106 | | - /> |
107 | | - <Type.p fontSize={12} mt={0} display="block"> |
108 | | - {"Don't"} have a Stacks address? <a href="https://wallet.blockstack.org" target="_blank" rel="noopener noreferrer">Download the Stacks wallet to get one</a> |
109 | | - </Type.p> |
110 | | - <MakerButton mt={4} disabled={saving} onClick={() => save({ btcAddress, stxAddress, apiServer, accessToken })}> |
111 | | - {saving ? 'Saving...' : 'Save'} |
112 | | - </MakerButton> |
113 | | - </Box> |
114 | | - </Flex> |
| 52 | + <PaymentContainer> |
| 53 | + <PaymentHeader>Payment details</PaymentHeader> |
| 54 | + <PaymentDescription /> |
| 55 | + <PaymentBtcField |
| 56 | + onChange={e => setBTCAddress(e.target.value)} |
| 57 | + value={btcAddress || ''} |
| 58 | + error={getBtcError(btcAddress)} |
| 59 | + /> |
| 60 | + <PaymentStxField |
| 61 | + onChange={e => setSTXAddress(e.target.value)} |
| 62 | + value={stxAddress || ''} |
| 63 | + error={getStxError(stxAddress)} |
| 64 | + /> |
| 65 | + <PaymentHelpText/> |
| 66 | + <PaymentButton |
| 67 | + disabled={isSaved || saving} |
| 68 | + onClick={() => save({ btcAddress, stxAddress, apiServer, accessToken })} |
| 69 | + > |
| 70 | + {buttonText()} |
| 71 | + </PaymentButton> |
| 72 | + </PaymentContainer> |
115 | 73 | ) |
116 | 74 | } |
117 | 75 |
|
|
0 commit comments