Skip to content

Commit

Permalink
feat(neuron-ui): call transactions controller methods with remote module
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 25, 2019
1 parent c4bc431 commit 4751817
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 314 deletions.
8 changes: 2 additions & 6 deletions packages/neuron-ui/src/components/Addresses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import { MIN_CELL_WIDTH, Routes } from 'utils/const'
import { localNumberFormatter, shannonToCKBFormatter } from 'utils/formatters'

const Addresses = ({
wallet: { id, addresses = [] },
wallet: { addresses = [] },
settings: { showAddressBook = false },
dispatch,
history,
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
const [t] = useTranslation()
Expand All @@ -33,17 +32,14 @@ const Addresses = ({
}, [showAddressBook, history])

const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
'address',
id,
useMemo(
() =>
addresses.map(({ address: key = '', description = '' }) => ({
key,
description,
})),
[addresses]
),
dispatch
)
)

const theme = getTheme()
Expand Down
16 changes: 12 additions & 4 deletions packages/neuron-ui/src/components/History/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react'
import { AppActions } from 'states/stateProvider/reducer'
import actionCreators from 'states/stateProvider/actionCreators'
import { NeuronWalletActions, AppActions } from 'states/stateProvider/reducer'
import { queryParsers } from 'utils/parser'
import { getTransactionList } from 'services/remote'

const backToTop = () => {
const container = document.querySelector('main') as HTMLElement
Expand Down Expand Up @@ -31,8 +31,16 @@ export const useSearch = (search: string = '', walletID: string = '', dispatch:
type: AppActions.CleanTransactions,
payload: null,
})

dispatch(actionCreators.getTransactions({ ...params, keywords: params.keywords, walletID }))
getTransactionList({ ...params, keywords: params.keywords, walletID }).then(res => {
if (res.status) {
dispatch({
type: NeuronWalletActions.UpdateTransactionList,
payload: res.result,
})
} else {
// TODO: notification
}
})
}, [search, walletID, dispatch])
return { keywords, onKeywordsChange, setKeywords }
}
Expand Down
9 changes: 6 additions & 3 deletions packages/neuron-ui/src/components/NetworkEditor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ export const useHandleSubmit = (
},
})
}
res = await updateNetwork(id!, {
name,
remote,
res = await updateNetwork({
networkID: id!,
options: {
name,
remote,
},
})
}
if (res && res.status) {
Expand Down
18 changes: 14 additions & 4 deletions packages/neuron-ui/src/components/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import {
MessageBarType,
} from 'office-ui-fabric-react'

import { StateWithDispatch } from 'states/stateProvider/reducer'
import actionCreators from 'states/stateProvider/actionCreators'
import { StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer'

import { showErrorMessage } from 'services/remote'
import { showErrorMessage, getTransactionList } from 'services/remote'

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

useEffect(() => {
dispatch(actionCreators.getTransactions({ pageNo: 1, pageSize: PAGE_SIZE, keywords: '', walletID: id }))
getTransactionList({
pageNo: 1,
pageSize: PAGE_SIZE,
keywords: '',
walletID: id,
}).then(res => {
if (res.status) {
dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result })
} else {
// TODO: notification
}
})
}, [id, dispatch])

const onTransactionRowRender = useCallback((props?: IDetailsRowProps) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/neuron-ui/src/components/Transaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { RouteComponentProps } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { Stack, DetailsList, Text, DetailsListLayoutMode, CheckboxVisibility, IColumn } from 'office-ui-fabric-react'

import { AppActions, StateWithDispatch } from 'states/stateProvider/reducer'
import actionCreators from 'states/stateProvider/actionCreators'
import { AppActions, StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer'
import chainState from 'states/initStates/chain'

import { localNumberFormatter, uniformTimeFormatter } from 'utils/formatters'
import { getTransaction } from 'services/remote'

const MIN_CELL_WIDTH = 70

Expand Down Expand Up @@ -105,7 +105,16 @@ const Transaction = ({
type: AppActions.CleanTransaction,
payload: null,
})
dispatch(actionCreators.getTransaction(walletID, match.params.hash))
getTransaction({ walletID, hash: match.params.hash }).then(res => {
if (res.status) {
dispatch({
type: NeuronWalletActions.UpdateTransaction,
payload: res.result,
})
} else {
// TODO: notification
}
})
}, [match.params.hash, dispatch, walletID])

const basicInfoItems = useMemo(
Expand Down
15 changes: 2 additions & 13 deletions packages/neuron-ui/src/components/TransactionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,18 @@ const onRenderHeader = ({ group }: any) => {
)
}

const TransactionList = ({
walletID,
items = [],
dispatch,
}: {
walletID: string
items: State.Transaction[]
dispatch: StateDispatch
}) => {
const TransactionList = ({ items = [] }: { walletID: string; items: State.Transaction[]; dispatch: StateDispatch }) => {
const [t] = useTranslation()

const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
'transaction',
walletID,
useMemo(
() =>
items.map(({ hash: key, description = '' }) => ({
key,
description,
})),
[items]
),
dispatch
)
)

const transactionColumns: IColumn[] = useMemo(
Expand Down
95 changes: 18 additions & 77 deletions packages/neuron-ui/src/containers/Main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import { WalletWizardPath } from 'components/WalletWizard'
import { NeuronWalletActions, StateDispatch, AppActions } from 'states/stateProvider/reducer'
import { actionCreators } from 'states/stateProvider/actionCreators'

import UILayer, {
AppMethod,
ChainMethod,
TransactionsMethod,
WalletsMethod,
walletsCall,
transactionsCall,
} from 'services/UILayer'
import { initWindow } from 'services/remote'
import UILayer, { AppMethod, ChainMethod, WalletsMethod, walletsCall } from 'services/UILayer'
import { initWindow, getTransactionList, getTransaction } from 'services/remote'
import {
SystemScript as SystemScriptSubject,
DataUpdate as DataUpdateSubject,
Expand Down Expand Up @@ -95,71 +88,6 @@ export const useChannelListeners = ({
}
})

UILayer.on(Channel.Transactions, (_e: Event, method: TransactionsMethod, args: ChannelResponse<any>) => {
if (args.status) {
switch (method) {
case TransactionsMethod.GetAllByKeywords: {
// TODO: verify the wallet id the transactions belong to
dispatch({
type: NeuronWalletActions.Chain,
payload: { transactions: { ...chain.transactions, ...args.result } },
})
break
}
case TransactionsMethod.Get: {
dispatch({
type: NeuronWalletActions.Chain,
payload: { transaction: args.result },
})
break
}
case TransactionsMethod.TransactionUpdated: {
const updatedTransaction: State.Transaction = args.result
if (
(!chain.transactions.items.length ||
updatedTransaction.timestamp === null ||
+(updatedTransaction.timestamp || updatedTransaction.createdAt) >
+(chain.transactions.items[0].timestamp || chain.transactions.items[0].createdAt)) &&
chain.transactions.pageNo === 1
) {
/**
* 1. transaction list is empty or the coming transaction is pending or the coming transaction is later than latest transaction in current list
* 2. the current page number is 1
*/
const newTransactionItems = [updatedTransaction, ...chain.transactions.items].slice(
0,
chain.transactions.pageSize
)
dispatch({
type: NeuronWalletActions.Chain,
payload: { transactions: { ...chain.transactions, items: newTransactionItems } },
})
} else {
const newTransactionItems = [...chain.transactions.items]
const idx = newTransactionItems.findIndex(item => item.hash === updatedTransaction.hash)
if (idx >= 0) {
newTransactionItems[idx] = updatedTransaction
dispatch({
type: NeuronWalletActions.Chain,
payload: { transactions: { ...chain.transactions, items: newTransactionItems } },
})
}
}
if (chain.transaction.hash === updatedTransaction.hash) {
dispatch({
type: NeuronWalletActions.Chain,
payload: { transaction: updatedTransaction },
})
}
break
}
default: {
break
}
}
}
})

UILayer.on(Channel.Wallets, (_e: Event, method: WalletsMethod, args: ChannelResponse<any>) => {
if (args.status) {
switch (method) {
Expand Down Expand Up @@ -334,11 +262,18 @@ export const useOnCurrentWalletChange = ({
useEffect(() => {
if (walletID) {
walletsCall.getAllAddresses(walletID)
transactionsCall.getAllByKeywords({
getTransactionList({
walletID,
keywords: '',
pageNo,
pageSize,
}).then(res => {
if (res.status) {
dispatch({
type: NeuronWalletActions.UpdateTransactionList,
payload: res.result,
})
}
})
} else {
initWindow()
Expand Down Expand Up @@ -387,13 +322,19 @@ export const useSubscription = ({
break
}
case 'transaction': {
transactionsCall.getAllByKeywords({
getTransactionList({
walletID,
keywords,
pageNo,
pageSize,
}).then(res => {
if (res.status) {
dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result })
} else {
// TODO: notification
}
})
transactionsCall.get(walletID, txHash)
getTransaction({ walletID, hash: txHash })
break
}
case 'wallet': {
Expand Down
25 changes: 0 additions & 25 deletions packages/neuron-ui/src/services/UILayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,10 @@ export enum WalletsMethod {
GetAllAddresses = 'getAllAddresses',
}

export enum TransactionsMethod {
GetAll = 'getAll',
GetAllByKeywords = 'getAllByKeywords',
Get = 'get',
UpdateDescription = 'updateDescription',
TransactionUpdated = 'transactionUpdated',
}

export enum HelpersMethod {
GenerateMnemonic = 'generateMnemonic',
}

export interface GetTransactionsParams {
pageNo: number
pageSize: number
keywords?: string
walletID: string
}

const UILayer = (() => {
if (window.bridge) {
return new SyntheticEventEmitter(window.bridge.ipcRenderer)
Expand Down Expand Up @@ -87,16 +72,6 @@ export const appCalls = instantiateMethodCall(app) as {
handleViewError: (errorMessage: string) => void
}

export const transactions = (method: TransactionsMethod, params: string | GetTransactionsParams) => {
UILayer.send(Channel.Transactions, method, params)
}

export const transactionsCall = instantiateMethodCall(transactions) as {
getAllByKeywords: (params: GetTransactionsParams) => void
get: (walletID: string, hash: string) => void
updateDescription: (params: { hash: string; description: string }) => void
}

export const wallets = (
method: WalletsMethod,
params:
Expand Down
Loading

0 comments on commit 4751817

Please sign in to comment.