2018-06-01 14:25:59 +00:00
|
|
|
import { createTypes, actionCreator } from 'redux-action-creator'
|
2018-06-05 17:19:21 +00:00
|
|
|
import { createSelector } from 'reselect'
|
2018-06-01 14:25:59 +00:00
|
|
|
|
|
|
|
export const types = createTypes([
|
2018-06-01 20:06:27 +00:00
|
|
|
'RECEIVE_ACCOUNTS',
|
2018-06-05 17:19:21 +00:00
|
|
|
'UPDATE_DEFAULT_ACCOUNT',
|
2018-06-13 19:04:06 +00:00
|
|
|
'ADD_TO_SNT_TOKEN_BALANCE',
|
2018-07-18 18:16:20 +00:00
|
|
|
'SUBTRACT_FROM_SNT_TOKEN_BALANCE',
|
2018-08-14 14:10:31 +00:00
|
|
|
'RECEIVE_STATUS_CONTACT_CODE',
|
|
|
|
'RECEIVE_SNT_ALLOWANCE'
|
2018-06-01 14:25:59 +00:00
|
|
|
], 'ACCOUNTS')
|
|
|
|
export const actions = {
|
2018-06-01 20:06:27 +00:00
|
|
|
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
2018-06-05 17:19:21 +00:00
|
|
|
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
2018-06-13 19:04:06 +00:00
|
|
|
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount'),
|
2018-07-18 18:16:20 +00:00
|
|
|
subtractfromSntTokenBalance: actionCreator(types.SUBTRACT_FROM_SNT_TOKEN_BALANCE, 'amount'),
|
2018-08-14 14:10:31 +00:00
|
|
|
receiveStatusContactCode: actionCreator(types.RECEIVE_STATUS_CONTACT_CODE, 'statusContactCode'),
|
|
|
|
receiveSntAllowance: actionCreator(types.RECEIVE_SNT_ALLOWANCE, 'SNTAllowance')
|
2018-06-01 14:25:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 16:45:11 +00:00
|
|
|
export default function(state = { loading: true, accounts: [] }, action) {
|
2018-06-01 14:25:59 +00:00
|
|
|
switch (action.type) {
|
2018-06-01 20:06:27 +00:00
|
|
|
case types.RECEIVE_ACCOUNTS: {
|
2018-06-01 16:45:11 +00:00
|
|
|
const { defaultAccount, accounts } = action.payload
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: false,
|
|
|
|
defaultAccount,
|
|
|
|
accounts
|
|
|
|
}
|
2018-06-01 20:06:27 +00:00
|
|
|
}
|
|
|
|
case types.UPDATE_DEFAULT_ACCOUNT: {
|
|
|
|
const { defaultAccount } = action.payload
|
|
|
|
return { ...state, defaultAccount }
|
|
|
|
}
|
2018-06-13 16:58:19 +00:00
|
|
|
case types.ADD_TO_SNT_TOKEN_BALANCE: {
|
2018-06-05 17:19:21 +00:00
|
|
|
const currentAccount = { ...getCurrentAccount({accounts: state}) }
|
2018-06-13 21:44:14 +00:00
|
|
|
currentAccount.SNTBalance = Number(currentAccount.SNTBalance) + Number(action.payload.amount)
|
2018-06-05 17:19:21 +00:00
|
|
|
const accounts = [ ...state.accounts ]
|
|
|
|
const idx = accounts.findIndex(a => a.address === currentAccount.address)
|
|
|
|
accounts[idx] = currentAccount
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
accounts
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 19:04:06 +00:00
|
|
|
case types.SUBTRACT_FROM_SNT_TOKEN_BALANCE: {
|
|
|
|
const currentAccount = { ...getCurrentAccount({accounts: state}) }
|
2018-06-13 21:44:14 +00:00
|
|
|
currentAccount.SNTBalance = Number(currentAccount.SNTBalance) - Number(action.payload.amount)
|
2018-06-13 19:04:06 +00:00
|
|
|
const accounts = [ ...state.accounts ]
|
|
|
|
const idx = accounts.findIndex(a => a.address === currentAccount.address)
|
|
|
|
accounts[idx] = currentAccount
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
accounts
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 18:16:20 +00:00
|
|
|
case types.RECEIVE_STATUS_CONTACT_CODE: {
|
|
|
|
const { statusContactCode } = action.payload
|
|
|
|
return { ...state, statusContactCode }
|
|
|
|
}
|
2018-08-14 14:10:31 +00:00
|
|
|
case types.RECEIVE_SNT_ALLOWANCE: {
|
|
|
|
const { SNTAllowance } = action.payload
|
|
|
|
return { ...state, SNTAllowance }
|
|
|
|
}
|
2018-06-01 14:25:59 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 20:06:27 +00:00
|
|
|
export const getAccountState = state => state.acounts;
|
2018-06-01 16:45:11 +00:00
|
|
|
export const getAccounts = state => state.accounts.accounts;
|
2018-06-01 20:06:27 +00:00
|
|
|
export const getDefaultAccount = state => state.accounts.defaultAccount;
|
2018-06-01 16:45:11 +00:00
|
|
|
export const accountsIsLoading = state => state.accounts.loading;
|
2018-07-18 18:16:20 +00:00
|
|
|
export const getStatusContactCode = state => state.accounts.statusContactCode;
|
2018-06-05 17:19:21 +00:00
|
|
|
export const getCurrentAccount = createSelector(
|
|
|
|
getDefaultAccount,
|
|
|
|
getAccounts,
|
|
|
|
(defaultAccount, accounts) => accounts.find(a => a.address === defaultAccount)
|
|
|
|
)
|