-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* neuron-ui talks to remote/api instead of remote/app * AppController is used as instance, not static functions object * Detach menu from app controller * Remove controller and UI action dependency from services
- Loading branch information
Showing
10 changed files
with
283 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { take } from 'rxjs/operators' | ||
|
||
import env from 'env' | ||
import i18n from 'utils/i18n' | ||
import { popContextMenu } from './app/menu' | ||
import { showWindow } from './app/show-window' | ||
import { TransactionsController, WalletsController, SyncInfoController } from 'controllers' | ||
import NetworksService from 'services/networks' | ||
import WalletsService from 'services/wallets' | ||
import SkipDataAndType from 'services/settings/skip-data-and-type' | ||
import { ConnectionStatusSubject } from 'models/subjects/node' | ||
import { SystemScriptSubject } from 'models/subjects/system-script' | ||
import { ResponseCode } from 'utils/const' | ||
|
||
export default class ApiController { | ||
public static loadInitData = async () => { | ||
const walletsService = WalletsService.getInstance() | ||
const networksService = NetworksService.getInstance() | ||
const [ | ||
currentWallet = null, | ||
wallets = [], | ||
currentNetworkID = '', | ||
networks = [], | ||
syncedBlockNumber = '0', | ||
connectionStatus = false, | ||
codeHash = '', | ||
] = await Promise.all([ | ||
walletsService.getCurrent(), | ||
walletsService.getAll(), | ||
networksService.getCurrentID(), | ||
networksService.getAll(), | ||
|
||
SyncInfoController.currentBlockNumber() | ||
.then(res => { | ||
if (res.status) { | ||
return res.result.currentBlockNumber | ||
} | ||
return '0' | ||
}) | ||
.catch(() => '0'), | ||
new Promise(resolve => { | ||
ConnectionStatusSubject.pipe(take(1)).subscribe( | ||
status => { | ||
resolve(status) | ||
}, | ||
() => { | ||
resolve(false) | ||
}, | ||
) | ||
}), | ||
new Promise(resolve => { | ||
SystemScriptSubject.pipe(take(1)).subscribe(({ codeHash: currentCodeHash }) => resolve(currentCodeHash)) | ||
}), | ||
]) | ||
|
||
const minerAddresses = await Promise.all( | ||
wallets.map(({ id }) => | ||
WalletsController.getAllAddresses(id).then(addrRes => { | ||
if (addrRes.result) { | ||
const minerAddr = addrRes.result.find(addr => addr.type === 0 && addr.index === 0) | ||
if (minerAddr) { | ||
return { | ||
address: minerAddr.address, | ||
identifier: minerAddr.identifier, | ||
} | ||
} | ||
} | ||
return undefined | ||
}), | ||
), | ||
) | ||
const addresses: Controller.Address[] = await (currentWallet | ||
? WalletsController.getAllAddresses(currentWallet.id).then(res => res.result) | ||
: []) | ||
|
||
const transactions = currentWallet | ||
? await TransactionsController.getAllByKeywords({ | ||
pageNo: 1, | ||
pageSize: 15, | ||
keywords: '', | ||
walletID: currentWallet.id, | ||
}).then(res => res.result) | ||
: [] | ||
|
||
const skipDataAndType = SkipDataAndType.getInstance().get() | ||
|
||
const initState = { | ||
currentWallet, | ||
wallets: [...wallets.map(({ name, id }, idx: number) => ({ id, name, minerAddress: minerAddresses[idx] }))], | ||
currentNetworkID, | ||
networks, | ||
addresses, | ||
transactions, | ||
syncedBlockNumber, | ||
connectionStatus, | ||
codeHash, | ||
skipDataAndType, | ||
} | ||
|
||
return { status: ResponseCode.Success, result: initState } | ||
} | ||
|
||
public static handleViewError = (error: string) => { | ||
if (env.isDevMode) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
public static isMainWindow = (winID: number) => { | ||
// TODO: Fix this | ||
return winID === 0 // AppController.mainWindow && winID === AppController.mainWindow.id | ||
} | ||
|
||
public static async contextMenu(params: { type: string; id: string }) { | ||
return popContextMenu(params) | ||
} | ||
|
||
public static async showTransactionDetails(hash: string) { | ||
showWindow(`${env.mainURL}#/transaction/${hash}`, i18n.t(`messageBox.transaction.title`, { hash })) | ||
} | ||
} |
Oops, something went wrong.