add check for status contact code
This commit is contained in:
parent
94fcf57fc5
commit
a3ddf46075
|
@ -1,7 +1,13 @@
|
||||||
import ERC20Token from 'Embark/contracts/ERC20Token';
|
import ERC20Token from 'Embark/contracts/ERC20Token';
|
||||||
import { actions as accountActions } from '../reducers/accounts'
|
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) => {
|
export const fetchAndDispatchAccountsWithBalances = (web3, dispatch) => {
|
||||||
web3.eth.getAccounts((err, addresses) => {
|
web3.eth.getAccounts((err, addresses) => {
|
||||||
if (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] }, '*');
|
||||||
|
}
|
||||||
|
|
|
@ -5,13 +5,15 @@ export const types = createTypes([
|
||||||
'RECEIVE_ACCOUNTS',
|
'RECEIVE_ACCOUNTS',
|
||||||
'UPDATE_DEFAULT_ACCOUNT',
|
'UPDATE_DEFAULT_ACCOUNT',
|
||||||
'ADD_TO_SNT_TOKEN_BALANCE',
|
'ADD_TO_SNT_TOKEN_BALANCE',
|
||||||
'SUBTRACT_FROM_SNT_TOKEN_BALANCE'
|
'SUBTRACT_FROM_SNT_TOKEN_BALANCE',
|
||||||
|
'RECEIVE_STATUS_CONTACT_CODE'
|
||||||
], 'ACCOUNTS')
|
], 'ACCOUNTS')
|
||||||
export const actions = {
|
export const actions = {
|
||||||
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
||||||
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
||||||
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount'),
|
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) {
|
export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
|
@ -51,6 +53,10 @@ export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
accounts
|
accounts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case types.RECEIVE_STATUS_CONTACT_CODE: {
|
||||||
|
const { statusContactCode } = action.payload
|
||||||
|
return { ...state, statusContactCode }
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -60,6 +66,7 @@ export const getAccountState = state => state.acounts;
|
||||||
export const getAccounts = state => state.accounts.accounts;
|
export const getAccounts = state => state.accounts.accounts;
|
||||||
export const getDefaultAccount = state => state.accounts.defaultAccount;
|
export const getDefaultAccount = state => state.accounts.defaultAccount;
|
||||||
export const accountsIsLoading = state => state.accounts.loading;
|
export const accountsIsLoading = state => state.accounts.loading;
|
||||||
|
export const getStatusContactCode = state => state.accounts.statusContactCode;
|
||||||
export const getCurrentAccount = createSelector(
|
export const getCurrentAccount = createSelector(
|
||||||
getDefaultAccount,
|
getDefaultAccount,
|
||||||
getAccounts,
|
getAccounts,
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import web3 from "Embark/web3"
|
import web3 from "Embark/web3"
|
||||||
import EmbarkJS from 'Embark/EmbarkJS'
|
import EmbarkJS from 'Embark/EmbarkJS'
|
||||||
import store from './configureStore'
|
import store from './configureStore'
|
||||||
import { fetchAndDispatchAccountsWithBalances } from '../actions/accounts'
|
import { fetchAndDispatchAccountsWithBalances, checkAndDispatchStatusContactCode } from '../actions/accounts'
|
||||||
|
|
||||||
const dispatch = action => store.dispatch(action)
|
const dispatch = action => store.dispatch(action)
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
__embarkContext.execWhenReady(async () => {
|
__embarkContext.execWhenReady(async () => {
|
||||||
fetchAndDispatchAccountsWithBalances(web3, dispatch)
|
fetchAndDispatchAccountsWithBalances(web3, dispatch)
|
||||||
|
checkAndDispatchStatusContactCode(dispatch)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue