add check for status contact code

This commit is contained in:
Barry Gitarts 2018-07-18 14:16:20 -04:00
parent 94fcf57fc5
commit a3ddf46075
3 changed files with 27 additions and 4 deletions

View File

@ -1,7 +1,13 @@
import ERC20Token from 'Embark/contracts/ERC20Token';
import { actions as accountActions } from '../reducers/accounts'
import { isNil } from 'lodash'
const { receiveAccounts, receiveStatusContactCode } = accountActions
const CONTACT_CODE = 'CONTACT_CODE'
const STATUS_API_REQUEST = 'STATUS_API_REQUEST'
const hasContactCode = () => !isNil(STATUS_API) && !isNil(STATUS_API[CONTACT_CODE])
const statusApiSuccess = event => event.data.type === 'STATUS_API_SUCCESS'
const { receiveAccounts } = accountActions
export const fetchAndDispatchAccountsWithBalances = (web3, dispatch) => {
web3.eth.getAccounts((err, addresses) => {
if (addresses) {
@ -17,3 +23,12 @@ export const fetchAndDispatchAccountsWithBalances = (web3, dispatch) => {
}
})
}
export const checkAndDispatchStatusContactCode = dispatch => {
window.addEventListener('message', function (event) {
if (!event.data || !event.data.type) return
if (statusApiSuccess(event) && hasContactCode()) dispatch(receiveStatusContactCode(STATUS_API[CONTACT_CODE]))
});
window.postMessage({ type: STATUS_API_REQUEST, permissions: [CONTACT_CODE] }, '*');
}

View File

@ -5,13 +5,15 @@ export const types = createTypes([
'RECEIVE_ACCOUNTS',
'UPDATE_DEFAULT_ACCOUNT',
'ADD_TO_SNT_TOKEN_BALANCE',
'SUBTRACT_FROM_SNT_TOKEN_BALANCE'
'SUBTRACT_FROM_SNT_TOKEN_BALANCE',
'RECEIVE_STATUS_CONTACT_CODE'
], 'ACCOUNTS')
export const actions = {
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount'),
subtractfromSntTokenBalance: actionCreator(types.SUBTRACT_FROM_SNT_TOKEN_BALANCE, 'amount')
subtractfromSntTokenBalance: actionCreator(types.SUBTRACT_FROM_SNT_TOKEN_BALANCE, 'amount'),
receiveStatusContactCode: actionCreator(types.RECEIVE_STATUS_CONTACT_CODE, 'statusContactCode')
}
export default function(state = { loading: true, accounts: [] }, action) {
@ -51,6 +53,10 @@ export default function(state = { loading: true, accounts: [] }, action) {
accounts
}
}
case types.RECEIVE_STATUS_CONTACT_CODE: {
const { statusContactCode } = action.payload
return { ...state, statusContactCode }
}
default:
return state;
}
@ -60,6 +66,7 @@ export const getAccountState = state => state.acounts;
export const getAccounts = state => state.accounts.accounts;
export const getDefaultAccount = state => state.accounts.defaultAccount;
export const accountsIsLoading = state => state.accounts.loading;
export const getStatusContactCode = state => state.accounts.statusContactCode;
export const getCurrentAccount = createSelector(
getDefaultAccount,
getAccounts,

View File

@ -1,12 +1,13 @@
import web3 from "Embark/web3"
import EmbarkJS from 'Embark/EmbarkJS'
import store from './configureStore'
import { fetchAndDispatchAccountsWithBalances } from '../actions/accounts'
import { fetchAndDispatchAccountsWithBalances, checkAndDispatchStatusContactCode } from '../actions/accounts'
const dispatch = action => store.dispatch(action)
export default () => {
__embarkContext.execWhenReady(async () => {
fetchAndDispatchAccountsWithBalances(web3, dispatch)
checkAndDispatchStatusContactCode(dispatch)
})
}