on master help sign in
Merge pull request #167 from urbit/feat/extract-txcursor

Extract txcursor

commit80d720cfb1b0c4d0657513f615bd825fef7452c4 authorMatt Condon <mattgcondon@gmail.com> dateMon 17 Jun 2019, 15:40 (7 years ago) parent7a2392975ca17b
4 files changed+46−22
src/bridge/Bridge.js+3−16 src/bridge/components/StatelessTransaction.js+4−2 src/bridge/store/txnCursor.js+35 src/bridge/views/SentTransaction.js+4−4
src/bridge/Bridge.js+3−16
@@ -19,6 +19,7 @@ import { NetworkProvider } from './store/network';
1919import { WalletProvider } from './store/wallet';
2020import { PointCursorProvider } from './store/pointCursor';
2121import { PointCacheProvider } from './store/pointCache';
22+import { TxnCursorProvider } from './store/txnCursor';
2223
2324const kInitialNetworkType = isDevelopment
2425 ? NETWORK_TYPES.LOCAL
@@ -88,6 +89,7 @@ const AllProviders = nest([
8889 WalletProvider,
8990 PointCursorProvider,
9091 PointCacheProvider,
92+ TxnCursorProvider,
9193]);
9294
9395class Bridge extends React.Component {
@@ -96,12 +98,8 @@ class Bridge extends React.Component {
9698
9799 this.state = {
98100 networkSeedCache: null,
99− // txn
100− txnHashCursor: Nothing(),
101− txnConfirmations: {},
102101 };
103102
104− this.setTxnHashCursor = this.setTxnHashCursor.bind(this);
105103 this.setNetworkSeedCache = this.setNetworkSeedCache.bind(this);
106104 }
107105
@@ -112,16 +110,8 @@ class Bridge extends React.Component {
112110 });
113111 }
114112
115− setTxnHashCursor(txnHashCursor) {
116− this.setState({ txnHashCursor });
117− }
118−
119113 render() {
120− const {
121− networkSeedCache,
122− networkRevisionCache,
123− txnHashCursor,
124− } = this.state;
114+ const { networkSeedCache, networkRevisionCache } = this.state;
125115
126116 return (
127117 <AllProviders
@@ -140,9 +130,6 @@ class Bridge extends React.Component {
140130 networkSeedCache={networkSeedCache}
141131 networkRevisionCache={networkRevisionCache}
142132 setNetworkSeedCache={this.setNetworkSeedCache}
143− onSent={this.setTxnHashCursor}
144− setTxnHashCursor={this.setTxnHashCursor}
145− txnHashCursor={txnHashCursor}
146133 />
147134
148135 <div className={'push'} />
src/bridge/components/StatelessTransaction.js+4−2
@@ -25,6 +25,7 @@ import { withTxnConfirmations } from '../store/txnConfirmations';
2525import { withNetwork } from '../store/network';
2626import { withHistory } from '../store/history';
2727import { withWallet } from '../store/wallet';
28+import { withTxnCursor } from '../store/txnCursor';
2829
2930const SUBMISSION_STATES = {
3031 PROMPT: 'Send transaction',
@@ -238,7 +239,7 @@ class StatelessTransaction extends React.Component {
238239 props.setTxnConfirmations
239240 )
240241 .then(txHash => {
241− props.onSent(Just(Ok(txHash)), state.stx.value);
242+ props.setTxnCursor(Just(Ok(txHash)));
242243
243244 let routeData = {};
244245 if (props.networkSeed) {
@@ -586,5 +587,6 @@ export default compose(
586587 withNetwork,
587588 withTxnConfirmations,
588589 withHistory,
589− withWallet
590+ withWallet,
591+ withTxnCursor
590592)(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';
1111import { useTxnConfirmations } from '../store/txnConfirmations';
1212import { useNetwork } from '../store/network';
1313import { usePointCursor } from '../store/pointCursor';
14+import { useTxnCursor } from '../store/txnCursor';
1415
1516class Success extends React.Component {
1617 constructor(props) {
@@ -115,16 +116,15 @@ function SentTransaction(props) {
115116 const history = useHistory();
116117 const { txnConfirmations } = useTxnConfirmations();
117118 const { setPointCursor, pointCursor } = usePointCursor();
118− const { web3 } = useNetwork();
119−
120− const { txnHashCursor, networkType } = props;
119+ const { web3, networkType } = useNetwork();
120+ const { txnCursor } = useTxnCursor();
121121
122122 const promptKeyfile = history.data && history.data.promptKeyfile;
123123
124124 // TODO: remove need's assumption about accepting the props object
125125 const w3 = need.web3({ web3 });
126126
127− const result = txnHashCursor.matchWith({
127+ const result = txnCursor.matchWith({
128128 Nothing: _ => {
129129 throw BRIDGE_ERROR.MISSING_TXN;
130130 },