parent
f5b1fbef38
commit
8dd8ab4259
@ -1,8 +1,60 @@ |
||||
import { shortenAddress } from '@remix-ui/helper' |
||||
import React from 'react' |
||||
import { fetchAccountsListFailed, fetchAccountsListRequest, fetchAccountsListSuccess } from './payload' |
||||
|
||||
let plugin, dispatch: React.Dispatch<any> |
||||
|
||||
const initSettingsTab = (udapp) => async (reducerDispatch: React.Dispatch<any>) => { |
||||
plugin = udapp |
||||
dispatch = reducerDispatch |
||||
export const initSettingsTab = (udapp) => async (reducerDispatch: React.Dispatch<any>) => { |
||||
if (udapp) { |
||||
plugin = udapp |
||||
dispatch = reducerDispatch |
||||
setupEvents() |
||||
setInterval(() => { |
||||
fillAccountsList() |
||||
}, 1000) |
||||
|
||||
fillAccountsList() |
||||
} |
||||
} |
||||
|
||||
const setupEvents = () => { |
||||
plugin.blockchain.events.on('newTransaction', (tx, receipt) => { |
||||
plugin.emit('newTransaction', tx, receipt) |
||||
}) |
||||
|
||||
plugin.blockchain.event.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => { |
||||
// if (!lookupOnly) this.el.querySelector('#value').value = 0
|
||||
if (error) return |
||||
updateAccountBalances() |
||||
}) |
||||
|
||||
const updateAccountBalances = () => { |
||||
// const accounts = $(this.el.querySelector('#txorigin')).children('option')
|
||||
|
||||
// accounts.each((index, account) => {
|
||||
// plugin.blockchain.getBalanceInEther(account.value, (err, balance) => {
|
||||
// if (err) return
|
||||
// const updated = shortenAddress(account.value, balance)
|
||||
|
||||
// if (updated !== account.innerText) { // check if the balance has been updated and update UI accordingly.
|
||||
// account.innerText = updated
|
||||
// }
|
||||
// })
|
||||
// })
|
||||
} |
||||
} |
||||
|
||||
const fillAccountsList = async () => { |
||||
try { |
||||
dispatch(fetchAccountsListRequest()) |
||||
const promise = plugin.blockchain.getAccounts() |
||||
|
||||
|
||||
} |
||||
promise.then((accounts: string[]) => { |
||||
dispatch(fetchAccountsListSuccess(accounts)) |
||||
}).catch((e) => { |
||||
dispatch(fetchAccountsListFailed(e.message)) |
||||
}) |
||||
} catch (e) { |
||||
// addTooltip(`Cannot get account list: ${e}`)
|
||||
} |
||||
} |
||||
|
@ -0,0 +1,19 @@ |
||||
export const fetchAccountsListRequest = () => { |
||||
return { |
||||
type: 'FETCH_ACCOUNTS_LIST_REQUEST' |
||||
} |
||||
} |
||||
|
||||
export const fetchAccountsListSuccess = (accounts: string[]) => { |
||||
return { |
||||
type: 'FETCH_ACCOUNTS_LIST_SUCCESS', |
||||
payload: accounts |
||||
} |
||||
} |
||||
|
||||
export const fetchAccountsListFailed = (error: string) => { |
||||
return { |
||||
type: 'FETCH_ACCOUNTS_LIST_FAILED', |
||||
payload: error |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
interface Action { |
||||
type: string |
||||
payload: any |
||||
} |
||||
|
||||
export interface RunTabState { |
||||
accounts: { |
||||
loadedAccounts: Record<string, any>, |
||||
isRequesting: boolean, |
||||
isSuccessful: boolean, |
||||
error: null |
||||
} |
||||
} |
||||
|
||||
export const runTabInitialState = { |
||||
accounts: { |
||||
loadedAccounts: {}, |
||||
isRequesting: false, |
||||
isSuccessful: false, |
||||
error: null |
||||
} |
||||
} |
||||
|
||||
export const runTabReducer = (state: RunTabState = runTabInitialState, action: Action) => { |
||||
switch (action.type) { |
||||
case 'FETCH_ACCOUNTS_LIST_REQUEST': { |
||||
return { |
||||
...state, |
||||
accounts: { |
||||
...state.accounts, |
||||
isRequesting: true, |
||||
isSuccessful: false, |
||||
error: null |
||||
} |
||||
} |
||||
} |
||||
case 'FETCH_ACCOUNTS_LIST_SUCCESS': { |
||||
const payload: string[] = action.payload as string[] |
||||
|
||||
return { |
||||
...state, |
||||
accounts: { |
||||
...state.accounts, |
||||
loadedAccounts: resolveAccountsList(state, payload), |
||||
isSuccessful: true, |
||||
isRequesting: false, |
||||
error: null |
||||
} |
||||
} |
||||
} |
||||
case 'FETCH_ACCOUNTS_LIST_FAILED': { |
||||
const payload = action.payload as string |
||||
|
||||
return { |
||||
...state, |
||||
accounts: { |
||||
...state.accounts, |
||||
isRequesting: false, |
||||
isSuccessful: false, |
||||
error: payload |
||||
} |
||||
} |
||||
} |
||||
default: |
||||
return state |
||||
} |
||||
} |
||||
|
||||
// TODO: unclear what's the goal of accountListCallId, feels like it can be simplified
|
||||
const resolveAccountsList = async (state: RunTabState, accounts: string[]) => { |
||||
// const txOrigin = this.el.querySelector('#txorigin')
|
||||
const loadedAccounts = state.accounts.loadedAccounts |
||||
|
||||
if (!accounts) accounts = [] |
||||
for (const loadedaddress in loadedAccounts) { |
||||
if (accounts.indexOf(loadedaddress) === -1) { |
||||
// txOrigin.removeChild(txOrigin.querySelector('option[value="' + loadedaddress + '"]'))
|
||||
delete loadedAccounts[loadedaddress] |
||||
} |
||||
} |
||||
for (const i in accounts) { |
||||
const address = accounts[i] |
||||
if (!loadedAccounts[address]) { |
||||
// txOrigin.appendChild(yo`<option value="${address}" >${address}</option>`)
|
||||
loadedAccounts[address] = 1 |
||||
} |
||||
} |
||||
return loadedAccounts |
||||
// txOrigin.setAttribute('value', accounts[0])
|
||||
} |
Loading…
Reference in new issue