Skip to content

Commit 4751817

Browse files
committed
feat(neuron-ui): call transactions controller methods with remote module
1 parent c4bc431 commit 4751817

16 files changed

Lines changed: 252 additions & 314 deletions

File tree

packages/neuron-ui/src/components/Addresses/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import { MIN_CELL_WIDTH, Routes } from 'utils/const'
2020
import { localNumberFormatter, shannonToCKBFormatter } from 'utils/formatters'
2121

2222
const Addresses = ({
23-
wallet: { id, addresses = [] },
23+
wallet: { addresses = [] },
2424
settings: { showAddressBook = false },
25-
dispatch,
2625
history,
2726
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
2827
const [t] = useTranslation()
@@ -33,17 +32,14 @@ const Addresses = ({
3332
}, [showAddressBook, history])
3433

3534
const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
36-
'address',
37-
id,
3835
useMemo(
3936
() =>
4037
addresses.map(({ address: key = '', description = '' }) => ({
4138
key,
4239
description,
4340
})),
4441
[addresses]
45-
),
46-
dispatch
42+
)
4743
)
4844

4945
const theme = getTheme()

packages/neuron-ui/src/components/History/hooks.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect } from 'react'
2-
import { AppActions } from 'states/stateProvider/reducer'
3-
import actionCreators from 'states/stateProvider/actionCreators'
2+
import { NeuronWalletActions, AppActions } from 'states/stateProvider/reducer'
43
import { queryParsers } from 'utils/parser'
4+
import { getTransactionList } from 'services/remote'
55

66
const backToTop = () => {
77
const container = document.querySelector('main') as HTMLElement
@@ -31,8 +31,16 @@ export const useSearch = (search: string = '', walletID: string = '', dispatch:
3131
type: AppActions.CleanTransactions,
3232
payload: null,
3333
})
34-
35-
dispatch(actionCreators.getTransactions({ ...params, keywords: params.keywords, walletID }))
34+
getTransactionList({ ...params, keywords: params.keywords, walletID }).then(res => {
35+
if (res.status) {
36+
dispatch({
37+
type: NeuronWalletActions.UpdateTransactionList,
38+
payload: res.result,
39+
})
40+
} else {
41+
// TODO: notification
42+
}
43+
})
3644
}, [search, walletID, dispatch])
3745
return { keywords, onKeywordsChange, setKeywords }
3846
}

packages/neuron-ui/src/components/NetworkEditor/hooks.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ export const useHandleSubmit = (
193193
},
194194
})
195195
}
196-
res = await updateNetwork(id!, {
197-
name,
198-
remote,
196+
res = await updateNetwork({
197+
networkID: id!,
198+
options: {
199+
name,
200+
remote,
201+
},
199202
})
200203
}
201204
if (res && res.status) {

packages/neuron-ui/src/components/Overview/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ import {
2121
MessageBarType,
2222
} from 'office-ui-fabric-react'
2323

24-
import { StateWithDispatch } from 'states/stateProvider/reducer'
25-
import actionCreators from 'states/stateProvider/actionCreators'
24+
import { StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer'
2625

27-
import { showErrorMessage } from 'services/remote'
26+
import { showErrorMessage, getTransactionList } from 'services/remote'
2827

2928
import { localNumberFormatter, shannonToCKBFormatter, uniformTimeFormatter as timeFormatter } from 'utils/formatters'
3029
import { PAGE_SIZE, MIN_CELL_WIDTH } from 'utils/const'
@@ -100,7 +99,18 @@ const Overview = ({
10099
const minerInfoRef = useRef<HTMLDivElement>(null)
101100

102101
useEffect(() => {
103-
dispatch(actionCreators.getTransactions({ pageNo: 1, pageSize: PAGE_SIZE, keywords: '', walletID: id }))
102+
getTransactionList({
103+
pageNo: 1,
104+
pageSize: PAGE_SIZE,
105+
keywords: '',
106+
walletID: id,
107+
}).then(res => {
108+
if (res.status) {
109+
dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result })
110+
} else {
111+
// TODO: notification
112+
}
113+
})
104114
}, [id, dispatch])
105115

106116
const onTransactionRowRender = useCallback((props?: IDetailsRowProps) => {

packages/neuron-ui/src/components/Transaction/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { RouteComponentProps } from 'react-router-dom'
33
import { useTranslation } from 'react-i18next'
44
import { Stack, DetailsList, Text, DetailsListLayoutMode, CheckboxVisibility, IColumn } from 'office-ui-fabric-react'
55

6-
import { AppActions, StateWithDispatch } from 'states/stateProvider/reducer'
7-
import actionCreators from 'states/stateProvider/actionCreators'
6+
import { AppActions, StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer'
87
import chainState from 'states/initStates/chain'
98

109
import { localNumberFormatter, uniformTimeFormatter } from 'utils/formatters'
10+
import { getTransaction } from 'services/remote'
1111

1212
const MIN_CELL_WIDTH = 70
1313

@@ -105,7 +105,16 @@ const Transaction = ({
105105
type: AppActions.CleanTransaction,
106106
payload: null,
107107
})
108-
dispatch(actionCreators.getTransaction(walletID, match.params.hash))
108+
getTransaction({ walletID, hash: match.params.hash }).then(res => {
109+
if (res.status) {
110+
dispatch({
111+
type: NeuronWalletActions.UpdateTransaction,
112+
payload: res.result,
113+
})
114+
} else {
115+
// TODO: notification
116+
}
117+
})
109118
}, [match.params.hash, dispatch, walletID])
110119

111120
const basicInfoItems = useMemo(

packages/neuron-ui/src/components/TransactionList/index.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,18 @@ const onRenderHeader = ({ group }: any) => {
5252
)
5353
}
5454

55-
const TransactionList = ({
56-
walletID,
57-
items = [],
58-
dispatch,
59-
}: {
60-
walletID: string
61-
items: State.Transaction[]
62-
dispatch: StateDispatch
63-
}) => {
55+
const TransactionList = ({ items = [] }: { walletID: string; items: State.Transaction[]; dispatch: StateDispatch }) => {
6456
const [t] = useTranslation()
6557

6658
const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
67-
'transaction',
68-
walletID,
6959
useMemo(
7060
() =>
7161
items.map(({ hash: key, description = '' }) => ({
7262
key,
7363
description,
7464
})),
7565
[items]
76-
),
77-
dispatch
66+
)
7867
)
7968

8069
const transactionColumns: IColumn[] = useMemo(

packages/neuron-ui/src/containers/Main/hooks.ts

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ import { WalletWizardPath } from 'components/WalletWizard'
44
import { NeuronWalletActions, StateDispatch, AppActions } from 'states/stateProvider/reducer'
55
import { actionCreators } from 'states/stateProvider/actionCreators'
66

7-
import UILayer, {
8-
AppMethod,
9-
ChainMethod,
10-
TransactionsMethod,
11-
WalletsMethod,
12-
walletsCall,
13-
transactionsCall,
14-
} from 'services/UILayer'
15-
import { initWindow } from 'services/remote'
7+
import UILayer, { AppMethod, ChainMethod, WalletsMethod, walletsCall } from 'services/UILayer'
8+
import { initWindow, getTransactionList, getTransaction } from 'services/remote'
169
import {
1710
SystemScript as SystemScriptSubject,
1811
DataUpdate as DataUpdateSubject,
@@ -95,71 +88,6 @@ export const useChannelListeners = ({
9588
}
9689
})
9790

98-
UILayer.on(Channel.Transactions, (_e: Event, method: TransactionsMethod, args: ChannelResponse<any>) => {
99-
if (args.status) {
100-
switch (method) {
101-
case TransactionsMethod.GetAllByKeywords: {
102-
// TODO: verify the wallet id the transactions belong to
103-
dispatch({
104-
type: NeuronWalletActions.Chain,
105-
payload: { transactions: { ...chain.transactions, ...args.result } },
106-
})
107-
break
108-
}
109-
case TransactionsMethod.Get: {
110-
dispatch({
111-
type: NeuronWalletActions.Chain,
112-
payload: { transaction: args.result },
113-
})
114-
break
115-
}
116-
case TransactionsMethod.TransactionUpdated: {
117-
const updatedTransaction: State.Transaction = args.result
118-
if (
119-
(!chain.transactions.items.length ||
120-
updatedTransaction.timestamp === null ||
121-
+(updatedTransaction.timestamp || updatedTransaction.createdAt) >
122-
+(chain.transactions.items[0].timestamp || chain.transactions.items[0].createdAt)) &&
123-
chain.transactions.pageNo === 1
124-
) {
125-
/**
126-
* 1. transaction list is empty or the coming transaction is pending or the coming transaction is later than latest transaction in current list
127-
* 2. the current page number is 1
128-
*/
129-
const newTransactionItems = [updatedTransaction, ...chain.transactions.items].slice(
130-
0,
131-
chain.transactions.pageSize
132-
)
133-
dispatch({
134-
type: NeuronWalletActions.Chain,
135-
payload: { transactions: { ...chain.transactions, items: newTransactionItems } },
136-
})
137-
} else {
138-
const newTransactionItems = [...chain.transactions.items]
139-
const idx = newTransactionItems.findIndex(item => item.hash === updatedTransaction.hash)
140-
if (idx >= 0) {
141-
newTransactionItems[idx] = updatedTransaction
142-
dispatch({
143-
type: NeuronWalletActions.Chain,
144-
payload: { transactions: { ...chain.transactions, items: newTransactionItems } },
145-
})
146-
}
147-
}
148-
if (chain.transaction.hash === updatedTransaction.hash) {
149-
dispatch({
150-
type: NeuronWalletActions.Chain,
151-
payload: { transaction: updatedTransaction },
152-
})
153-
}
154-
break
155-
}
156-
default: {
157-
break
158-
}
159-
}
160-
}
161-
})
162-
16391
UILayer.on(Channel.Wallets, (_e: Event, method: WalletsMethod, args: ChannelResponse<any>) => {
16492
if (args.status) {
16593
switch (method) {
@@ -334,11 +262,18 @@ export const useOnCurrentWalletChange = ({
334262
useEffect(() => {
335263
if (walletID) {
336264
walletsCall.getAllAddresses(walletID)
337-
transactionsCall.getAllByKeywords({
265+
getTransactionList({
338266
walletID,
339267
keywords: '',
340268
pageNo,
341269
pageSize,
270+
}).then(res => {
271+
if (res.status) {
272+
dispatch({
273+
type: NeuronWalletActions.UpdateTransactionList,
274+
payload: res.result,
275+
})
276+
}
342277
})
343278
} else {
344279
initWindow()
@@ -387,13 +322,19 @@ export const useSubscription = ({
387322
break
388323
}
389324
case 'transaction': {
390-
transactionsCall.getAllByKeywords({
325+
getTransactionList({
391326
walletID,
392327
keywords,
393328
pageNo,
394329
pageSize,
330+
}).then(res => {
331+
if (res.status) {
332+
dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result })
333+
} else {
334+
// TODO: notification
335+
}
395336
})
396-
transactionsCall.get(walletID, txHash)
337+
getTransaction({ walletID, hash: txHash })
397338
break
398339
}
399340
case 'wallet': {

packages/neuron-ui/src/services/UILayer.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,10 @@ export enum WalletsMethod {
3535
GetAllAddresses = 'getAllAddresses',
3636
}
3737

38-
export enum TransactionsMethod {
39-
GetAll = 'getAll',
40-
GetAllByKeywords = 'getAllByKeywords',
41-
Get = 'get',
42-
UpdateDescription = 'updateDescription',
43-
TransactionUpdated = 'transactionUpdated',
44-
}
45-
4638
export enum HelpersMethod {
4739
GenerateMnemonic = 'generateMnemonic',
4840
}
4941

50-
export interface GetTransactionsParams {
51-
pageNo: number
52-
pageSize: number
53-
keywords?: string
54-
walletID: string
55-
}
56-
5742
const UILayer = (() => {
5843
if (window.bridge) {
5944
return new SyntheticEventEmitter(window.bridge.ipcRenderer)
@@ -87,16 +72,6 @@ export const appCalls = instantiateMethodCall(app) as {
8772
handleViewError: (errorMessage: string) => void
8873
}
8974

90-
export const transactions = (method: TransactionsMethod, params: string | GetTransactionsParams) => {
91-
UILayer.send(Channel.Transactions, method, params)
92-
}
93-
94-
export const transactionsCall = instantiateMethodCall(transactions) as {
95-
getAllByKeywords: (params: GetTransactionsParams) => void
96-
get: (walletID: string, hash: string) => void
97-
updateDescription: (params: { hash: string; description: string }) => void
98-
}
99-
10075
export const wallets = (
10176
method: WalletsMethod,
10277
params:

0 commit comments

Comments
 (0)