on master help sign in
Merge pull request #167 from urbit/feat/extract-txcursor
80d720cMatt Condon
src/bridge/Bridge.js+3−16
@@ -19,6 +19,7 @@ import { NetworkProvider } from './store/network';
19import { WalletProvider } from './store/wallet';
19import { WalletProvider } from './store/wallet';
20import { PointCursorProvider } from './store/pointCursor';
20import { PointCursorProvider } from './store/pointCursor';
21import { PointCacheProvider } from './store/pointCache';
21import { PointCacheProvider } from './store/pointCache';
22+import { TxnCursorProvider } from './store/txnCursor';
22
23
23const kInitialNetworkType = isDevelopment
24const kInitialNetworkType = isDevelopment
24 ? NETWORK_TYPES.LOCAL
25 ? NETWORK_TYPES.LOCAL
@@ -88,6 +89,7 @@ const AllProviders = nest([
88 WalletProvider,
89 WalletProvider,
89 PointCursorProvider,
90 PointCursorProvider,
90 PointCacheProvider,
91 PointCacheProvider,
92+ TxnCursorProvider,
91]);
93]);
92
94
93class Bridge extends React.Component {
95class Bridge extends React.Component {
@@ -96,12 +98,8 @@ class Bridge extends React.Component {
96
98
97 this.state = {
99 this.state = {
98 networkSeedCache: null,
100 networkSeedCache: null,
99− // txn
100− txnHashCursor: Nothing(),
101− txnConfirmations: {},
102 };
101 };
103
102
104− this.setTxnHashCursor = this.setTxnHashCursor.bind(this);
105 this.setNetworkSeedCache = this.setNetworkSeedCache.bind(this);
103 this.setNetworkSeedCache = this.setNetworkSeedCache.bind(this);
106 }
104 }
107
105
@@ -112,16 +110,8 @@ class Bridge extends React.Component {
112 });
110 });
113 }
111 }
114
112
115− setTxnHashCursor(txnHashCursor) {
116− this.setState({ txnHashCursor });
117− }
118−
119 render() {
113 render() {
120− const {
121− networkSeedCache,
122− networkRevisionCache,
114+ const { networkSeedCache, networkRevisionCache } = this.state;
123− txnHashCursor,
124− } = this.state;
125
115
126 return (
116 return (
127 <AllProviders
117 <AllProviders
@@ -140,9 +130,6 @@ class Bridge extends React.Component {
140 networkSeedCache={networkSeedCache}
130 networkSeedCache={networkSeedCache}
141 networkRevisionCache={networkRevisionCache}
131 networkRevisionCache={networkRevisionCache}
142 setNetworkSeedCache={this.setNetworkSeedCache}
132 setNetworkSeedCache={this.setNetworkSeedCache}
143− onSent={this.setTxnHashCursor}
144− setTxnHashCursor={this.setTxnHashCursor}
145− txnHashCursor={txnHashCursor}
146 />
133 />
147
134
148 <div className={'push'} />
135 <div className={'push'} />
src/bridge/components/StatelessTransaction.js+4−2
@@ -25,6 +25,7 @@ import { withTxnConfirmations } from '../store/txnConfirmations';
25import { withNetwork } from '../store/network';
25import { withNetwork } from '../store/network';
26import { withHistory } from '../store/history';
26import { withHistory } from '../store/history';
27import { withWallet } from '../store/wallet';
27import { withWallet } from '../store/wallet';
28+import { withTxnCursor } from '../store/txnCursor';
28
29
29const SUBMISSION_STATES = {
30const SUBMISSION_STATES = {
30 PROMPT: 'Send transaction',
31 PROMPT: 'Send transaction',
@@ -238,7 +239,7 @@ class StatelessTransaction extends React.Component {
238 props.setTxnConfirmations
239 props.setTxnConfirmations
239 )
240 )
240 .then(txHash => {
241 .then(txHash => {
241− props.onSent(Just(Ok(txHash)), state.stx.value);
242+ props.setTxnCursor(Just(Ok(txHash)));
242
243
243 let routeData = {};
244 let routeData = {};
244 if (props.networkSeed) {
245 if (props.networkSeed) {
@@ -586,5 +587,6 @@ export default compose(
586 withNetwork,
587 withNetwork,
587 withTxnConfirmations,
588 withTxnConfirmations,
588 withHistory,
589 withHistory,
589− withWallet
590+ withWallet,
591+ withTxnCursor
590)(StatelessTransaction);
592)(StatelessTransaction);
src/bridge/store/txnCursor.jsnew file+35
@@ -0,0 +1,35 @@
1+import React, { createContext, forwardRef, useContext, useState } from 'react';
2+
3+export const TxnCursorContext = createContext(null);
4+
5+function _useTxnCursor() {
6+ const [txnCursor, setTxnCursor] = useState({});
7+
8+ return {
9+ txnCursor,
10+ setTxnCursor,
11+ };
12+}
13+
14+export function TxnCursorProvider({ children }) {
15+ const txnCursor = _useTxnCursor();
16+
17+ return (
18+ <TxnCursorContext.Provider value={txnCursor}>
19+ {children}
20+ </TxnCursorContext.Provider>
21+ );
22+}
23+
24+// Hook version
25+export function useTxnCursor() {
26+ return useContext(TxnCursorContext);
27+}
28+
29+// HOC version
30+export const withTxnCursor = Component =>
31+ forwardRef((props, ref) => (
32+ <TxnCursorContext.Consumer>
33+ {txnCursor => <Component ref={ref} {...txnCursor} {...props} />}
34+ </TxnCursorContext.Consumer>
35+ ));
src/bridge/views/SentTransaction.js+4−4
@@ -11,6 +11,7 @@ import { NETWORK_TYPES } from '../lib/network';
11import { useTxnConfirmations } from '../store/txnConfirmations';
11import { useTxnConfirmations } from '../store/txnConfirmations';
12import { useNetwork } from '../store/network';
12import { useNetwork } from '../store/network';
13import { usePointCursor } from '../store/pointCursor';
13import { usePointCursor } from '../store/pointCursor';
14+import { useTxnCursor } from '../store/txnCursor';
14
15
15class Success extends React.Component {
16class Success extends React.Component {
16 constructor(props) {
17 constructor(props) {
@@ -115,16 +116,15 @@ function SentTransaction(props) {
115 const history = useHistory();
116 const history = useHistory();
116 const { txnConfirmations } = useTxnConfirmations();
117 const { txnConfirmations } = useTxnConfirmations();
117 const { setPointCursor, pointCursor } = usePointCursor();
118 const { setPointCursor, pointCursor } = usePointCursor();
118− const { web3 } = useNetwork();
119+ const { web3, networkType } = useNetwork();
119−
120+ const { txnCursor } = useTxnCursor();
120− const { txnHashCursor, networkType } = props;
121
121
122 const promptKeyfile = history.data && history.data.promptKeyfile;
122 const promptKeyfile = history.data && history.data.promptKeyfile;
123
123
124 // TODO: remove need's assumption about accepting the props object
124 // TODO: remove need's assumption about accepting the props object
125 const w3 = need.web3({ web3 });
125 const w3 = need.web3({ web3 });
126
126
127− const result = txnHashCursor.matchWith({
127+ const result = txnCursor.matchWith({
128 Nothing: _ => {
128 Nothing: _ => {
129 throw BRIDGE_ERROR.MISSING_TXN;
129 throw BRIDGE_ERROR.MISSING_TXN;
130 },
130 },